246 lines
8.2 KiB
Luau
246 lines
8.2 KiB
Luau
--!strict
|
|
-- video/mpegts: MPEG transport streams (ISO/IEC 13818-1: .ts / .m2ts segments, HLS). The demuxer is containers/MpegTs,
|
|
-- absorbed from mpegts-luau (resync, PAT / PMT with CRC, continuity checks, PES reassembly, 33-bit PTS / DTS
|
|
-- unwrapped); this format turns its units into a Demuxer (timescale 90 kHz; one PES = one packet: a picture as
|
|
-- muxed by ffmpeg, one or more audio frames) and returns a VideoDecoder: MPEG-1 video through the codecs/Video
|
|
-- registry (stream type 0x02 checked for an MPEG-2 sequence extension), H.264 when registered, audio (MP1 / MP2 /
|
|
-- MP3, AAC in ADTS) through the api's opts.openAudio hook. Keyframes: MPEG-1 / 2 I-pictures, H.264 IDR access units.
|
|
-- M2TS (192-byte packets with a 4-byte timestamp prefix) is read too. Not supported: writing, scrambled streams,
|
|
-- PES without PTS for video before the first one (dropped).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local MpegTs = require("@formats/containers/MpegTs")
|
|
local MpegEs = require("@formats/containers/MpegEs")
|
|
local Demux = require("@formats/containers/Demux")
|
|
local Video = require("@formats/codecs/Video")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Ts = {
|
|
name = "mpegts",
|
|
category = "video",
|
|
extensions = { "ts", "m2ts", "mts", "tsv" },
|
|
mime = { "video/mp2t" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "containers/MpegTs", "containers/MpegEs", "containers/Demux", "codecs/Video" },
|
|
caps = { decode = true, encode = false, streaming = true, video = { "mpeg1", "h264" }, audio = true },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type OpenOptions = {
|
|
rgba: boolean?,
|
|
track: number?, -- the video PID to decode (default: the first video stream of the PMT)
|
|
audio: boolean?,
|
|
maxPixels: number?,
|
|
openAudio: any?, -- set by the api
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
local CODEC: { [string]: string } = {
|
|
mpeg1video = "mpeg1",
|
|
mpeg2video = "mpeg2",
|
|
mpegaudio = "mp2",
|
|
aac = "aac",
|
|
aac_latm = "aac_latm",
|
|
h264 = "h264",
|
|
hevc = "hevc",
|
|
mpeg4 = "mpeg4",
|
|
ac3 = "ac3",
|
|
eac3 = "eac3",
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
-- packetSize(r) -> 188 | 192 (M2TS) | 0, and the offset of the first sync byte: three sync bytes a packet apart
|
|
-- (fewer in a short file) within the first 2 KB
|
|
local function packetSize(r: Bytes.Reader): (number, number)
|
|
local n = r:remaining()
|
|
for start = 0, math.min(n, 2048) - 1 do
|
|
if r:peekU8(start) == 0x47 then
|
|
for _, size in { 188, 192 } do
|
|
local hits, need = 0, math.min(3, (n - start - 1) // size + 1)
|
|
for k = 0, 2 do
|
|
local at = start + k * size
|
|
if at < n and r:peekU8(at) == 0x47 then
|
|
hits += 1
|
|
end
|
|
end
|
|
if hits >= need and (need >= 2 or start == 0) then
|
|
if size == 192 and start >= 4 then
|
|
return 192, start - 4
|
|
elseif size == 188 then
|
|
return 188, start
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return 0, 0
|
|
end
|
|
|
|
function Ts.sniff(r: Bytes.Reader): number
|
|
local s, off = packetSize(r)
|
|
if s == 0 then
|
|
return 0
|
|
end
|
|
if off > 0 then
|
|
return 0.6
|
|
end
|
|
return if r:remaining() >= 3 * s then 0.9 else 0.5
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- the 188-byte packets of an M2TS file (192-byte packets: drop the 4-byte timestamps)
|
|
local function strip192(b: buffer, base: number, limit: number): buffer
|
|
local n = (limit - base) // 192
|
|
local out = buffer.create(n * 188)
|
|
for i = 0, n - 1 do
|
|
buffer.copy(out, i * 188, b, base + i * 192 + 4, 188)
|
|
end
|
|
return out
|
|
end
|
|
|
|
local function mpegAudioCodec(u: any): string
|
|
local d: buffer = u.data
|
|
for i = 0, math.min(buffer.len(d), 4096) - 2 do
|
|
if buffer.readu8(d, i) == 0xFF and buffer.readu8(d, i + 1) >= 0xE0 then
|
|
local layer = (buffer.readu8(d, i + 1) // 2) % 4
|
|
return if layer == 1 then "mp3" elseif layer == 3 then "mp1" else "mp2"
|
|
end
|
|
end
|
|
return "mp2"
|
|
end
|
|
|
|
-- demux(r, opts?) -> the Demuxer | nil, Err
|
|
function Ts.demux(r: Bytes.Reader, _opts: OpenOptions?): (any, Err.Err?)
|
|
local size, off = packetSize(r)
|
|
if size == 0 then
|
|
return nil, Err.new(Err.CORRUPT, "no transport stream sync", r.base)
|
|
end
|
|
local d = MpegTs.new()
|
|
if size == 192 then
|
|
local b = strip192(r.buf, r.base + off, r.limit)
|
|
d:push(b, 0, buffer.len(b))
|
|
else
|
|
d:push(r.buf, r.base + off, r.limit - r.base - off)
|
|
end
|
|
d:flush()
|
|
local units = {}
|
|
while true do
|
|
local u = d:pull()
|
|
if u == nil then
|
|
break
|
|
end
|
|
table.insert(units, u)
|
|
end
|
|
if #units == 0 then
|
|
return nil, Err.new(Err.CORRUPT, "no PES units")
|
|
end
|
|
-- tracks: the PMT's streams, then PIDs found without one
|
|
local tracks: { Types.Track } = {}
|
|
local byPid: { [number]: any } = {}
|
|
local function track(pid: number, kind: string, codec: string, st: number, lang: string?): any
|
|
local t = byPid[pid]
|
|
if t then
|
|
return t
|
|
end
|
|
t = {
|
|
id = pid,
|
|
kind = if kind == "other" then "data" else kind,
|
|
codec = CODEC[codec] or codec,
|
|
codecTag = string.format("0x%02x", st),
|
|
timescale = 90000,
|
|
language = lang,
|
|
streamType = st,
|
|
}
|
|
byPid[pid] = t
|
|
table.insert(tracks, t)
|
|
return t
|
|
end
|
|
for _, s in d.streams do
|
|
track(s.pid, s.kind, s.codec, s.streamType, s.language)
|
|
end
|
|
for _, u in units do
|
|
track(u.pid, u.kind, u.codec, u.streamType, nil)
|
|
end
|
|
-- codec details from the first unit
|
|
local seen: { [number]: boolean } = {}
|
|
for _, u in units do
|
|
local t = byPid[u.pid]
|
|
if not seen[u.pid] then
|
|
seen[u.pid] = true
|
|
if t.codec == "unknown" or t.kind == "data" then
|
|
-- a private / unknown stream type: go by the PES stream id and the payload
|
|
local sid = u.streamId or 0
|
|
if sid >= 0xE0 and sid <= 0xEF then
|
|
t.kind = "video"
|
|
local info = MpegEs.info(u.data, 0, buffer.len(u.data))
|
|
t.codec = if info then (if info.mpeg2 then "mpeg2" else "mpeg1") elseif MpegEs.findStart(u.data, 0, math.min(buffer.len(u.data), 8)) then "h264" else "unknown"
|
|
elseif sid >= 0xC0 and sid <= 0xDF then
|
|
t.kind, t.codec = "audio", "mp2"
|
|
end
|
|
end
|
|
if t.codec == "mpeg1" or t.codec == "mpeg2" then
|
|
local info = MpegEs.info(u.data, 0, buffer.len(u.data))
|
|
if info then
|
|
t.codec = if info.mpeg2 then "mpeg2" else "mpeg1"
|
|
t.width, t.height, t.frameRate = info.width, info.height, info.frameRate
|
|
end
|
|
elseif t.codec == "mp2" then
|
|
t.codec = mpegAudioCodec(u)
|
|
elseif t.codec == "aac" then
|
|
t.adts = true
|
|
local b: buffer = u.data
|
|
if buffer.len(b) >= 4 and buffer.readu8(b, 0) == 0xFF then
|
|
local rates = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 }
|
|
t.sampleRate = rates[(buffer.readu8(b, 2) // 4) % 16 + 1]
|
|
t.channels = (buffer.readu8(b, 2) % 2) * 4 + buffer.readu8(b, 3) // 64
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local dmx = Demux.new("mpegts", tracks, r.buf, { stats = d.stats })
|
|
local lastPts: { [number]: number } = {}
|
|
for _, u in units do
|
|
local t = byPid[u.pid]
|
|
local pts = u.pts90 or u.dts90
|
|
if pts == nil then
|
|
local prev = lastPts[u.pid]
|
|
if prev and t.frameRate and t.frameRate > 0 then
|
|
pts = prev + 90000 / t.frameRate
|
|
else
|
|
pts = prev
|
|
end
|
|
end
|
|
if pts ~= nil then
|
|
lastPts[u.pid] = pts
|
|
local key = true
|
|
local data: buffer = u.data
|
|
if t.kind == "video" then
|
|
if t.codec == "mpeg1" or t.codec == "mpeg2" then
|
|
key = MpegEs.pictureType(data, 0, buffer.len(data)) == 1
|
|
elseif t.codec == "h264" then
|
|
key = u.randomAccess or MpegEs.h264Key(data, 0, buffer.len(data))
|
|
else
|
|
key = u.randomAccess
|
|
end
|
|
end
|
|
dmx:addData(u.pid, pts, u.dts90 or pts, nil, key, data)
|
|
end
|
|
end
|
|
dmx:finish()
|
|
return dmx, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ open
|
|
-- open(r, opts?) -> a VideoDecoder | nil, Err
|
|
function Ts.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
|
|
local dmx, err = Ts.demux(r, opts)
|
|
if not dmx then
|
|
return nil, err
|
|
end
|
|
return Video.open(dmx, opts)
|
|
end
|
|
|
|
return Ts
|