comments: containers MpegTs / IsoBmff Demux
This commit is contained in:
1 parent
a0a11af953
commit
facdf124a1
2 files changed
+5
-49
No files matched your search
@@ -1,16 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- containers/MpegTs: an MPEG transport stream demuxer (ISO/IEC 13818-1). Absorbed from mpegts-luau 6e16197 (same
|
||||
-- author; no third-party code). Changed: this header, and the unit queue keeps an explicit tail (pull() used #q on a
|
||||
-- table with holes at its front, which LuaJIT's border rule makes lose units). 188-byte packets with resync, PAT / PMT sections
|
||||
-- (assembled across packets, CRC-checked), adaptation fields (PCR, discontinuity, random access), continuity
|
||||
-- counter checks (gaps are flagged on the unit, duplicates dropped), PES reassembly with 33-bit PTS / DTS.
|
||||
--
|
||||
-- local units = MpegTs.demux(buf) -- whole segment: { video = {...}, audio = {...}, streams, ... }
|
||||
-- local d = MpegTs.new(); d:push(chunk); local u = d:pull() ... d:flush() -- streamed
|
||||
--
|
||||
-- Native code must never throw (an error unwinding through native code crashes Roblox Studio), so every read is
|
||||
-- bounds-checked by construction and bad input is skipped and counted in `stats`, never raised.
|
||||
-- author; no third-party code); changed: the unit queue keeps an explicit tail (#q on a table with holes at its front
|
||||
-- loses units under LuaJIT's border rule). Never throws: bad input is skipped and counted in `stats`.
|
||||
local Crc32 = require("@rosetta/core/Crc32")
|
||||
|
||||
local MpegTs = {}
|
||||
@@ -172,7 +164,6 @@ function MpegTs.new(): Demuxer
|
||||
return self
|
||||
end
|
||||
|
||||
-- PSI ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
local function addStream(self: Demuxer, pid: number, streamType: number, program: number, language: string?)
|
||||
local info = CODECS[streamType]
|
||||
@@ -259,7 +250,6 @@ local function sectionBytes(self: Demuxer, pid: number, s: SectionState, src: bu
|
||||
end
|
||||
buffer.copy(s.buf, s.len, src, from, n)
|
||||
s.len += n
|
||||
-- every complete section in the buffer
|
||||
while s.active and s.len >= 3 do
|
||||
if readu8(s.buf, 0) == 0xFF then
|
||||
s.active = false -- stuffing: no more sections until the next unit start
|
||||
@@ -304,7 +294,6 @@ local function sectionPayload(self: Demuxer, pid: number, pusi: boolean, b: buff
|
||||
sectionBytes(self, pid, s, b, pos, stop)
|
||||
end
|
||||
|
||||
-- PES ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
local function completePes(self: Demuxer, pid: number, st: PesState)
|
||||
st.active = false
|
||||
@@ -516,7 +505,6 @@ local function pesPayload(
|
||||
end
|
||||
end
|
||||
|
||||
-- packets -----------------------------------------------------------------------------------------------------
|
||||
|
||||
local function packet(self: Demuxer, b: buffer, pk: number)
|
||||
local stats = self.stats
|
||||
@@ -572,7 +560,6 @@ local function packet(self: Demuxer, b: buffer, pk: number)
|
||||
return -- adaptation field only: the counter does not advance
|
||||
end
|
||||
end
|
||||
-- continuity counter
|
||||
local ccError = false
|
||||
local last = self._cc[pid]
|
||||
if last ~= nil and not discontinuity then
|
||||
@@ -592,8 +579,6 @@ local function packet(self: Demuxer, b: buffer, pk: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- Appends bytes (a buffer, or a string) and demuxes every whole packet in them. Completed units queue up for
|
||||
-- pull(). Returns false for a bad argument.
|
||||
function Demuxer.push(self: Demuxer, data: buffer | string, offset: number?, len: number?): boolean
|
||||
local src: buffer
|
||||
if type(data) == "string" then
|
||||
@@ -612,7 +597,6 @@ function Demuxer.push(self: Demuxer, data: buffer | string, offset: number?, len
|
||||
if n < 0 then
|
||||
return false
|
||||
end
|
||||
-- append to the pending bytes
|
||||
local need = self._pendLen + n
|
||||
if need > buffer.len(self._pend) then
|
||||
local cap = buffer.len(self._pend)
|
||||
@@ -678,7 +662,6 @@ function Demuxer.push(self: Demuxer, data: buffer | string, offset: number?, len
|
||||
return true
|
||||
end
|
||||
|
||||
-- The next completed unit (any PID), or nil.
|
||||
function Demuxer.pull(self: Demuxer): Unit?
|
||||
local q = self._queue
|
||||
local h = self._qhead
|
||||
@@ -697,8 +680,6 @@ function Demuxer.pull(self: Demuxer): Unit?
|
||||
return u
|
||||
end
|
||||
|
||||
-- Ends every unit in progress (call at the end of the data: a video PES with no length only ends at the next
|
||||
-- unit start). Units still queue up for pull().
|
||||
function Demuxer.flush(self: Demuxer)
|
||||
local pids = {}
|
||||
for pid, st in self._pes do
|
||||
@@ -713,7 +694,6 @@ function Demuxer.flush(self: Demuxer)
|
||||
self._pendLen = 0
|
||||
end
|
||||
|
||||
-- Forgets buffered bytes, units in progress and continuity state, but keeps the PAT / PMT (for a seek).
|
||||
function Demuxer.reset(self: Demuxer)
|
||||
self._pendLen = 0
|
||||
for _, st in self._pes do
|
||||
@@ -732,7 +712,6 @@ function Demuxer.reset(self: Demuxer)
|
||||
self._qtail = 0
|
||||
end
|
||||
|
||||
-- Streams by kind: the first video / audio PID in PMT order (or in unit order without a PMT).
|
||||
function Demuxer.streamFor(self: Demuxer, kind: string): Stream?
|
||||
for _, s in self.streams do
|
||||
if s.kind == kind then
|
||||
@@ -744,9 +723,6 @@ end
|
||||
|
||||
export type DemuxOptions = { videoPid: number?, audioPid: number? }
|
||||
|
||||
-- Demuxes a whole buffer (one segment). Video and audio are the units of one PID each: opts.videoPid /
|
||||
-- opts.audioPid, else the first video / audio stream of the PMT, else the first PID whose units look like video /
|
||||
-- audio (PES stream_id). Never throws; a bad argument gives empty lists.
|
||||
function MpegTs.demux(data: buffer | string, opts: DemuxOptions?): Demuxed
|
||||
local d = MpegTs.new()
|
||||
d:push(data)
|
||||
@@ -796,7 +772,6 @@ function MpegTs.demux(data: buffer | string, opts: DemuxOptions?): Demuxed
|
||||
}
|
||||
end
|
||||
|
||||
-- Concatenates the data of a list of units (e.g. a segment's audio access units, for Mp3.open).
|
||||
function MpegTs.concat(units: { Unit }): buffer
|
||||
local n = 0
|
||||
for _, u in units do
|
||||
|
||||
Reference in new issue
Block a user