absorb: containers/Ogg (demuxer from ogg-luau be9f604, Err values, Reader input) + a new muxer (page writer); tests vs opusenc / flac / ffmpeg fixtures

This commit is contained in:
4DBug committed 2026-10-31 08:26:39 -05:00
1 parent cdcb5b0b77
commit c22b50e80b
6 files changed
+1322

No files matched your search

+208
View File
@@ -0,0 +1,208 @@
--!strict
--!native
--!optimize 2
-- containers/Ogg/Mux: the Ogg page writer (RFC 3533), for Ogg Opus / Vorbis / FLAC output. New in formats-luau.
-- Packets are laced into pages per logical stream; a page is closed when it reaches opts.pageSize bytes of data (or
-- 255 lacing values), when a packet asks for flush, and at the end (eos). The page granule is the granule of the last
-- packet that finishes on it (-1 when none does). Pages go to the output in the order they close, so the bos pages
-- of a multiplexed file come first when every stream's first packet is written (with flush) before any other.
--
-- local mux = Mux.new({ pageSize = 4096 })
-- local s = mux:stream(0x1234) -- serial (default: 1, 2, ...)
-- s:packet(head, 0, { flush = true }) -- OpusHead / FLAC mapping header: alone on the bos page
-- s:packet(tags, 0, { flush = true })
-- s:packet(audio, granule) -- granule after this packet (samples, at the codec's rate)
-- local out = mux:finish() -- closes every stream with an eos page
--
-- Never throws: a bad argument returns false, Err.
local Err = require("@formats/core/Err")
local Page = require("@formats/containers/Ogg/Page")
local CRC = Page.CRC_TABLE
local readu8, writeu8, writeu32, writei32 = buffer.readu8, buffer.writeu8, buffer.writeu32, buffer.writei32
local bxor, lshift, rshift = bit32.bxor, bit32.lshift, bit32.rshift
local Mux = {}
Mux.__index = Mux
local Stream = {}
Stream.__index = Stream
export type Options = {
pageSize: number?, -- close a page once its data reaches this many bytes (default 4096; at most 255 * 255)
}
export type PacketOptions = {
flush: boolean?, -- close the page after this packet (headers; default false)
eos: boolean?, -- this is the stream's last packet (default false: finish() writes an empty eos page if needed)
}
-- new(opts?) -> Mux
function Mux.new(opts: Options?): any
local size = opts and opts.pageSize or 4096
if type(size) ~= "number" or size < 1 then
size = 4096
end
return setmetatable({
pageSize = math.min(size, 255 * 255),
chunks = {} :: { buffer },
total = 0,
streams = {} :: { any },
nextSerial = 1,
}, Mux)
end
-- stream(serial?) -> Stream (serials must be unique in the file)
function Mux.stream(self: any, serial: number?): any
local sr = serial or self.nextSerial
self.nextSerial = math.max(self.nextSerial, sr + 1)
local s = setmetatable({
mux = self,
serial = sr % 4294967296,
seq = 0,
lacing = {} :: { number },
parts = {} :: { any }, -- buffer, offset, length triples of the page data
dataLen = 0,
granule = -1,
continued = false, -- the page being built starts with the tail of a packet
first = true,
ended = false,
lastGranule = -1,
}, Stream)
table.insert(self.streams, s)
return s
end
-- the CRC of a page (the crc field written as zero)
local function crc(b: buffer, n: number): number
local c = 0
for i = 0, n - 1 do
c = bxor(lshift(c, 8), CRC[bxor(rshift(c, 24), readu8(b, i)) + 1])
end
return c
end
-- close the page being built (also when empty, for eos)
function Stream._flush(self: any, eos: boolean)
local nseg = #self.lacing
local n = 27 + nseg + self.dataLen
local b = buffer.create(n)
buffer.writestring(b, 0, "OggS")
local flags = (if self.continued then 1 else 0) + (if self.first then 2 else 0) + (if eos then 4 else 0)
writeu8(b, 5, flags)
local g = self.granule
if g < 0 then
writeu32(b, 6, 0xFFFFFFFF)
writei32(b, 10, -1)
else
writeu32(b, 6, g % 4294967296)
writeu32(b, 10, (g // 4294967296) % 4294967296)
end
writeu32(b, 14, self.serial)
writeu32(b, 18, self.seq % 4294967296)
writeu8(b, 26, nseg)
for i = 1, nseg do
writeu8(b, 26 + i, self.lacing[i])
end
local o = 27 + nseg
local parts = self.parts
for i = 1, #parts, 3 do
buffer.copy(b, o, parts[i], parts[i + 1], parts[i + 2])
o += parts[i + 2]
end
writeu32(b, 22, crc(b, n))
local m = self.mux
table.insert(m.chunks, b)
m.total += n
self.seq += 1
self.first = false
self.lacing, self.parts, self.dataLen, self.granule = {}, {}, 0, -1
self.continued = false
if eos then
self.ended = true
end
end
-- packet(data, granule, opts?) -> true | false, Err. data: buffer or string; granule: the position after this packet
-- (-1: unknown, not recommended); offset / length select a range of data.
function Stream.packet(self: any, data: any, granule: number, opts: PacketOptions?, offset: number?, length: number?): (boolean, Err.Err?)
if self.ended then
return false, Err.new(Err.ARG, "packet after the end of stream " .. self.serial)
end
local b: buffer
if type(data) == "string" then
b = buffer.fromstring(data)
elseif type(data) == "buffer" then
b = data
else
return false, Err.new(Err.ARG, "packet: data must be a buffer or a string")
end
if type(granule) ~= "number" then
return false, Err.new(Err.ARG, "packet: granule must be a number")
end
local off = offset or 0
local len = length or (buffer.len(b) - off)
if off < 0 or len < 0 or off + len > buffer.len(b) then
return false, Err.new(Err.ARG, "packet: range outside the buffer")
end
local pageSize = self.mux.pageSize
local left = len
local pos = off
-- lacing: 255-byte segments then one < 255 (0 when the size is a multiple of 255)
while true do
if #self.lacing == 255 or (self.dataLen >= pageSize and #self.lacing > 0) then
self:_flush(false)
self.continued = pos > off -- the next page starts inside this packet
end
local seg = math.min(left, 255)
table.insert(self.lacing, seg)
if seg > 0 then
table.insert(self.parts, b)
table.insert(self.parts, pos)
table.insert(self.parts, seg)
end
self.dataLen += seg
pos += seg
left -= seg
if seg < 255 then
break
end
end
if granule >= 0 then -- -1 (unknown) keeps the granule of an earlier packet finishing on this page
self.granule = granule
self.lastGranule = granule
end
local eos = opts and opts.eos or false
if eos then
self:_flush(true)
elseif (opts and opts.flush) or self.dataLen >= pageSize or #self.lacing == 255 then
self:_flush(false)
end
return true, nil
end
-- flush(): close the current page now (if it holds anything)
function Stream.flush(self: any)
if not self.ended and #self.lacing > 0 then
self:_flush(false)
end
end
-- finish() -> the file: closes every stream with an eos page (an empty one when its last page was already written)
function Mux.finish(self: any): buffer
for _, s in self.streams do
if not s.ended then
if #s.lacing == 0 then
s.granule = s.lastGranule
end
s:_flush(true)
end
end
local out = buffer.create(self.total)
local o = 0
for _, c in self.chunks do
buffer.copy(out, o, c, 0, buffer.len(c))
o += buffer.len(c)
end
return out
end
return Mux
+185
View File
@@ -0,0 +1,185 @@
--!strict
--!native
--!optimize 2
-- containers/Ogg/Page: Ogg pages (RFC 3533). Absorbed from ogg-luau be9f604 (same author, no third-party code): the CRC-32 (poly 0x04C11DB7, MSB first, init 0, the CRC field read as zero), header
-- parsing with bounds checks, and the capture-pattern resync (find the next valid page at or after a byte offset,
-- forwards or backwards). Nothing here throws: invalid or truncated data gives nil.
--
-- A page header (27 bytes + segment table):
-- 0 "OggS" | 4 version (0) | 5 flags (1 continued, 2 bos, 4 eos) | 6 granule (i64 LE) | 14 serial (u32)
-- 18 sequence (u32) | 22 crc (u32) | 26 segment count | 27 lacing values | data
local readu8, readu32, readi32 = buffer.readu8, buffer.readu32, buffer.readi32
local band, bxor, lshift, rshift = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift
local Page = {}
export type Info = {
offset: number, -- byte offset of "OggS"
size: number, -- header + segment table + data
headerSize: number, -- 27 + segment count
nsegs: number,
flags: number,
continued: boolean,
bos: boolean,
eos: boolean,
granule: number, -- -1 when no packet finishes on this page
serial: number,
sequence: number,
}
-- CRC table, MSB-first
local CRC = table.create(256, 0)
for i = 0, 255 do
local r = lshift(i, 24)
for _ = 1, 8 do
if band(r, 0x80000000) ~= 0 then
r = bxor(lshift(r, 1), 0x04C11DB7)
else
r = lshift(r, 1)
end
end
CRC[i + 1] = r
end
Page.CRC_TABLE = CRC
-- CRC of bytes [from, to) (caller guarantees bounds), continuing from crc
local function crcRange(b: buffer, from: number, to: number, crc: number): number
for i = from, to - 1 do
crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, i)) + 1])
end
return crc
end
-- CRC-32 of a byte range, the Ogg way (the crc field is not zeroed: use for arbitrary data)
function Page.crc32(b: buffer, offset: number?, len: number?): number
local o = offset or 0
local n = len or (buffer.len(b) - o)
if o < 0 or n < 0 or o + n > buffer.len(b) then
return 0
end
return crcRange(b, o, o + n, 0)
end
-- Parse the page at pos. Returns Info, or nil + reason ("short" if the buffer ends inside the page, "magic",
-- "version", "crc"). verify = false skips the CRC check.
function Page.parse(b: buffer, pos: number, verify: boolean?): (Info?, string?)
local size = buffer.len(b)
if pos < 0 or pos + 27 > size then
return nil, "short"
end
if readu32(b, pos) ~= 0x5367674F then -- "OggS"
return nil, "magic"
end
if readu8(b, pos + 4) ~= 0 then
return nil, "version"
end
local nsegs = readu8(b, pos + 26)
local hsize = 27 + nsegs
if pos + hsize > size then
return nil, "short"
end
local dlen = 0
for i = pos + 27, pos + hsize - 1 do
dlen += readu8(b, i)
end
local total = hsize + dlen
if pos + total > size then
return nil, "short"
end
if verify ~= false then
local c = crcRange(b, pos, pos + 22, 0)
for _ = 1, 4 do
c = bxor(lshift(c, 8), CRC[rshift(c, 24) + 1])
end
c = crcRange(b, pos + 26, pos + total, c)
if c ~= readu32(b, pos + 22) then
return nil, "crc"
end
end
local flags = readu8(b, pos + 5)
return {
offset = pos,
size = total,
headerSize = hsize,
nsegs = nsegs,
flags = flags,
continued = band(flags, 1) ~= 0,
bos = band(flags, 2) ~= 0,
eos = band(flags, 4) ~= 0,
granule = readi32(b, pos + 10) * 4294967296 + readu32(b, pos + 6),
serial = readu32(b, pos + 14),
sequence = readu32(b, pos + 18),
}, nil
end
-- The first valid page starting at an offset in [pos, limit) (limit defaults to the buffer end). Resyncs on the
-- capture pattern: bytes that are not a valid page (garbage, a bad CRC, a truncated page) are skipped.
function Page.find(b: buffer, pos: number, limit: number?, verify: boolean?): Info?
local size = buffer.len(b)
local lim = math.min(limit or size, size - 27 + 1)
if pos < 0 then
pos = 0
end
while pos < lim do
if readu8(b, pos) == 0x4F and readu32(b, pos) == 0x5367674F then
local p = Page.parse(b, pos, verify)
if p then
return p
end
end
pos += 1
end
return nil
end
-- The last valid page starting at an offset in [floor, pos] (searching backwards).
function Page.findBack(b: buffer, pos: number, floor: number?, verify: boolean?): Info?
local size = buffer.len(b)
local lo = math.max(floor or 0, 0)
if pos > size - 27 then
pos = size - 27
end
while pos >= lo do
if readu8(b, pos) == 0x4F and readu32(b, pos) == 0x5367674F then
local p = Page.parse(b, pos, verify)
if p then
return p
end
end
pos -= 1
end
return nil
end
-- The identification of a codec from a stream's first packet (the bos page's packet)
function Page.codec(b: buffer, off: number, len: number): string
local function is(s: string, at: number): boolean
if at + #s > len then
return false
end
for i = 1, #s do
if readu8(b, off + at + i - 1) ~= string.byte(s, i) then
return false
end
end
return true
end
if is("OpusHead", 0) then
return "opus"
elseif len >= 5 and readu8(b, off) == 0x7F and is("FLAC", 1) then
return "flac"
elseif len >= 7 and readu8(b, off) == 1 and is("vorbis", 1) then
return "vorbis"
elseif len >= 7 and readu8(b, off) == 0x80 and is("theora", 1) then
return "theora"
elseif is("Speex ", 0) then
return "speex"
elseif is("fishead", 0) then
return "skeleton"
elseif is("fLaC", 0) then
return "flac-old" -- pre-1.1.1 Ogg FLAC mapping
end
return "unknown"
end
return Page
+456
View File
@@ -0,0 +1,456 @@
--!strict
--!native
--!optimize 2
-- containers/Ogg: the Ogg container (RFC 3533): a demuxer and a muxer (page writer). Absorbed from ogg-luau be9f604
-- (same author, no third-party code; the demuxer), the muxer is new. Used by audio/opus, audio/flac (Ogg FLAC) and
-- the Vorbis decoder / encoders. Pages are CRC-checked and resynced on the capture
-- pattern; packets are reassembled across pages (lacing values, continued packets) per logical stream (serial);
-- seeking bisects byte offsets to the page that holds a granule position. No Roblox APIs; nothing throws (bad
-- input gives nil, err).
--
-- local demux, err = Ogg.open(buf)
-- for _, s in demux:streams() do print(s.serial, s.codec, s.lastGranule) end
-- local r = demux:reader(serial) -- or demux:packets(serial) as a for-iterator
-- local p = r:nextPacket() -- { data, offset, size, granule, eos, bos, ... } / nil
-- local start = r:seekGranule(48000) -- next packet starts at granule `start` (<= target)
--
-- local mux = Ogg.muxer() -- the page writer (Containers/Ogg/Mux)
-- local s = mux:stream(serial?) -- one logical stream
-- s:packet(idHeader, 0, { flush = true }) -- headers on their own pages
-- s:packet(audio, granuleAfterThisPacket) -- pages fill up to opts.pageSize (default 4096) bytes
-- local file = mux:finish() -- every stream ends with an eos page
local Err = require("@formats/core/Err")
local Page = require("@formats/containers/Ogg/Page")
local Mux = require("@formats/containers/Ogg/Mux")
local readu8 = buffer.readu8
type PageInfo = any -- Page.Info (the sibling module is untyped here: required through `script` in Roblox)
local Ogg = {}
Ogg.Page = Page
Ogg.Mux = Mux
Ogg.muxer = Mux.new
export type Packet = {
data: buffer, -- the packet bytes are data[offset .. offset + size - 1]
offset: number,
size: number,
granule: number, -- the page granule if this is the last packet finishing on its page, else -1
pageGranule: number, -- the granule of the page the packet finishes on (may be -1)
bos: boolean, -- first packet of the stream
eos: boolean, -- last packet of the stream (last packet finishing on an eos page)
pageOffset: number, -- byte offset of the page the packet finishes on
gap: boolean, -- data was lost before this packet (a missing page / bad CRC in between)
}
export type Stream = {
serial: number,
codec: string, -- "opus" | "flac" | "vorbis" | "theora" | "speex" | "skeleton" | "flac-old" | "unknown"
firstPacket: buffer, -- a copy of the identification packet
bosOffset: number, -- byte offset of the bos page
lastGranule: number, -- the last known granule of the stream (-1 if none)
}
-- Reader: the packet cursor of one logical stream
local Reader = {}
Reader.__index = Reader
local function newReader(d: any, serial: number, start: number): any
return setmetatable({
demux = d,
buf = d.buf,
serial = serial,
start = start, -- the bos page offset
pagePos = start, -- where to look for the next page
page = nil :: PageInfo?,
seg = 0, -- next segment index on the page
dataPos = 0, -- byte offset of the next segment's data
lastComplete = -1, -- index of the last segment ending a packet on this page
firstPacketOnPage = true,
parts = {} :: { number }, -- pending partial packet: offset, len pairs
partLen = 0,
lastSeq = -1,
gap = false,
ended = false,
total = 0,
}, Reader)
end
-- Load the next page of this serial; false at the end of the data
function Reader._nextPage(self: any): boolean
local b, d = self.buf, self.demux
while true do
local p = Page.find(b, self.pagePos, nil, d.verify)
if not p then
return false
end
self.pagePos = p.offset + p.size
if p.serial == self.serial then
if self.lastSeq >= 0 and p.sequence ~= (self.lastSeq + 1) % 4294967296 then
-- a page is missing: drop the partial packet
self.parts, self.partLen = {}, 0
self.gap = true
end
self.lastSeq = p.sequence
self.page = p
self.seg = 0
self.dataPos = p.offset + p.headerSize
self.firstPacketOnPage = true
local lc = -1
local base = p.offset + 27
for i = 0, p.nsegs - 1 do
if readu8(b, base + i) < 255 then
lc = i
end
end
self.lastComplete = lc
if p.continued then
if self.partLen == 0 and #self.parts == 0 then
-- the start of this packet is not ours (after a seek or a loss): skip its tail
local s = 0
while s < p.nsegs do
local l = readu8(b, base + s)
self.dataPos += l
s += 1
if l < 255 then
break
end
end
self.seg = s
self.firstPacketOnPage = false
end
elseif #self.parts > 0 then
-- a fresh packet while one was pending: the pending one is lost
self.parts, self.partLen = {}, 0
self.gap = true
end
return true
end
end
end
-- The next packet, or nil at the end of the stream
function Reader.nextPacket(self: any): Packet?
if self.ended then
return nil
end
local b = self.buf
while true do
local p = self.page
if p and self.seg < p.nsegs then
local base = p.offset + 27
local start = self.dataPos
local len = 0
local s = self.seg
local complete = false
while s < p.nsegs do
local l = readu8(b, base + s)
s += 1
len += l
if l < 255 then
complete = true
break
end
end
self.seg = s
self.dataPos = start + len
if complete then
local data, off, size = b, start, len
local parts = self.parts
if #parts > 0 then
size = self.partLen + len
data = buffer.create(size)
off = 0
local o = 0
for i = 1, #parts, 2 do
buffer.copy(data, o, b, parts[i], parts[i + 1])
o += parts[i + 1]
end
buffer.copy(data, o, b, start, len)
self.parts, self.partLen = {}, 0
end
local last = (s - 1) == self.lastComplete
local pkt = {
data = data,
offset = off,
size = size,
granule = if last then p.granule else -1,
pageGranule = p.granule,
bos = p.bos and self.firstPacketOnPage,
eos = p.eos and last,
pageOffset = p.offset,
gap = self.gap,
}
self.firstPacketOnPage = false
self.gap = false
self.total += 1
if pkt.eos then
self.ended = true
end
return pkt
else
-- the packet continues on a later page
table.insert(self.parts, start)
table.insert(self.parts, len)
self.partLen += len
end
else
if p and p.eos then
self.ended = true
return nil
end
if not self:_nextPage() then
self.ended = true
return nil
end
end
end
end
-- Back to the start of the stream (the identification packet comes next)
function Reader.rewind(self: any)
self.pagePos = self.start
self.page = nil
self.seg = 0
self.parts, self.partLen = {}, 0
self.lastSeq = -1
self.gap = false
self.ended = false
end
-- Position on page p: the packets finishing on p are skipped; the next packet is the one that starts after them
function Reader._at(self: any, p: PageInfo)
self:rewind()
self.pagePos = p.offset
self:_nextPage() -- loads p (it is a valid page of this serial)
local b = self.buf
local base = p.offset + 27
local dp = p.offset + p.headerSize
for i = 0, self.lastComplete do
dp += readu8(b, base + i)
end
self.seg = self.lastComplete + 1
self.dataPos = dp
self.parts, self.partLen = {}, 0
self.firstPacketOnPage = false
self.gap = false
self.ended = false
end
-- Seek so that the next packet is the first one that starts at or before granule g: positions after the last
-- page whose granule is <= g (found by bisection over byte offsets, then a short linear scan). Returns the
-- granule at the start of that next packet, or nil, err. A g before every granule rewinds to the stream start
-- (headers included) and returns -1. A g past the end leaves the reader at the end (nextPacket gives nil).
function Reader.seekGranule(self: any, g: number): (number?, Err.Err?)
local b, d = self.buf, self.demux
local serial = self.serial
local verify = d.verify
local function nextOf(pos: number, limit: number?): PageInfo?
while true do
local p = Page.find(b, pos, limit, verify)
if not p then
return nil
end
if p.serial == serial and p.granule ~= -1 then
return p
end
pos = p.offset + p.size
end
end
local lo, hi = self.start, buffer.len(b)
local best: PageInfo? = nil
while hi - lo > 65536 do
local mid = (lo + hi) // 2
local p = nextOf(mid, hi)
if p and p.granule <= g then
best = p
lo = p.offset
else
hi = mid
end
end
local pos = lo
while true do
local p = nextOf(pos)
if not p or p.granule > g then
break
end
best = p
pos = p.offset + p.size
end
if not best then
self:rewind()
return -1, nil
end
self:_at(best)
return best.granule, nil
end
-- Demux
local Demux = {}
Demux.__index = Demux
-- open(src, opts?) -> Demux | nil, Err. src: a buffer (the whole file) or a core/Bytes Reader (its range is copied
-- when it is not the whole buffer). opts.verify = false skips CRC checks (faster on trusted data); opts.scanAll =
-- true lists every bos page in the file (chained streams), not just the first group
function Ogg.open(src: any, opts: { verify: boolean?, scanAll: boolean? }?): (any, Err.Err?)
local buf: buffer
if type(src) == "buffer" then
buf = src
elseif type(src) == "table" and type(src.buf) == "buffer" and type(src.base) == "number" and type(src.limit) == "number" then
if src.base == 0 and src.limit == buffer.len(src.buf) then
buf = src.buf
else
local n = math.max(0, src.limit - src.base)
buf = buffer.create(n)
buffer.copy(buf, 0, src.buf, src.base, n)
end
else
return nil, Err.new(Err.ARG, "open: expected a buffer or a Reader")
end
local verify = not (opts and opts.verify == false)
local first = Page.find(buf, 0, nil, verify)
if not first then
return nil, Err.new(Err.CORRUPT, "no Ogg page found")
end
local d = setmetatable({
buf = buf,
size = buffer.len(buf),
verify = verify,
_streams = {} :: { Stream },
_bySerial = {} :: { [number]: Stream },
_readers = {} :: { [number]: any },
_lastDone = false,
}, Demux)
-- the bos pages (at the start of the file, and later ones for chains if scanAll)
local pos = first.offset
local scanAll = opts and opts.scanAll
while true do
local p = Page.find(buf, pos, nil, verify)
if not p then
break
end
if p.bos and not d._bySerial[p.serial] then
local st = {
serial = p.serial,
codec = "unknown",
firstPacket = buffer.create(0),
bosOffset = p.offset,
lastGranule = -1,
}
table.insert(d._streams, st)
d._bySerial[p.serial] = st
local r = newReader(d, p.serial, p.offset)
local pk = r:nextPacket()
if pk then
local c = buffer.create(pk.size)
buffer.copy(c, 0, pk.data, pk.offset, pk.size)
st.firstPacket = c
st.codec = Page.codec(c, 0, pk.size)
end
elseif not p.bos and not scanAll then
break
end
pos = p.offset + p.size
end
if #d._streams == 0 then
return nil, Err.new(Err.CORRUPT, "no beginning-of-stream page")
end
return d, nil
end
-- Scan backwards from the end for each stream's last granule
function Demux._scanLast(self: any)
if self._lastDone then
return
end
self._lastDone = true
local b = self.buf
local need = #self._streams
local pos = self.size - 27
while need > 0 do
local p = Page.findBack(b, pos, 0, self.verify)
if not p then
break
end
local st = self._bySerial[p.serial]
if st and st.lastGranule == -1 and p.granule ~= -1 then
st.lastGranule = p.granule
need -= 1
end
pos = p.offset - 1
end
end
-- Every logical stream: { serial, codec, firstPacket, bosOffset, lastGranule }
function Demux.streams(self: any): { Stream }
self:_scanLast()
return self._streams
end
function Demux.stream(self: any, serial: number): Stream?
self:_scanLast()
return self._bySerial[serial]
end
-- The serial of the first stream of a codec, or nil
function Demux.find(self: any, codec: string): number?
for _, s in self._streams do
if s.codec == codec then
return s.serial
end
end
return nil
end
-- The packet cursor of a stream (one per serial, shared by nextPacket / seekGranule below), or nil, Err
function Demux.reader(self: any, serial: number): (any, Err.Err?)
local r = self._readers[serial]
if r then
return r, nil
end
local st = self._bySerial[serial]
if not st then
return nil, Err.new(Err.ARG, "unknown serial " .. tostring(serial))
end
r = newReader(self, serial, st.bosOffset)
self._readers[serial] = r
return r, nil
end
function Demux.nextPacket(self: any, serial: number): Packet?
local r = self:reader(serial)
return if r then r:nextPacket() else nil
end
function Demux.seekGranule(self: any, serial: number, g: number): (number?, Err.Err?)
local r, err = self:reader(serial)
if not r then
return nil, err
end
return r:seekGranule(g)
end
function Demux.rewind(self: any, serial: number)
local r = self:reader(serial)
if r then
r:rewind()
end
end
-- for p in demux:packets(serial) do ... end (continues from the stream's current position)
function Demux.packets(self: any, serial: number): () -> Packet?
local r = self:reader(serial)
return function()
return if r then r:nextPacket() else nil
end
end
-- Copy a packet's bytes into a fresh buffer of its own
function Ogg.copy(p: Packet): buffer
local c = buffer.create(p.size)
buffer.copy(c, 0, p.data, p.offset, p.size)
return c
end
return Ogg