comments: containers Tar / Cpio / Ogg / AudioWrap

This commit is contained in:
4DBug committed 2026-11-01 01:57:14 -05:00
1 parent a355bf3ca3
commit 3cda652143
4 files changed
+14 -94

No files matched your search

+4 -23
View File
@@ -1,18 +1,7 @@
--!strict
-- containers/AudioWrap: an audio track of a video container (a Types.Demuxer track and its packets) as a standalone
-- stream that an audio format reads, so the api can open it with the audio formats (a format never requires another
-- format). New in rosetta.
-- PCM (pcm_u8 / s16 / s24 / s32 le and be, f32 / f64, alaw, mulaw) -> WAV (big-endian samples byte-swapped);
-- an AVI audio track with its WAVEFORMATEX (track.waveFormat: IMA / MS ADPCM, ...) -> WAV with that fmt chunk;
-- adpcm_swf (Flash ADPCM, FLV SoundFormat 1) -> decoded through codecs/audio/Adpcm into a float WAV;
-- mp3 / mp2 / mp1 -> the frames back to back (format "mp3");
-- flac -> "fLaC" + the metadata blocks (CodecPrivate / dfLa) + the frames;
-- opus -> Ogg Opus (OpusHead from the config or made up, OpusTags, packets with granules from their TOC durations);
-- vorbis -> Ogg Vorbis (the three headers from Matroska's Xiph-laced CodecPrivate or MP4's esds-less layout);
-- aac -> ADTS (from the AudioSpecificConfig) for audio/aac;
-- ac3 -> the syncframes back to back (format "ac3"; E-AC-3 tracks stay UNSUPPORTED).
--
-- local buf, format, err = AudioWrap.wrap(demuxer, track) -- then F.open(buf, { format = format })
-- containers/AudioWrap: a video container's audio track as a stream an audio format reads (a format never requires
-- another). New in rosetta. PCM / WAVEFORMATEX -> WAV, adpcm_swf -> f32 WAV, mp1-3 / ac3 -> frames back to back,
-- flac -> fLaC + metadata blocks, opus / vorbis -> Ogg, aac -> ADTS. E-AC-3 is UNSUPPORTED.
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
@@ -58,7 +47,6 @@ local function concat(packets: { Types.Packet }, prefix: buffer?): buffer
return out
end
-- ------------------------------------------------------------------------------------------------ WAV
local PCM: { [string]: { number } } = { -- tag, bits, bytes swapped (big-endian source)
pcm_u8 = { 1, 8, 0 },
pcm_s8 = { 1, 8, 0 },
@@ -159,7 +147,6 @@ local function swfWav(track: Types.Track, packets: { Types.Packet }): (buffer?,
return wav(f:finish(), data), nil
end
-- ------------------------------------------------------------------------------------------------ Ogg
-- the number of 48 kHz samples in an Opus packet (RFC 6716 section 3.1)
local function opusSamples(b: buffer): number
local n = buffer.len(b)
@@ -315,7 +302,6 @@ local function vorbisModes(id: buffer, setup: buffer): ({ number }?, { boolean }
return nil, nil
end
-- ------------------------------------------------------------------------------------------------ ADTS
local function adts(track: Types.Track, packets: { Types.Packet }): (buffer?, Err.Err?)
local c = track.config
local profile, rateIndex, chanConfig
@@ -353,10 +339,7 @@ local function adts(track: Types.Track, packets: { Types.Packet }): (buffer?, Er
return w:finish(), nil
end
-- ------------------------------------------------------------------------------------------------ trim
-- trim(decoder, frames, length?) -> an audio Decoder that drops the first `frames` sample frames (a container's
-- codec delay: Matroska CodecDelay, an MP4 edit's media time for MP2 / MP3 / AAC, which ffmpeg drops too; Opus
-- handles its pre-skip itself) and, with length, ends after that many frames (an MP4 edit's length: end padding)
-- trim: a container's codec delay (Matroska CodecDelay, an MP4 edit's media time for MP2 / MP3 / AAC, as ffmpeg) and end padding
function AudioWrap.trim(dec: any, skip: number, length: number?): any
if skip <= 0 and length == nil then
return dec
@@ -412,8 +395,6 @@ function AudioWrap.trim(dec: any, skip: number, length: number?): any
return w
end
-- ------------------------------------------------------------------------------------------------ wrap
-- wrap(demuxer, track) -> buffer, audio format name | nil, nil, Err
function AudioWrap.wrap(dmx: any, track: Types.Track): (buffer?, string?, Err.Err?)
if type(track) ~= "table" or track.kind ~= "audio" then
return nil, nil, Err.new(Err.ARG, "not an audio track")
+3 -17
View File
@@ -1,16 +1,7 @@
--!strict
-- containers/Cpio: cpio archives, read and written. Reading: SVR4 "newc" (070701, hex fields, 4-byte alignment),
-- "crc" (070702: newc plus a byte-sum of the data, verified), POSIX.1 "odc" (070707, octal fields, no padding) and the
-- old binary format in both byte orders (magic 070707 as a little / big endian u16, 2-byte alignment), up to the
-- TRAILER!!! entry. Hard links: newc / crc store the data once, on the last link of an inode (as GNU cpio and bsdtar
-- write it); every link of the group reads that data, and the later links name the first one in `hardlink`. Symlinks
-- hold their target as data (`link`). Writing: newc (default), crc or odc, padded to opts.block bytes (default 512, as
-- GNU cpio). Written from the cpio(5) manual pages (libarchive, GNU cpio) and POSIX.1-1988 (odc); no code ported. The
-- archive formats (archive/cpio, archive/rpm's payload) build on it.
--
-- local entries, info = Cpio.read(buf, offset?, length?, opts?) -- { Entry } , { format = "newc" | ... } | nil, Err
-- local out, err = Cpio.write({ { name = "a.txt", data = buf, mode = 0o100644, mtime = 0 } }, { format = "odc" })
--
-- containers/Cpio: cpio, read and written, from cpio(5) (libarchive, GNU cpio) and POSIX.1-1988 (odc). Reads newc
-- (070701), crc (070702, byte sums verified), odc (070707), old binary in both byte orders; writes newc / crc / odc
-- padded to opts.block (512, as GNU cpio). A hard link group's data sits on its last link (as GNU cpio and bsdtar).
-- Never throws: bad data returns nil, Err.
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
@@ -62,7 +53,6 @@ Cpio.TYPE_OF = TYPES -- S_IFMT bits -> type letter
local TRAILER = "TRAILER!!!"
-- format(buf, offset) -> "newc" | "crc" | "odc" | "bin" | "binbe" | nil: the format of the header at offset
function Cpio.format(b: buffer, o: number): string?
local n = buffer.len(b)
if o + 6 <= n then
@@ -116,7 +106,6 @@ function Cpio.checksum(b: buffer, o: number, n: number): number
return s % 4294967296
end
-- one header at p -> entry fields, header end (the name's start), name size, data size | nil, Err
local function header(b: buffer, p: number, e: number, fmt: string): (any, Err.Err?)
local h: any = {}
local nameAt
@@ -179,7 +168,6 @@ local function header(b: buffer, p: number, e: number, fmt: string): (any, Err.E
return h, nil
end
-- read(buf, offset?, length?, opts?) -> { Entry }, Info | nil, Err
function Cpio.read(b: buffer, offset: number?, length: number?, opts: ReadOptions?): ({ Entry }?, any)
local o = offset or 0
local e = math.min(buffer.len(b), o + (length or (buffer.len(b) - o)))
@@ -278,7 +266,6 @@ function Cpio.read(b: buffer, offset: number?, length: number?, opts: ReadOption
return out, { format = fmt }
end
-- ------------------------------------------------------------------------------------------------ writing
export type WriteEntry = {
name: string,
data: buffer?, -- file data (a symlink: its target, else taken from link)
@@ -316,7 +303,6 @@ local function oct(v: number, w: number): string?
return string.rep("0", w - #s) .. s
end
-- write(entries, opts?) -> buffer | nil, Err
function Cpio.write(entries: { WriteEntry }, opts: WriteOptions?): (buffer?, Err.Err?)
local fmt = (opts and opts.format) or "newc"
if fmt ~= "newc" and fmt ~= "crc" and fmt ~= "odc" then
+4 -37
View File
@@ -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)
+3 -17
View File
@@ -1,16 +1,7 @@
--!strict
-- containers/Tar: tar archives (POSIX.1-1988 ustar, POSIX.1-2001 pax, GNU), read and written. Reading handles
-- v7 / ustar / GNU / pax headers: the ustar name prefix, pax extended headers ('x' per file, 'g' global: path,
-- linkpath, size, mtime, uid, gid, uname, gname), GNU long names and link names ('L' / 'K'), base-256 numeric fields,
-- header checksums (unsigned and the historical signed sum) and the end-of-archive blocks. Writing produces ustar
-- headers, with a pax extended header for what ustar cannot hold (long or non-ASCII names, sizes of 8 GiB and more,
-- fractional / negative mtimes) or, with opts.format = "gnu", GNU 'L' / 'K' records. Written from POSIX
-- (pax(1), IEEE Std 1003.1-2017) and the GNU tar manual. Not supported: sparse files (GNU 'S', pax GNU.sparse), multi-
-- volume archives (they list, with type "S" / "M"; reading them returns their stored bytes).
--
-- local entries, err = Tar.read(buf, offset?, length?) -- { { name, size, type, mode, mtime, link, offset, ... } }
-- local out, err = Tar.write({ { name = "a.txt", data = buf, mtime = 0 }, { name = "d/", dir = true } }, opts)
--
-- containers/Tar: tar (POSIX.1-1988 ustar, POSIX.1-2001 pax, GNU), read and written; from POSIX pax(1) (IEEE Std
-- 1003.1-2017) and the GNU tar manual. Reads v7 / ustar / GNU / pax headers ('x' / 'g', 'L' / 'K', base-256, signed
-- checksums); writes ustar + pax, or GNU 'L' / 'K'. Not supported: sparse files, multi-volume (listed as "S" / "M").
-- Never throws: bad data returns nil, Err.
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
@@ -78,7 +69,6 @@ local function checksum(b: buffer, o: number): (number, number)
return u, s
end
-- valid(buf, offset) -> true when the block at offset is a tar header with a correct checksum
function Tar.valid(b: buffer, o: number): boolean
if o + BLOCK > buffer.len(b) or zeros(b, o) then
return false
@@ -117,7 +107,6 @@ end
export type ReadOptions = { maxEntries: number? } -- default 1e6
-- read(buf, offset?, length?, opts?) -> { Entry } | nil, Err
function Tar.read(b: buffer, offset: number?, length: number?, opts: ReadOptions?): ({ Entry }?, Err.Err?)
local o = offset or 0
local e = math.min(buffer.len(b), o + (length or (buffer.len(b) - o)))
@@ -261,7 +250,6 @@ function Tar.read(b: buffer, offset: number?, length: number?, opts: ReadOptions
return out, nil
end
-- ------------------------------------------------------------------------------------------------ writing
export type WriteEntry = {
name: string,
data: buffer?,
@@ -305,7 +293,6 @@ local function paxRecord(key: string, value: string): string
return tostring(n) .. body
end
-- one header block (the checksum filled in)
local function header(name: string, typ: string, size: number, e: WriteEntry, prefix: string, link: string, gnu: boolean): buffer
local h = buffer.create(BLOCK)
field(h, 0, 100, name)
@@ -340,7 +327,6 @@ local function split(path: string): (string?, string?)
return nil, nil
end
-- write(entries, opts?) -> buffer | nil, Err
function Tar.write(entries: { WriteEntry }, opts: WriteOptions?): (buffer?, Err.Err?)
local fmt = (opts and opts.format) or "pax"
local w = Bytes.writer(BLOCK * (2 * #entries + 2))