ws-video: video/mpegts (containers/MpegTs absorbed from mpegts-luau 6e16197, its unit queue fixed for LuaJIT; M2TS, resync before the first packet, private stream types by PES id), video/mpegps (containers/MpegPs: MPEG-1 / MPEG-2 packs, PSM, private stream 1; pictures cut from the joined ES, PTS from PES / GOP temporal references; raw .m1v), containers/MpegEs; tests vs ffprobe / ffmpeg
This commit is contained in:
1 parent
a2763bee35
commit
aa4963dc27
11 files changed
+1852
-15
No files matched your search
@@ -0,0 +1,192 @@
|
||||
--!strict
|
||||
-- video/mpegps: MPEG program streams (ISO/IEC 11172-1 system streams and 13818-1 program streams: .mpg / .mpeg /
|
||||
-- .vob / VCD .dat). New in formats-luau, on containers/MpegPs. Video PES payloads are joined into the elementary
|
||||
-- stream and cut into pictures (containers/MpegEs: one packet per picture with its sequence / GOP headers); a
|
||||
-- picture takes the PTS of the PES it starts in, pictures without one get theirs from the GOP's temporal references.
|
||||
-- Audio packets are the PES payloads (MP1 / MP2 / MP3 frames, AC-3, LPCM). open() returns a VideoDecoder: MPEG-1
|
||||
-- video through the codecs/Video registry (MPEG-2 detected and reported), audio through the api's opts.openAudio hook.
|
||||
-- Timescale 90 kHz. Not supported: writing, H.264 in PS (packets are whole PES payloads), DVD navigation packets.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local MpegPs = require("@formats/containers/MpegPs")
|
||||
local MpegEs = require("@formats/containers/MpegEs")
|
||||
local Demux = require("@formats/containers/Demux")
|
||||
local Video = require("@formats/codecs/Video")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Ps = {
|
||||
name = "mpegps",
|
||||
category = "video",
|
||||
extensions = { "mpg", "mpeg", "vob", "m1v", "mpv", "dat" },
|
||||
mime = { "video/mpeg" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/MpegPs", "containers/MpegEs", "containers/Demux", "codecs/Video" },
|
||||
caps = { decode = true, encode = false, streaming = true, video = { "mpeg1" }, audio = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type OpenOptions = {
|
||||
rgba: boolean?,
|
||||
track: number?, -- the video stream id to decode (0xE0.. ; default: the first video stream)
|
||||
audio: boolean?,
|
||||
maxPixels: number?,
|
||||
openAudio: any?, -- set by the api
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Ps.sniff(r: Bytes.Reader): number
|
||||
if r:peek(4) == "\0\0\1\186" then
|
||||
return 0.9
|
||||
end
|
||||
if r:peek(4) == "\0\0\1\179" then
|
||||
return 0.5 -- an MPEG-1 / 2 video elementary stream (sequence header)
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- the pictures of a video stream as packets: join the PES payloads, cut at pictures, place the timestamps
|
||||
local function videoPackets(dmx: any, buf: buffer, s: MpegPs.Stream, t: any)
|
||||
local total = 0
|
||||
for _, p in s.pes do
|
||||
total += p.size
|
||||
end
|
||||
local es = buffer.create(total)
|
||||
local starts: { number } = {}
|
||||
local o = 0
|
||||
for i, p in s.pes do
|
||||
starts[i] = o
|
||||
buffer.copy(es, o, buf, p.offset, p.size)
|
||||
o += p.size
|
||||
end
|
||||
local info = MpegEs.info(es, 0, total)
|
||||
if info then
|
||||
t.width, t.height, t.frameRate = info.width, info.height, info.frameRate
|
||||
if info.mpeg2 then
|
||||
t.codec = "mpeg2"
|
||||
end
|
||||
end
|
||||
local pics = MpegEs.pictures(es, 0, total)
|
||||
local fd = if t.frameRate and t.frameRate > 0 then 90000 / t.frameRate else 3600
|
||||
-- the PTS of the PES each picture starts in (only the first picture starting in a PES takes it)
|
||||
local k = 1
|
||||
local used: { [number]: boolean } = {}
|
||||
local ptsOf: { [number]: number } = {}
|
||||
for i, pic in pics do
|
||||
while k < #s.pes and starts[k + 1] <= pic.offset do
|
||||
k += 1
|
||||
end
|
||||
-- a PES whose payload starts inside this picture's headers also labels it
|
||||
local pes = s.pes[k]
|
||||
if pes and pes.pts and not used[k] then
|
||||
used[k] = true
|
||||
ptsOf[i] = pes.pts
|
||||
end
|
||||
end
|
||||
-- fill the rest from the temporal references of each GOP (an elementary stream without any PTS: from 0)
|
||||
local anchorPts, anchorTr = nil :: number?, 0
|
||||
local lastPts = nil :: number?
|
||||
local timed = next(ptsOf) ~= nil
|
||||
local count = 0
|
||||
for i, pic in pics do
|
||||
if pic.gop then
|
||||
anchorPts = nil
|
||||
if not timed then
|
||||
anchorPts, anchorTr = count * fd, 0
|
||||
end
|
||||
end
|
||||
if not timed and anchorPts == nil then
|
||||
anchorPts, anchorTr = count * fd, pic.temporal
|
||||
end
|
||||
count += 1
|
||||
local pts = ptsOf[i]
|
||||
if pts then
|
||||
anchorPts, anchorTr = pts, pic.temporal
|
||||
elseif anchorPts then
|
||||
pts = anchorPts + (pic.temporal - anchorTr) * fd
|
||||
elseif lastPts then
|
||||
pts = lastPts + fd
|
||||
end
|
||||
if pts then
|
||||
lastPts = pts
|
||||
local d = buffer.create(pic.size)
|
||||
buffer.copy(d, 0, es, pic.offset, pic.size)
|
||||
dmx:addData(t.id, pts, nil, fd, pic.type == 1, d)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- demux(r, opts?) -> the Demuxer | nil, Err
|
||||
function Ps.demux(r: Bytes.Reader, _opts: OpenOptions?): (any, Err.Err?)
|
||||
if r:peek(4) == "\0\0\1\179" then
|
||||
-- an MPEG-1 / 2 video elementary stream (.m1v): one video stream, no timestamps
|
||||
local t: any = { id = 0xE0, kind = "video", codec = "mpeg1", codecTag = "es", timescale = 90000, streamId = 0xE0 }
|
||||
local dmx = Demux.new("mpegps", { t }, r.buf, { elementary = true })
|
||||
videoPackets(dmx, r.buf, { id = 0xE0, kind = "video", codec = "mpeg1", pes = { { offset = r.base, size = r.limit - r.base } } }, t)
|
||||
dmx:finish()
|
||||
return dmx, nil
|
||||
end
|
||||
local ok, file, err = pcall(MpegPs.read, r.buf, r.base, r.limit)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "internal error: " .. tostring(file))
|
||||
end
|
||||
if not file then
|
||||
return nil, err
|
||||
end
|
||||
local tracks: { Types.Track } = {}
|
||||
for _, id in file.order do
|
||||
local s = file.streams[id]
|
||||
table.insert(tracks, {
|
||||
id = id,
|
||||
kind = s.kind :: any,
|
||||
codec = s.codec,
|
||||
codecTag = string.format("0x%02x", id % 256),
|
||||
timescale = 90000,
|
||||
streamId = id,
|
||||
})
|
||||
end
|
||||
local dmx = Demux.new("mpegps", tracks, r.buf, { mpeg2 = file.mpeg2, errors = file.errors })
|
||||
for i, id in file.order do
|
||||
local s = file.streams[id]
|
||||
local t: any = tracks[i]
|
||||
if t.kind == "video" and (t.codec == "mpeg1" or t.codec == "mpeg2") then
|
||||
videoPackets(dmx, r.buf, s, t)
|
||||
else
|
||||
local last = nil :: number?
|
||||
for _, p in s.pes do
|
||||
local pts = p.pts or last
|
||||
if pts and p.size > 0 then
|
||||
last = pts
|
||||
dmx:add(id, pts, p.dts, nil, true, p.offset, p.size)
|
||||
end
|
||||
end
|
||||
if t.codec == "mp2" and #s.pes > 0 then
|
||||
local p = s.pes[1]
|
||||
local b = r.buf
|
||||
for i2 = p.offset, math.min(p.offset + p.size, r.limit) - 2 do
|
||||
if buffer.readu8(b, i2) == 0xFF and buffer.readu8(b, i2 + 1) >= 0xE0 then
|
||||
local layer = (buffer.readu8(b, i2 + 1) // 2) % 4
|
||||
t.codec = if layer == 1 then "mp3" elseif layer == 3 then "mp1" else "mp2"
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
dmx:finish() -- (read() without a track gives the video packets first, then the audio ones)
|
||||
return dmx, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- open(r, opts?) -> a VideoDecoder | nil, Err
|
||||
function Ps.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
|
||||
local dmx, err = Ps.demux(r, opts)
|
||||
if not dmx then
|
||||
return nil, err
|
||||
end
|
||||
return Video.open(dmx, opts)
|
||||
end
|
||||
|
||||
return Ps
|
||||
@@ -0,0 +1,245 @@
|
||||
--!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
|
||||
Reference in new issue
Block a user