--!strict -- containers/MpegEs: helpers over MPEG video elementary streams for the MPEG-TS / PS demuxers. New in formats-luau. -- Start-code scanning, MPEG-1 / MPEG-2 video access units (a picture with the sequence / GOP headers before it, -- picture type and temporal reference, the sequence's frame rate and size, MPEG-2 detection by its sequence -- extension) and H.264 Annex B keyframes (an IDR slice, or a recovery-point SEI). Interpreted; never throws. local readu8 = buffer.readu8 local MpegEs = {} -- next start code (00 00 01 xx) at or after p, before limit: position of the 00 00 01 | nil function MpegEs.findStart(b: buffer, p: number, limit: number): number? local i = p while i + 3 <= limit do local c = readu8(b, i + 2) if c > 1 then i += 3 elseif c == 1 then if readu8(b, i) == 0 and readu8(b, i + 1) == 0 then return i end i += 3 else -- c == 0: the start code may begin at i + 1 or i + 2 i += 1 end end return nil end local FRAME_RATES = { 24000 / 1001, 24, 25, 30000 / 1001, 30, 50, 60000 / 1001, 60 } export type Picture = { offset: number, -- start of the access unit (its first header's start code) size: number, type: number, -- 1 I, 2 P, 3 B, 4 D (0 unknown) temporal: number, -- temporal_reference gop: boolean, -- a GOP header precedes the picture sequence: boolean, -- a sequence header precedes the picture } export type Info = { width: number, height: number, frameRate: number, mpeg2: boolean } -- info(b, off, limit) -> the first sequence header's facts | nil function MpegEs.info(b: buffer, off: number, limit: number): Info? local p = MpegEs.findStart(b, off, limit) local info: Info? = nil local count = 0 while p and count < 64 do count += 1 local code = if p + 3 < limit then readu8(b, p + 3) else -1 if code == 0xB3 and p + 11 < limit and not info then local w = readu8(b, p + 4) * 16 + readu8(b, p + 5) // 16 local h = (readu8(b, p + 5) % 16) * 256 + readu8(b, p + 6) local fr = readu8(b, p + 7) % 16 info = { width = w, height = h, frameRate = FRAME_RATES[fr] or 0, mpeg2 = false } elseif code == 0xB5 and info then info.mpeg2 = true return info elseif code == 0x00 and info then return info end p = MpegEs.findStart(b, p + 4, limit) end return info end -- pictureType(b, off, limit) -> the type (1 I, 2 P, 3 B) and temporal reference of the first picture header | 0 function MpegEs.pictureType(b: buffer, off: number, limit: number): (number, number) local p = MpegEs.findStart(b, off, limit) local count = 0 while p and count < 32 do count += 1 if p + 5 < limit and readu8(b, p + 3) == 0x00 then local x = readu8(b, p + 4) * 256 + readu8(b, p + 5) return (x // 8) % 8, x // 64 end p = MpegEs.findStart(b, p + 4, limit) end return 0, 0 end -- pictures(b, off, limit) -> access units of an MPEG-1 / 2 video elementary stream: each picture starts at its -- sequence / GOP header when one precedes it and runs to the next unit's start function MpegEs.pictures(b: buffer, off: number, limit: number): { Picture } local out: { Picture } = {} local unitStart: number? = nil local seq, gop = false, false local cur: Picture? = nil local p = MpegEs.findStart(b, off, limit) while p do local code = if p + 3 < limit then readu8(b, p + 3) else -1 if code == 0xB3 or code == 0xB8 or code == 0x00 or code == 0xB7 then -- a sequence / GOP header or a picture after a picture closes it if cur and (code ~= 0xB7) then cur.size = p - cur.offset table.insert(out, cur) cur = nil end if code == 0xB3 then unitStart = unitStart or p seq = true elseif code == 0xB8 then unitStart = unitStart or p gop = true elseif code == 0x00 then local t, tr = 0, 0 if p + 5 < limit then local x = readu8(b, p + 4) * 256 + readu8(b, p + 5) t, tr = (x // 8) % 8, x // 64 end cur = { offset = unitStart or p, size = 0, type = t, temporal = tr, gop = gop, sequence = seq } unitStart, seq, gop = nil, false, false elseif code == 0xB7 and cur then cur.size = p + 4 - cur.offset table.insert(out, cur) cur = nil end end p = MpegEs.findStart(b, p + 4, limit) end if cur then cur.size = limit - cur.offset table.insert(out, cur) end return out end -- h264Key(b, off, limit) -> an Annex B access unit holds an IDR slice (NAL type 5) function MpegEs.h264Key(b: buffer, off: number, limit: number): boolean local p = MpegEs.findStart(b, off, limit) local count = 0 while p and count < 64 do count += 1 if p + 3 < limit then local t = readu8(b, p + 3) % 32 if t == 5 then return true elseif t == 1 then return false end end p = MpegEs.findStart(b, p + 3, limit) end return false end return MpegEs