--!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, } :: Types.Track) 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 :: Video.OpenOptions?) end return Ps