- codecs/video/Mpeg2/Decoder: coefficient loop keeps the bit position in a local and reads a 25-bit window with one readu32 (no fifth byte); MPEG-1 oddification with bor; dequant shifts; tracks the highest coefficient so the IDCT skips the zero rows; toRgba / toRgba422 through shifted-channel lookup tables (one rshift + one index per channel, the alpha folded in), two rows per chroma row without a per-pixel branch - codecs/video/Dsp: idct(blk, dst, off, stride, add, last?): pass 1 only over the rows up to last's, pass 2 with the rows-4..7-zero branch tree when they are, fused with the put / add (rounding and the CLAMP offset in one division); mc: an unrolled whole-sample copy for 8 / 16 wide blocks, the 4-tap case a column at a time reusing each row's pair sums - codecs/image/Yuv.toRgba: per-matrix luma table, shifted-channel clip tables, chroma once per 2x2 - codecs/video/H264: prediction scratch as buffers (put = u32 copy, bi-pred average four samples at a time), whole-sample single-list blocks copied straight from the reference, implicit / identity weights routed to the plain paths, 2-tap chroma when a fraction is 0 and the bilinear taps slide along the row, six-tap with two multiplies; deblocking skips the inner edges of macroblocks with one motion and no luma coefficients (S.flatTab) and finds equal motion before motionDiffers - codecs/image/Vp8/Predict: six-tap filters with a sliding window (one read per sample) - --!optimize 2 on every video codec / container module that lacked it (the bundles already force it) - tools/bench_video.luau: the video fps lines on their own (sections, "once", "jitdefault"), more MPEG-1 clips
313 lines
6.9 KiB
Luau
313 lines
6.9 KiB
Luau
--!strict
|
|
--!optimize 2
|
|
-- containers/Ebml: EBML (RFC 8794), the binary structure under Matroska and WebM. New in rosetta.
|
|
-- Reading: element headers (IDs keep their length marker bits, as the specs write them: 0x1A45DFA3; sizes drop it;
|
|
-- an all-ones size is "unknown"), children iteration, and the value types (unsigned / signed integers, floats,
|
|
-- strings, binary). Writing: a byte builder with nested master elements whose 8-byte sizes are patched on close, and
|
|
-- minimal-length value elements.
|
|
-- Never throws: malformed input gives nil (readers) or stops iteration.
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
|
|
local readu8 = buffer.readu8
|
|
|
|
local Ebml = {}
|
|
|
|
Ebml.UNKNOWN = -1
|
|
|
|
function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?)
|
|
if pos >= limit then
|
|
return nil, nil
|
|
end
|
|
local b = readu8(buf, pos)
|
|
if b == 0 then
|
|
return nil, nil
|
|
end
|
|
local len = 1
|
|
local mask = 0x80
|
|
while bit32.band(b, mask) == 0 do
|
|
len += 1
|
|
mask = mask // 2
|
|
end
|
|
if pos + len > limit then
|
|
return nil, nil
|
|
end
|
|
local v = bit32.band(b, mask - 1)
|
|
local allOnes = v == mask - 1
|
|
for i = 1, len - 1 do
|
|
local c = readu8(buf, pos + i)
|
|
v = v * 256 + c
|
|
if c ~= 255 then
|
|
allOnes = false
|
|
end
|
|
end
|
|
if allOnes then
|
|
return -1, len
|
|
end
|
|
return v, len
|
|
end
|
|
|
|
function Ebml.svint(buf: buffer, pos: number, limit: number): (number?, number?)
|
|
local v, len = Ebml.vint(buf, pos, limit)
|
|
if v == nil or len == nil then
|
|
return nil, nil
|
|
end
|
|
if v == -1 then
|
|
return nil, nil
|
|
end
|
|
return v - (2 ^ (7 * len - 1) - 1), len
|
|
end
|
|
|
|
function Ebml.id(buf: buffer, pos: number, limit: number): (number?, number?)
|
|
if pos >= limit then
|
|
return nil, nil
|
|
end
|
|
local b = readu8(buf, pos)
|
|
local len = if b >= 0x80 then 1 elseif b >= 0x40 then 2 elseif b >= 0x20 then 3 elseif b >= 0x10 then 4 else 0
|
|
if len == 0 or pos + len > limit then
|
|
return nil, nil
|
|
end
|
|
local v = b
|
|
for i = 1, len - 1 do
|
|
v = v * 256 + readu8(buf, pos + i)
|
|
end
|
|
return v, len
|
|
end
|
|
|
|
function Ebml.header(buf: buffer, pos: number, limit: number): (number?, number?, number?)
|
|
local id, il = Ebml.id(buf, pos, limit)
|
|
if not id or not il then
|
|
return nil, nil, nil
|
|
end
|
|
local size, sl = Ebml.vint(buf, pos + il, limit)
|
|
if not size or not sl then
|
|
return nil, nil, nil
|
|
end
|
|
return id, size, pos + il + sl
|
|
end
|
|
|
|
function Ebml.children(buf: buffer, off: number, limit: number): () -> (number?, number, number)
|
|
local pos = off
|
|
return function(): (number?, number, number)
|
|
if pos >= limit then
|
|
return nil, 0, 0
|
|
end
|
|
local id, size, data = Ebml.header(buf, pos, limit)
|
|
if not id or not size or not data then
|
|
pos = limit
|
|
return nil, 0, 0
|
|
end
|
|
if size < 0 or data + size > limit then
|
|
size = limit - data
|
|
end
|
|
pos = data + size
|
|
return id, data, size
|
|
end
|
|
end
|
|
|
|
function Ebml.uint(buf: buffer, off: number, len: number): number
|
|
local v = 0
|
|
for i = 0, math.min(len, 8) - 1 do
|
|
v = v * 256 + readu8(buf, off + i)
|
|
end
|
|
return v
|
|
end
|
|
function Ebml.int(buf: buffer, off: number, len: number): number
|
|
if len <= 0 then
|
|
return 0
|
|
end
|
|
local v = Ebml.uint(buf, off, len)
|
|
local n = math.min(len, 8)
|
|
if readu8(buf, off) >= 0x80 then
|
|
v -= 2 ^ (8 * n)
|
|
end
|
|
return v
|
|
end
|
|
function Ebml.float(buf: buffer, off: number, len: number): number
|
|
local r = Bytes.reader(buf, off, len)
|
|
if len == 4 then
|
|
return r:f32be()
|
|
elseif len == 8 then
|
|
return r:f64be()
|
|
end
|
|
return 0
|
|
end
|
|
function Ebml.string(buf: buffer, off: number, len: number): string
|
|
local s = buffer.readstring(buf, off, len)
|
|
local z = string.find(s, "\0", 1, true)
|
|
if z then
|
|
s = string.sub(s, 1, z - 1)
|
|
end
|
|
return s
|
|
end
|
|
function Ebml.binary(buf: buffer, off: number, len: number): buffer
|
|
local b = buffer.create(len)
|
|
buffer.copy(b, 0, buf, off, len)
|
|
return b
|
|
end
|
|
|
|
local W = {}
|
|
W.__index = W
|
|
|
|
export type Writer = typeof(setmetatable({} :: { w: Bytes.Writer }, W))
|
|
|
|
function Ebml.writer(capacity: number?): Writer
|
|
return setmetatable({ w = Bytes.writer(capacity or 65536) }, W)
|
|
end
|
|
|
|
local function writeId(w: Bytes.Writer, id: number)
|
|
if id >= 0x1000000 then
|
|
w:u32be(id)
|
|
elseif id >= 0x10000 then
|
|
w:u24be(id)
|
|
elseif id >= 0x100 then
|
|
w:u16be(id)
|
|
else
|
|
w:u8(id)
|
|
end
|
|
end
|
|
|
|
local function writeSize(w: Bytes.Writer, size: number, n: number?)
|
|
local len = n or 1
|
|
if not n then
|
|
while size >= 2 ^ (7 * len) - 1 do
|
|
len += 1
|
|
end
|
|
end
|
|
local v = size + 2 ^ (7 * len) -- the marker bit
|
|
local bytes = table.create(len, 0)
|
|
for i = len, 1, -1 do
|
|
bytes[i] = v % 256
|
|
v = v // 256
|
|
end
|
|
for i = 1, len do
|
|
w:u8(bytes[i])
|
|
end
|
|
end
|
|
Ebml.writeSize = writeSize
|
|
|
|
function Ebml.vintString(v: number, n: number?): string
|
|
local w = Bytes.writer(8)
|
|
writeSize(w, v, n)
|
|
return buffer.readstring(w:finish(), 0, w:tell())
|
|
end
|
|
|
|
function W.tell(self: Writer): number
|
|
return self.w:tell()
|
|
end
|
|
|
|
function W.open(self: Writer, id: number): number
|
|
writeId(self.w, id)
|
|
local at = self.w:tell()
|
|
for _ = 1, 8 do
|
|
self.w:u8(0)
|
|
end
|
|
return at
|
|
end
|
|
|
|
function W.close(self: Writer, at: number)
|
|
local w = self.w
|
|
local v = w:tell() - at - 8
|
|
w:setU8(at, 0x01)
|
|
for i = 7, 1, -1 do
|
|
w:setU8(at + i, v % 256)
|
|
v = v // 256
|
|
end
|
|
end
|
|
|
|
function W.openUnknown(self: Writer, id: number)
|
|
writeId(self.w, id)
|
|
self.w:u8(0x01)
|
|
for _ = 1, 7 do
|
|
self.w:u8(0xFF)
|
|
end
|
|
end
|
|
|
|
function W.uint(self: Writer, id: number, v: number)
|
|
local n = 1
|
|
while v >= 2 ^ (8 * n) and n < 8 do
|
|
n += 1
|
|
end
|
|
writeId(self.w, id)
|
|
writeSize(self.w, n)
|
|
local bytes = table.create(n, 0)
|
|
local x = v
|
|
for i = n, 1, -1 do
|
|
bytes[i] = x % 256
|
|
x = x // 256
|
|
end
|
|
for i = 1, n do
|
|
self.w:u8(bytes[i])
|
|
end
|
|
end
|
|
|
|
function W.int(self: Writer, id: number, v: number)
|
|
local n = 1
|
|
while (v < -(2 ^ (8 * n - 1)) or v >= 2 ^ (8 * n - 1)) and n < 8 do
|
|
n += 1
|
|
end
|
|
local u = if v < 0 then v + 2 ^ (8 * n) else v
|
|
writeId(self.w, id)
|
|
writeSize(self.w, n)
|
|
local bytes = table.create(n, 0)
|
|
for i = n, 1, -1 do
|
|
bytes[i] = u % 256
|
|
u = u // 256
|
|
end
|
|
for i = 1, n do
|
|
self.w:u8(bytes[i])
|
|
end
|
|
end
|
|
|
|
function W.float(self: Writer, id: number, v: number)
|
|
writeId(self.w, id)
|
|
writeSize(self.w, 8)
|
|
self.w:f64be(v)
|
|
end
|
|
|
|
function W.string(self: Writer, id: number, s: string)
|
|
writeId(self.w, id)
|
|
writeSize(self.w, #s)
|
|
self.w:string(s)
|
|
end
|
|
|
|
function W.binary(self: Writer, id: number, b: buffer, off: number?, len: number?)
|
|
local o = off or 0
|
|
local n = len or (buffer.len(b) - o)
|
|
writeId(self.w, id)
|
|
writeSize(self.w, n)
|
|
self.w:bytes(b, o, n)
|
|
end
|
|
|
|
function W.raw(self: Writer, s: string)
|
|
self.w:string(s)
|
|
end
|
|
function W.rawBuffer(self: Writer, b: buffer, off: number?, len: number?)
|
|
local o = off or 0
|
|
self.w:bytes(b, o, len or (buffer.len(b) - o))
|
|
end
|
|
function W.header(self: Writer, id: number, size: number)
|
|
writeId(self.w, id)
|
|
writeSize(self.w, size)
|
|
end
|
|
|
|
function W.void(self: Writer, n: number)
|
|
self.w:u8(0xEC)
|
|
if n - 2 < 127 then
|
|
writeSize(self.w, n - 2, 1)
|
|
for _ = 1, n - 2 do
|
|
self.w:u8(0)
|
|
end
|
|
else
|
|
writeSize(self.w, n - 9, 8)
|
|
for _ = 1, n - 9 do
|
|
self.w:u8(0)
|
|
end
|
|
end
|
|
end
|
|
|
|
function W.finish(self: Writer): buffer
|
|
return self.w:finish()
|
|
end
|
|
|
|
return Ebml
|