Files
rosetta/src/Rosetta/containers/MpegEs.luau
T

165 lines
5.0 KiB
Luau

--!strict
-- containers/MpegEs: helpers over MPEG video elementary streams for the MPEG-TS / PS demuxers. New in rosetta.
-- 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 = {}
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
structure: number, -- MPEG-2 picture_structure: 1 top field, 2 bottom field, 3 frame (MPEG-1: 3)
fields: number?, -- 2: a frame coded as two field pictures (both in this unit)
}
export type Info = { width: number, height: number, frameRate: number, mpeg2: boolean }
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
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
function MpegEs.pictureStructure(b: buffer, p: number, limit: number): number
local q = MpegEs.findStart(b, p + 4, limit)
local count = 0
while q and count < 8 do
count += 1
local code = if q + 6 < limit then readu8(b, q + 3) else -1
if code == 0xB5 then
if readu8(b, q + 4) // 16 == 8 then
local st = readu8(b, q + 6) % 4
return if st == 0 then 3 else st
end
elseif code ~= 0xB2 then
break
end
q = MpegEs.findStart(b, q + 4, limit)
end
return 3
end
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
local st = if code == 0x00 then MpegEs.pictureStructure(b, p, limit) else 3
if code == 0x00 and cur and cur.structure ~= 3 and cur.fields == nil and st ~= 3 and st ~= cur.structure then
-- the second field of the current frame: same unit
cur.fields = 2
elseif 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, structure = st } :: Picture
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
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