--!strict -- containers/MpegPs: the MPEG program stream (ISO/IEC 11172-1 system streams and ISO/IEC 13818-1 program streams: -- .mpg / .mpeg / .vob). New in formats-luau. Pack headers (MPEG-1 and MPEG-2 layouts), system headers, the program -- stream map (stream types), padding, and PES packets of the video (0xE0..0xEF), audio (0xC0..0xDF) and private -- stream 1 (0xBD: AC-3 / DTS / LPCM / subpicture sub-streams) ids with their PTS / DTS (both PES header layouts, -- 33-bit timestamps unwrapped). Data between packs that is not a start code is skipped (resync). -- -- local ps, err = MpegPs.read(buf, base, limit) -- ps.streams[id] = { id, kind, codec, streamType, pes = { { pts?, dts?, offset, size } } } (id: stream_id, or -- 0xBD00 + sub-stream id for private stream 1); ps.order = the ids in order of appearance -- -- Never throws: malformed packets are skipped and counted in ps.errors. local Err = require("@formats/core/Err") local MpegEs = require("@formats/containers/MpegEs") local readu8 = buffer.readu8 local MpegPs = {} export type Pes = { pts: number?, dts: number?, offset: number, size: number } export type Stream = { id: number, kind: string, codec: string, streamType: number?, pes: { Pes } } export type File = { streams: { [number]: Stream }, order: { number }, mpeg2: boolean, errors: number } local WRAP = 8589934592 -- 2^33 -- PSM stream_type -> kind, codec local TYPES: { [number]: { string } } = { [0x01] = { "video", "mpeg1" }, [0x02] = { "video", "mpeg2" }, [0x03] = { "audio", "mp2" }, [0x04] = { "audio", "mp2" }, [0x0F] = { "audio", "aac" }, [0x10] = { "video", "mpeg4" }, [0x1B] = { "video", "h264" }, [0x24] = { "video", "hevc" }, [0x81] = { "audio", "ac3" }, } local function ts(b: buffer, p: number): number local a = readu8(b, p) local v = (a // 2) % 8 v = v * 32768 + (readu8(b, p + 1) * 256 + readu8(b, p + 2)) // 2 v = v * 32768 + (readu8(b, p + 3) * 256 + readu8(b, p + 4)) // 2 return v end -- read(buf, base, limit) -> File | nil, Err function MpegPs.read(b: buffer, base: number, limit: number): (File?, Err.Err?) local file: File = { streams = {}, order = {}, mpeg2 = false, errors = 0 } local types: { [number]: number } = {} local last: { [number]: number } = {} local wrap: { [number]: number } = {} local packs = 0 local p = MpegEs.findStart(b, base, limit) local function unwrap(id: number, v: number): number local off = wrap[id] or 0 local prev = last[id] local t = v + off if prev and t < prev - WRAP / 2 then off += WRAP wrap[id] = off t = v + off end last[id] = t return t end while p do if p + 4 > limit then break end local code = readu8(b, p + 3) local nextP = p + 4 if code == 0xBA then packs += 1 if p + 5 > limit then break end local m = readu8(b, p + 4) if m // 64 == 1 then -- MPEG-2 pack: 14 bytes + stuffing file.mpeg2 = true if p + 14 > limit then break end nextP = p + 14 + readu8(b, p + 13) % 8 else nextP = p + 12 end elseif code == 0xB9 then nextP = p + 4 elseif code >= 0xBB then if p + 6 > limit then break end local len = readu8(b, p + 4) * 256 + readu8(b, p + 5) local stop = p + 6 + len if stop > limit then stop = limit end nextP = stop if code == 0xBC and len >= 10 then -- program stream map local infoLen = readu8(b, p + 8) * 256 + readu8(b, p + 9) local q = p + 10 + infoLen if q + 2 <= stop then local mapLen = readu8(b, q) * 256 + readu8(b, q + 1) q += 2 local e = math.min(stop, q + mapLen) while q + 4 <= e do local st, sid = readu8(b, q), readu8(b, q + 1) local il = readu8(b, q + 2) * 256 + readu8(b, q + 3) types[sid] = st q += 4 + il end end elseif code == 0xBD or (code >= 0xC0 and code <= 0xEF) then -- a PES packet local q = p + 6 local pts, dts: number? = nil, nil local ok = true if q < stop and readu8(b, q) // 64 == 2 then -- MPEG-2 PES header if q + 3 > stop then ok = false else local flags = readu8(b, q + 1) local hlen = readu8(b, q + 2) local h = q + 3 if flags >= 0x80 and h + 5 <= stop then pts = ts(b, h) if flags // 64 == 3 and h + 10 <= stop then dts = ts(b, h + 5) end end q = h + hlen end else -- MPEG-1 packet header local n = 0 while q < stop and readu8(b, q) == 0xFF and n < 16 do q += 1 n += 1 end if q < stop and readu8(b, q) // 64 == 1 then q += 2 end if q < stop then local c = readu8(b, q) // 16 if c == 2 and q + 5 <= stop then pts = ts(b, q) q += 5 elseif c == 3 and q + 10 <= stop then pts = ts(b, q) dts = ts(b, q + 5) q += 10 elseif readu8(b, q) == 0x0F then q += 1 else ok = false end end end if ok and q <= stop then local id = code local kind, codec = "other", "unknown" if code == 0xBD then if q >= stop then ok = false else local sub = readu8(b, q) id = 0xBD00 + sub if sub >= 0x80 and sub <= 0x87 then kind, codec = "audio", "ac3" q += 4 elseif sub >= 0x88 and sub <= 0x8F then kind, codec = "audio", "dts" q += 4 elseif sub >= 0xA0 and sub <= 0xA7 then kind, codec = "audio", "pcm_dvd" q += 7 elseif sub >= 0x20 and sub <= 0x3F then kind, codec = "subtitle", "dvdsub" q += 1 else q += 1 end end elseif code >= 0xE0 then kind, codec = "video", "mpeg1" else kind, codec = "audio", "mp2" end local st = types[code] if st and TYPES[st] then kind, codec = TYPES[st][1], TYPES[st][2] end if ok and q <= stop then local s = file.streams[id] if not s then s = { id = id, kind = kind, codec = codec, streamType = st, pes = {} } file.streams[id] = s table.insert(file.order, id) end table.insert(s.pes, { pts = if pts then unwrap(id, pts) else nil, dts = if dts then unwrap(id + 0x10000, dts) else nil, offset = q, size = stop - q, }) end end if not ok then file.errors += 1 end end else -- a start code that is not a system one (stray ES data between packets): resync file.errors += 1 end p = MpegEs.findStart(b, nextP, limit) end if packs == 0 and #file.order == 0 then return nil, Err.new(Err.CORRUPT, "no MPEG pack or PES packet", base) end return file, nil end return MpegPs