comments: containers Tar / Cpio / Ogg / AudioWrap
This commit is contained in:
1 parent
a355bf3ca3
commit
3cda652143
4 files changed
+14
-94
No files matched your search
@@ -1,24 +1,9 @@
|
||||
--!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
|
||||
-- containers/Ogg: the Ogg container (RFC 3533): a demuxer (absorbed from ogg-luau be9f604, same author, no third-party
|
||||
-- code) and the muxer (Ogg/Mux, new). Packets are reassembled across pages per serial; seeking bisects byte offsets
|
||||
-- to the page holding a granule. Never throws.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Page = require("@rosetta/containers/Ogg/Page")
|
||||
local Mux = require("@rosetta/containers/Ogg/Mux")
|
||||
@@ -51,7 +36,6 @@ export type Stream = {
|
||||
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
|
||||
|
||||
@@ -76,7 +60,6 @@ local function newReader(d: any, serial: number, start: number): any
|
||||
}, 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
|
||||
@@ -129,7 +112,6 @@ function Reader._nextPage(self: any): boolean
|
||||
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
|
||||
@@ -207,7 +189,6 @@ function Reader.nextPacket(self: any): Packet?
|
||||
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
|
||||
@@ -218,7 +199,6 @@ function Reader.rewind(self: any)
|
||||
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
|
||||
@@ -237,10 +217,7 @@ function Reader._at(self: any, p: PageInfo)
|
||||
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).
|
||||
-- seekGranule: after the last page with granule <= g (bisection, then a short scan); before every granule rewinds (-1)
|
||||
function Reader.seekGranule(self: any, g: number): (number?, Err.Err?)
|
||||
local b, d = self.buf, self.demux
|
||||
local serial = self.serial
|
||||
@@ -286,13 +263,9 @@ function Reader.seekGranule(self: any, g: number): (number?, Err.Err?)
|
||||
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
|
||||
@@ -359,7 +332,6 @@ function Ogg.open(src: any, opts: { verify: boolean?, scanAll: boolean? }?): (an
|
||||
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
|
||||
@@ -382,7 +354,6 @@ function Demux._scanLast(self: any)
|
||||
end
|
||||
end
|
||||
|
||||
-- Every logical stream: { serial, codec, firstPacket, bosOffset, lastGranule }
|
||||
function Demux.streams(self: any): { Stream }
|
||||
self:_scanLast()
|
||||
return self._streams
|
||||
@@ -393,7 +364,6 @@ function Demux.stream(self: any, serial: number): Stream?
|
||||
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
|
||||
@@ -403,7 +373,6 @@ function Demux.find(self: any, codec: string): number?
|
||||
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
|
||||
@@ -438,7 +407,6 @@ function Demux.rewind(self: any, serial: number)
|
||||
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()
|
||||
@@ -446,7 +414,6 @@ function Demux.packets(self: any, serial: number): () -> Packet?
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user