merge ws/perf-video

This commit is contained in:
4DBug committed 2026-11-01 21:22:20 -06:00
commit 627ddded77
38 files changed
+3059 -1599

No files matched your search

+18 -14
View File
@@ -26,10 +26,12 @@ local function sixH(src: buffer, so: number, ss: number, dst: buffer, d: number,
local k = f * 6
local t0, t1, t2, t3, t4, t5 = SIX[k + 1], SIX[k + 2], SIX[k + 3], SIX[k + 4], SIX[k + 5], SIX[k + 6]
for _ = 1, h do
for i = so - 2, so + w - 3 do
local v = readu8(src, i) * t0 + readu8(src, i + 1) * t1 + readu8(src, i + 2) * t2 + readu8(src, i + 3) * t3
+ readu8(src, i + 4) * t4 + readu8(src, i + 5) * t5 + 64
writeu8(dst, d + i - so + 2, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
local a0, a1, a2, a3, a4 = readu8(src, so - 2), readu8(src, so - 1), readu8(src, so), readu8(src, so + 1), readu8(src, so + 2)
for i = 0, w - 1 do
local a5 = readu8(src, so + i + 3)
local v = a0 * t0 + a1 * t1 + a2 * t2 + a3 * t3 + a4 * t4 + a5 * t5 + 64
writeu8(dst, d + i, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
end
so += ss
d += ds
@@ -39,17 +41,19 @@ end
local function sixV(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
local k = f * 6
local t0, t1, t2, t3, t4, t5 = SIX[k + 1], SIX[k + 2], SIX[k + 3], SIX[k + 4], SIX[k + 5], SIX[k + 6]
local s1, s2, s3 = ss, 2 * ss, 3 * ss
for _ = 1, h do
local p = so - s2
for i = 0, w - 1 do
local q = p + i
local v = readu8(src, q) * t0 + readu8(src, q + s1) * t1 + readu8(src, q + s2) * t2 + readu8(src, q + s3) * t3
+ readu8(src, q + s2 + s2) * t4 + readu8(src, q + s2 + s3) * t5 + 64
writeu8(dst, d + i, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
for i = 0, w - 1 do
local q = so + i
local a0, a1, a2, a3, a4 = readu8(src, q - 2 * ss), readu8(src, q - ss), readu8(src, q), readu8(src, q + ss), readu8(src, q + 2 * ss)
q += 3 * ss
local o = d + i
for _ = 1, h do
local a5 = readu8(src, q)
local v = a0 * t0 + a1 * t1 + a2 * t2 + a3 * t3 + a4 * t4 + a5 * t5 + 64
writeu8(dst, o, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
q += ss
o += ds
end
so += ss
d += ds
end
end
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/image/Vp8/Tables: the VP8 tables of RFC 6386 (sections 11.5, 13.4, 13.5, 14.1, 16-18), from libwebp 1.6 src/dec
-- (BSD-3-Clause, licenses/libwebp-LICENSE.txt) and libvpx 1.17 vp8/common (BSD-3-Clause, licenses/libvpx-LICENSE.txt).
-- Flat 1-based arrays.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/image/Vp8: the VP8 video codec (RFC 6386), decode only.
-- Key frames ported from libwebp 1.6 (BSD-3-Clause, licenses/libwebp-LICENSE.txt), inter frames after libvpx 1.17
-- (BSD-3-Clause, licenses/libvpx-LICENSE.txt); bit-exact with both. Scale bits are ignored, as libwebp and ffmpeg do.
+51 -14
View File
@@ -19,7 +19,7 @@ local MATRIX: { [string]: { kr: number, kb: number } } = {
bt709 = { kr = 0.2126, kb = 0.0722 },
}
type Coeffs = { ys: number, yo: number, crv: number, cgu: number, cgv: number, cbu: number, cs: number }
type Coeffs = { ys: number, yo: number, crv: number, cgu: number, cgv: number, cbu: number, cs: number, yt: { number } }
local cache: { [string]: Coeffs } = {}
local function coeffs(range: string?, matrix: string?): Coeffs
@@ -42,7 +42,11 @@ local function coeffs(range: string?, matrix: string?): Coeffs
cgu = floor(2 * (1 - kb) * kb / kg * cs * 65536 + 0.5),
cgv = floor(2 * (1 - kr) * kr / kg * cs * 65536 + 0.5),
cs = cs,
yt = table.create(256, 0),
}
for v = 0, 255 do
c.yt[v + 1] = (c.ys * (v - c.yo) + 32768) // 65536 + 1024
end
cache[key] = c
return c
end
@@ -51,34 +55,67 @@ local CLIP = bcreate(2304)
for i = 0, 2303 do
writeu8(CLIP, i, math.clamp(i - 1024, 0, 255))
end
-- CLIP's values shifted into their rgba8 byte (CB carries the alpha); indexes stay within 600..1700
local CR: { number } = table.create(2304, 0)
local CG: { number } = table.create(2304, 0)
local CB: { number } = table.create(2304, 0)
for i = 1, 2304 do
local v = math.clamp(i - 1024, 0, 255)
CR[i] = v
CG[i] = v * 256
CB[i] = v * 65536 + 0xff000000
end
function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, cw: number, range: string?, matrix: string?, out: buffer?): buffer
local dst = out or bcreate(w * h * 4)
local c = coeffs(range, matrix)
local ys, yo, crv, cgu, cgv, cbu = c.ys, c.yo, c.crv, c.cgu, c.cgv, c.cbu
local crv, cgu, cgv, cbu = c.crv, c.cgu, c.cgv, c.cbu
local yt, cr, cg, cb = c.yt, CR, CG, CB
local ch = (h + 1) // 2
if buffer.len(src) < vo + cw * ch or buffer.len(dst) < w * h * 4 or uo + cw * ch > buffer.len(src) then
return dst
end
for y = 0, h - 1 do
local w2 = w // 2
local stride = w * 4
for y = 0, h - 1, 2 do
local yrow = y * w
local crow = (y // 2) * cw
local ci = uo + (y // 2) * cw
local dv = vo - uo
local o = yrow * 4
for x = 0, w - 1, 2 do
local ci = crow + (x // 2)
local u = readu8(src, uo + ci) - 128
local v = readu8(src, vo + ci) - 128
local two = y + 1 < h
for _ = 1, w2 do
local u = readu8(src, ci) - 128
local v = readu8(src, ci + dv) - 128
ci += 1
local rv = (crv * v + 32768) // 65536
local gv = (cgu * u + cgv * v + 32768) // 65536
local gv = -((cgu * u + cgv * v + 32768) // 65536)
local bu = (cbu * u + 32768) // 65536
local yy = (ys * (readu8(src, yrow + x) - yo) + 32768) // 65536 + 1024
writeu32(dst, o, readu8(CLIP, yy + rv) + readu8(CLIP, yy - gv) * 256 + readu8(CLIP, yy + bu) * 65536 + 0xff000000)
if x + 1 < w then
yy = (ys * (readu8(src, yrow + x + 1) - yo) + 32768) // 65536 + 1024
writeu32(dst, o + 4, readu8(CLIP, yy + rv) + readu8(CLIP, yy - gv) * 256 + readu8(CLIP, yy + bu) * 65536 + 0xff000000)
local yy = yt[readu8(src, yrow) + 1]
writeu32(dst, o, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
yy = yt[readu8(src, yrow + 1) + 1]
writeu32(dst, o + 4, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
if two then
yy = yt[readu8(src, yrow + w) + 1]
writeu32(dst, o + stride, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
yy = yt[readu8(src, yrow + w + 1) + 1]
writeu32(dst, o + stride + 4, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
end
yrow += 2
o += 8
end
if w % 2 == 1 then
local u = readu8(src, ci) - 128
local v = readu8(src, ci + dv) - 128
local rv = (crv * v + 32768) // 65536
local gv = -((cgu * u + cgv * v + 32768) // 65536)
local bu = (cbu * u + 32768) // 65536
local yy = yt[readu8(src, yrow) + 1]
writeu32(dst, o, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
if two then
yy = yt[readu8(src, yrow + w) + 1]
writeu32(dst, o + stride, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
end
end
end
return dst
end
+155 -40
View File
@@ -27,13 +27,17 @@ for i = 0, 2 * COFF + 255 do
end
Dsp.CLAMP = CLAMP
Dsp.COFF = COFF
local RND2 = 131072 + COFF * 262144
function Dsp.idct(blk: buffer, dst: buffer, off: number, stride: number, add: boolean)
-- last: the highest natural index that may be nonzero (default 63); the rows below its row are zero and skipped
-- (pass 2 is the same arithmetic with those inputs 0). blk keeps pass 1's rows (the caller clears them).
function Dsp.idct(blk: buffer, dst: buffer, off: number, stride: number, add: boolean, last: number?)
local tmp0, tmp1, tmp2, tmp3 = 0, 0, 0, 0
local tmp10, tmp11, tmp12, tmp13 = 0, 0, 0, 0
local z1, z2, z3, z4, z5 = 0, 0, 0, 0, 0
local rows = if last then last // 8 + 1 else 8
-- pass 1: rows, scaled up by 2^PASS1_BITS
for r = 0, 224, 32 do
for r = 0, rows * 32 - 32, 32 do
local d0 = readi32(blk, r)
local d1 = readi32(blk, r + 4)
local d2 = readi32(blk, r + 8)
@@ -319,7 +323,91 @@ function Dsp.idct(blk: buffer, dst: buffer, off: number, stride: number, add: bo
writei32(blk, r + 16, (tmp13 - tmp0 + 1024) // 2048)
end
end
-- pass 2: columns, descaled by 2^(CONST_BITS + PASS1_BITS + 3)
-- pass 2: columns, descaled by 2^(CONST_BITS + PASS1_BITS + 3), then put / add, clamped (RND2: the rounding plus
-- COFF, the CLAMP offset, in the same division)
local s1, s2, s3 = stride, 2 * stride, 3 * stride
local s4, s5, s6, s7 = 4 * stride, 5 * stride, 6 * stride, 7 * stride
if rows <= 4 then
-- rows 4..7 are zero: the full tree's d4 = d5 = d6 = d7 = 0 branches
for c = 0, 28, 4 do
local d0 = readi32(blk, c)
local d1 = readi32(blk, c + 32)
local d2 = readi32(blk, c + 64)
local d3 = readi32(blk, c + 96)
if d2 ~= 0 then
tmp2 = (d2) * 4433
tmp3 = (d2) * 10703
tmp0 = (d0) * 8192
tmp10 = tmp0 + tmp3
tmp13 = tmp0 - tmp3
tmp11 = tmp0 + tmp2
tmp12 = tmp0 - tmp2
else
tmp13 = (d0) * 8192
tmp10 = tmp13
tmp12 = tmp13
tmp11 = tmp13
end
if d3 ~= 0 then
if d1 ~= 0 then
z5 = d1 + d3
tmp3 = (d1) * 1730
tmp2 = (-d3) * 11893
z1 = (d1) * 8697
z2 = (-d3) * 17799
z4 = (z5) * 6436
z5 = (z5) * 9633
tmp0 = z1 - z4
tmp1 = z2 + z4
tmp2 += z5
tmp3 += z5
else
tmp0 = (-d3) * 6436
tmp1 = (-d3) * 11363
tmp2 = (-d3) * 2260
tmp3 = (d3) * 9633
end
elseif d1 ~= 0 then
tmp0 = (d1) * 2260
tmp1 = (d1) * 6436
tmp2 = (d1) * 9633
tmp3 = (d1) * 11363
else
tmp3 = 0
tmp0 = tmp3
tmp1 = tmp3
tmp2 = tmp3
end
local o = off + c // 4
local v0 = (tmp10 + tmp3 + RND2) // 262144
local v7 = (tmp10 - tmp3 + RND2) // 262144
local v1 = (tmp11 + tmp2 + RND2) // 262144
local v6 = (tmp11 - tmp2 + RND2) // 262144
local v2 = (tmp12 + tmp1 + RND2) // 262144
local v5 = (tmp12 - tmp1 + RND2) // 262144
local v3 = (tmp13 + tmp0 + RND2) // 262144
local v4 = (tmp13 - tmp0 + RND2) // 262144
if add then
v0 += readu8(dst, o)
v1 += readu8(dst, o + s1)
v2 += readu8(dst, o + s2)
v3 += readu8(dst, o + s3)
v4 += readu8(dst, o + s4)
v5 += readu8(dst, o + s5)
v6 += readu8(dst, o + s6)
v7 += readu8(dst, o + s7)
end
writeu8(dst, o, readu8(CLAMP, v0))
writeu8(dst, o + s1, readu8(CLAMP, v1))
writeu8(dst, o + s2, readu8(CLAMP, v2))
writeu8(dst, o + s3, readu8(CLAMP, v3))
writeu8(dst, o + s4, readu8(CLAMP, v4))
writeu8(dst, o + s5, readu8(CLAMP, v5))
writeu8(dst, o + s6, readu8(CLAMP, v6))
writeu8(dst, o + s7, readu8(CLAMP, v7))
end
return
end
for c = 0, 28, 4 do
local d0 = readi32(blk, c)
local d1 = readi32(blk, c + 32)
@@ -584,38 +672,33 @@ function Dsp.idct(blk: buffer, dst: buffer, off: number, stride: number, add: bo
end
end
end
writei32(blk, c, (tmp10 + tmp3 + 131072) // 262144)
writei32(blk, c + 224, (tmp10 - tmp3 + 131072) // 262144)
writei32(blk, c + 32, (tmp11 + tmp2 + 131072) // 262144)
writei32(blk, c + 192, (tmp11 - tmp2 + 131072) // 262144)
writei32(blk, c + 64, (tmp12 + tmp1 + 131072) // 262144)
writei32(blk, c + 160, (tmp12 - tmp1 + 131072) // 262144)
writei32(blk, c + 96, (tmp13 + tmp0 + 131072) // 262144)
writei32(blk, c + 128, (tmp13 - tmp0 + 131072) // 262144)
end
-- put / add, clamped
for r = 0, 224, 32 do
local p0 = readi32(blk, r) + COFF
local p1 = readi32(blk, r + 4) + COFF
local p2 = readi32(blk, r + 8) + COFF
local p3 = readi32(blk, r + 12) + COFF
local p4 = readi32(blk, r + 16) + COFF
local p5 = readi32(blk, r + 20) + COFF
local p6 = readi32(blk, r + 24) + COFF
local p7 = readi32(blk, r + 28) + COFF
local o = off + c // 4
local v0 = (tmp10 + tmp3 + RND2) // 262144
local v7 = (tmp10 - tmp3 + RND2) // 262144
local v1 = (tmp11 + tmp2 + RND2) // 262144
local v6 = (tmp11 - tmp2 + RND2) // 262144
local v2 = (tmp12 + tmp1 + RND2) // 262144
local v5 = (tmp12 - tmp1 + RND2) // 262144
local v3 = (tmp13 + tmp0 + RND2) // 262144
local v4 = (tmp13 - tmp0 + RND2) // 262144
if add then
p0 += readu8(dst, off)
p1 += readu8(dst, off + 1)
p2 += readu8(dst, off + 2)
p3 += readu8(dst, off + 3)
p4 += readu8(dst, off + 4)
p5 += readu8(dst, off + 5)
p6 += readu8(dst, off + 6)
p7 += readu8(dst, off + 7)
v0 += readu8(dst, o)
v1 += readu8(dst, o + s1)
v2 += readu8(dst, o + s2)
v3 += readu8(dst, o + s3)
v4 += readu8(dst, o + s4)
v5 += readu8(dst, o + s5)
v6 += readu8(dst, o + s6)
v7 += readu8(dst, o + s7)
end
writeu32(dst, off, readu8(CLAMP, p0) + readu8(CLAMP, p1) * 256 + readu8(CLAMP, p2) * 65536 + readu8(CLAMP, p3) * 16777216)
writeu32(dst, off + 4, readu8(CLAMP, p4) + readu8(CLAMP, p5) * 256 + readu8(CLAMP, p6) * 65536 + readu8(CLAMP, p7) * 16777216)
off += stride
writeu8(dst, o, readu8(CLAMP, v0))
writeu8(dst, o + s1, readu8(CLAMP, v1))
writeu8(dst, o + s2, readu8(CLAMP, v2))
writeu8(dst, o + s3, readu8(CLAMP, v3))
writeu8(dst, o + s4, readu8(CLAMP, v4))
writeu8(dst, o + s5, readu8(CLAMP, v5))
writeu8(dst, o + s6, readu8(CLAMP, v6))
writeu8(dst, o + s7, readu8(CLAMP, v7))
end
end
@@ -642,6 +725,44 @@ end
-- Four bytes at a time: avg(a, b) = (a | b) - ((a ^ b) & 0xFE..) >> 1; the 4-tap average splits each byte into its
-- low 2 bits and high 6 bits so no lane overflows.
function Dsp.mc(src: buffer, sStride: number, sOff: number, dst: buffer, dStride: number, dOff: number, w: number, h: number, hx: number, hy: number, avg: boolean)
if hx == 0 and hy == 0 and not avg and (w == 16 or w == 8) then
local w16 = w == 16
for _ = 1, h do
writeu32(dst, dOff, readu32(src, sOff))
writeu32(dst, dOff + 4, readu32(src, sOff + 4))
if w16 then
writeu32(dst, dOff + 8, readu32(src, sOff + 8))
writeu32(dst, dOff + 12, readu32(src, sOff + 12))
end
sOff += sStride
dOff += dStride
end
return
end
if hx == 1 and hy == 1 then
-- a column of words at a time, each row's horizontal pair sums (low 2 bits, high 6 bits) carried to the next
for i = 0, w - 4, 4 do
local s0, d0 = sOff + i, dOff + i
local a, b = readu32(src, s0), readu32(src, s0 + 1)
local l0 = band(a, 0x03030303) + band(b, 0x03030303) + 0x02020202
local h0 = rshift(band(a, 0xFCFCFCFC), 2) + rshift(band(b, 0xFCFCFCFC), 2)
for _ = 1, h do
s0 += sStride
local c: number, d: number = readu32(src, s0), readu32(src, s0 + 1)
local l1 = band(c, 0x03030303) + band(d, 0x03030303)
local h1 = rshift(band(c, 0xFCFCFCFC), 2) + rshift(band(d, 0xFCFCFCFC), 2)
local p = h0 + h1 + band(rshift(l0 + l1, 2), 0x0F0F0F0F)
if avg then
local q = readu32(dst, d0)
p = bor(p, q) - rshift(band(bxor(p, q), 0xFEFEFEFE), 1)
end
writeu32(dst, d0, p)
l0, h0 = l1 + 0x02020202, h1
d0 += dStride
end
end
return
end
for _ = 1, h do
for i = 0, w - 4, 4 do
local p
@@ -652,15 +773,9 @@ function Dsp.mc(src: buffer, sStride: number, sOff: number, dst: buffer, dStride
local a, b = readu32(src, sOff + i), readu32(src, sOff + i + sStride)
p = bor(a, b) - rshift(band(bxor(a, b), 0xFEFEFEFE), 1)
end
elseif hy == 0 then
local a, b = readu32(src, sOff + i), readu32(src, sOff + i + 1)
p = bor(a, b) - rshift(band(bxor(a, b), 0xFEFEFEFE), 1)
else
local a, b = readu32(src, sOff + i), readu32(src, sOff + i + 1)
local c, d = readu32(src, sOff + i + sStride), readu32(src, sOff + i + sStride + 1)
local l = band(a, 0x03030303) + band(b, 0x03030303) + band(c, 0x03030303) + band(d, 0x03030303) + 0x02020202
local hi = rshift(band(a, 0xFCFCFCFC), 2) + rshift(band(b, 0xFCFCFCFC), 2) + rshift(band(c, 0xFCFCFCFC), 2) + rshift(band(d, 0xFCFCFCFC), 2)
p = hi + band(rshift(l, 2), 0x0F0F0F0F)
p = bor(a, b) - rshift(band(bxor(a, b), 0xFEFEFEFE), 1)
end
if avg then
local q = readu32(dst, dOff + i)
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/H263/Tables: the H.263 variable-length codes (ITU-T H.263 (01/2005) tables 7 / 8 MCBPC, 12 DQUANT,
-- 13 CBPY, 14 MVD, 16 TCOEF, figure 14 zigzag). Flat tables indexed by the next K bits: len * 65536 + value (len 0: none).
-- MCBPC_I / MCBPC_P (K = 9): type * 4 + CBPC (0 INTER, 1 INTER+Q, 2 INTER4V, 3 INTRA, 4 INTRA+Q), STUFFING
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/H263: an H.263 / H.263+ and Sorenson Spark (FLV1) video decoder, bit-exact with ffmpeg -idct int.
-- From ITU-T H.263 (01/2005) and Adobe's Video File Format Specification v10. Annex D UMV, GOB headers and Sorenson
-- escapes are supported; arithmetic coding, advanced prediction, PB / B / EI / EP and other H.263+ options are not.
+6 -2
View File
@@ -153,8 +153,9 @@ end
function Deblock.picture(S: any, pic: any, params: { any })
local mbW, mbH = S.mbW, S.mbH
local w4 = S.w4
local kind, sliceTab, qpTab, qpcTab, qpc2Tab, t8Tab = S.kind, S.sliceTab, S.qpTab, S.qpcTab, S.qpc2Tab, S.t8Tab
local kind, sliceTab, qpTab, qpcTab, qpc2Tab, t8Tab, flatTab = S.kind, S.sliceTab, S.qpTab, S.qpcTab, S.qpc2Tab, S.t8Tab, S.flatTab
local nzDb = S.nzDb
local uid0, uid1, mv0, mv1 = pic.uid0, pic.uid1, pic.mv0, pic.mv1
local ys, cs = pic.ys, pic.cs
local Y, U, V = pic.y, pic.u, pic.v
local hook = S.hook
@@ -172,6 +173,7 @@ function Deblock.picture(S: any, pic: any, params: { any })
local idc, aOff, bOff = prm[1], prm[2], prm[3]
local intraQ = kind[addr] <= 4
local t8 = t8Tab[addr] == 1
local flat = flatTab[addr] == 1 -- the inner edges have bS 0
local qpQ, qpcQ, qpc2Q = qpTab[addr], qpcTab[addr], qpc2Tab[addr]
local g0 = my * 4 * w4 + mx * 4 + 1
local lo = pic.yo + my * 16 * ys + mx * 16
@@ -193,7 +195,7 @@ function Deblock.picture(S: any, pic: any, params: { any })
if e == 0 and not edgeOk then
continue
end
if t8 and (e == 1 or e == 3) then
if (t8 and (e == 1 or e == 3)) or (flat and e > 0) then
continue
end
local intraP = intraQ
@@ -217,6 +219,8 @@ function Deblock.picture(S: any, pic: any, params: { any })
bS = ((e == 0) and (4) or (3))
elseif nzDb[qg] ~= 0 or nzDb[pg] ~= 0 then
bS = 2
elseif uid0[pg] == uid0[qg] and uid1[pg] == uid1[qg] and mv0[2 * pg - 1] == mv0[2 * qg - 1] and mv0[2 * pg] == mv0[2 * qg] and mv1[2 * pg - 1] == mv1[2 * qg - 1] and mv1[2 * pg] == mv1[2 * qg] then
bS = 0 -- the same references and vectors (what motionDiffers would find, sooner)
elseif motionDiffers(pic, pg, qg) then
bS = 1
else
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/H264/Decoder: the decoding process above the macroblock layer (ITU-T H.264 8.1, 8.2, C.4): picture
-- boundaries (7.4.1.2.4), POC types 0 / 1 / 2 (8.2.1), frame_num gaps (8.2.5.2), list modification (8.2.4), marking
-- (8.2.5), output in POC order (num_reorder_frames, else the DPB size).
+122 -50
View File
@@ -4,6 +4,8 @@
-- codecs/video/H264/Inter: inter prediction sample interpolation and weighting (ITU-T H.264 8.4.2.2 / 8.4.2.3).
-- Reference planes carry a border of PAD / PADC replicated samples, so reads equal the clamped positions of 8.4.2.2.1.
local readu8, writeu8 = buffer.readu8, buffer.writeu8
local readu32, writeu32 = buffer.readu32, buffer.writeu32
local band, bor, bxor, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.rshift
local Inter = {}
@@ -18,51 +20,51 @@ end
local B1 = table.create(24 * 17, 0)
-- one small function per six-tap case: LuaJIT keeps each loop in one trace
local function lumaCopy(ref: buffer, s: number, base: number, w: number, h: number, P: { number })
local function lumaCopy(ref: buffer, s: number, base: number, w: number, h: number, P: buffer)
for r = 0, h - 1 do
local o = base + r * s
local d = r * 16 + 1
for c = 0, w - 1 do
P[d + c] = readu8(ref, o + c)
local d = r * 16
for c = 0, w - 4, 4 do
writeu32(P, d + c, readu32(ref, o + c))
end
end
end
-- horizontal half sample b; avg 0: b, 1: (G + b + 1) >> 1, 2: (b + H + 1) >> 1
local function lumaH(ref: buffer, s: number, base: number, w: number, h: number, P: { number }, avg: number)
local function lumaH(ref: buffer, s: number, base: number, w: number, h: number, P: buffer, avg: number)
for r = 0, h - 1 do
local o = base + r * s
local d = r * 16 + 1
local d = r * 16
local a0, a1, a2, a3, a4 = readu8(ref, o - 2), readu8(ref, o - 1), readu8(ref, o), readu8(ref, o + 1), readu8(ref, o + 2)
for c = 0, w - 1 do
local a5 = readu8(ref, o + c + 3)
local v = min(max((a0 - 5 * a1 + 20 * a2 + 20 * a3 - 5 * a4 + a5 + 16) // 32, 0), 255)
local v = min(max(((a2 + a3) * 20 - (a1 + a4) * 5 + a0 + a5 + 16) // 32, 0), 255)
if avg == 1 then
v = (v + a2 + 1) // 2
elseif avg == 2 then
v = (v + a3 + 1) // 2
end
P[d + c] = v
writeu8(P, d + c, v)
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
end
end
end
-- vertical half sample h; avg 0: h, 1: (G + h + 1) >> 1, 2: (h + G below + 1) >> 1
local function lumaV(ref: buffer, s: number, base: number, w: number, h: number, P: { number }, avg: number)
local function lumaV(ref: buffer, s: number, base: number, w: number, h: number, P: buffer, avg: number)
for c = 0, w - 1 do
local o = base + c
local a0, a1, a2, a3, a4 = readu8(ref, o - 2 * s), readu8(ref, o - s), readu8(ref, o), readu8(ref, o + s), readu8(ref, o + 2 * s)
local d = c + 1
local d = c
for r = 0, h - 1 do
local a5 = readu8(ref, o + (r + 3) * s)
local v = min(max((a0 - 5 * a1 + 20 * a2 + 20 * a3 - 5 * a4 + a5 + 16) // 32, 0), 255)
local v = min(max(((a2 + a3) * 20 - (a1 + a4) * 5 + a0 + a5 + 16) // 32, 0), 255)
if avg == 1 then
v = (v + a2 + 1) // 2
elseif avg == 2 then
v = (v + a3 + 1) // 2
end
P[d + r * 16] = v
writeu8(P, d + r * 16, v)
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
end
end
@@ -76,27 +78,27 @@ local function horizontal1(ref: buffer, s: number, base: number, w: number, h: n
local a0, a1, a2, a3, a4 = readu8(ref, o - 2), readu8(ref, o - 1), readu8(ref, o), readu8(ref, o + 1), readu8(ref, o + 2)
for c = 0, w do
local a5 = readu8(ref, o + c + 3)
B1[d + c] = a0 - 5 * a1 + 20 * a2 + 20 * a3 - 5 * a4 + a5
B1[d + c] = (a2 + a3) * 20 - (a1 + a4) * 5 + a0 + a5
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
end
end
end
-- centre j; with = 0: j, 1: (b + j + 1) >> 1 (b of this row), 2: (s + j + 1) >> 1 (b of the next row)
local function lumaJ(ref: buffer, s: number, base: number, w: number, h: number, P: { number }, with: number)
local function lumaJ(ref: buffer, s: number, base: number, w: number, h: number, P: buffer, with: number)
horizontal1(ref, s, base, w, h)
for c = 0, w - 1 do
local k = 2 * 17 + 1 + c
local a0, a1, a2, a3, a4 = B1[k - 34], B1[k - 17], B1[k], B1[k + 17], B1[k + 34]
for r = 0, h - 1 do
local a5 = B1[k + 51]
local v = min(max((a0 - 5 * a1 + 20 * a2 + 20 * a3 - 5 * a4 + a5 + 512) // 1024, 0), 255)
local v = min(max(((a2 + a3) * 20 - (a1 + a4) * 5 + a0 + a5 + 512) // 1024, 0), 255)
if with == 1 then
v = (v + min(max((a2 + 16) // 32, 0), 255) + 1) // 2
elseif with == 2 then
v = (v + min(max((a3 + 16) // 32, 0), 255) + 1) // 2
end
P[r * 16 + c + 1] = v
writeu8(P, r * 16 + c, v)
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
k += 17
end
@@ -104,38 +106,38 @@ local function lumaJ(ref: buffer, s: number, base: number, w: number, h: number,
end
-- (j + h + 1) >> 1 with h at column c + co (co 0: i, 1: k)
local function lumaJV(ref: buffer, s: number, base: number, w: number, h: number, P: { number }, co: number)
local function lumaJV(ref: buffer, s: number, base: number, w: number, h: number, P: buffer, co: number)
lumaJ(ref, s, base, w, h, P, 0)
for c = 0, w - 1 do
local o = base + c + co
local a0, a1, a2, a3, a4 = readu8(ref, o - 2 * s), readu8(ref, o - s), readu8(ref, o), readu8(ref, o + s), readu8(ref, o + 2 * s)
for r = 0, h - 1 do
local a5 = readu8(ref, o + (r + 3) * s)
local hv = min(max((a0 - 5 * a1 + 20 * a2 + 20 * a3 - 5 * a4 + a5 + 16) // 32, 0), 255)
local d = r * 16 + c + 1
P[d] = (P[d] + hv + 1) // 2
local hv = min(max(((a2 + a3) * 20 - (a1 + a4) * 5 + a0 + a5 + 16) // 32, 0), 255)
local d = r * 16 + c
writeu8(P, d, (readu8(P, d) + hv + 1) // 2)
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
end
end
end
-- diagonal quarter samples e, g, p, r: (b or s) averaged with (h or m)
local function lumaDiag(ref: buffer, s: number, base: number, w: number, h: number, P: { number }, ro: number, co: number)
local function lumaDiag(ref: buffer, s: number, base: number, w: number, h: number, P: buffer, ro: number, co: number)
lumaH(ref, s, base + ro, w, h, P, 0)
for c = 0, w - 1 do
local o = base + c + co
local a0, a1, a2, a3, a4 = readu8(ref, o - 2 * s), readu8(ref, o - s), readu8(ref, o), readu8(ref, o + s), readu8(ref, o + 2 * s)
for r = 0, h - 1 do
local a5 = readu8(ref, o + (r + 3) * s)
local vb = min(max((a0 - 5 * a1 + 20 * a2 + 20 * a3 - 5 * a4 + a5 + 16) // 32, 0), 255)
local d = r * 16 + c + 1
P[d] = (P[d] + vb + 1) // 2
local vb = min(max(((a2 + a3) * 20 - (a1 + a4) * 5 + a0 + a5 + 16) // 32, 0), 255)
local d = r * 16 + c
writeu8(P, d, (readu8(P, d) + vb + 1) // 2)
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
end
end
end
function Inter.luma(ref: buffer, s: number, base: number, fx: number, fy: number, w: number, h: number, P: { number })
function Inter.luma(ref: buffer, s: number, base: number, fx: number, fy: number, w: number, h: number, P: buffer)
if fy == 0 then
if fx == 0 then
lumaCopy(ref, s, base, w, h, P)
@@ -153,13 +155,13 @@ function Inter.luma(ref: buffer, s: number, base: number, fx: number, fy: number
end
end
function Inter.chroma(ref: buffer, s: number, base: number, fx: number, fy: number, w: number, h: number, P: { number })
function Inter.chroma(ref: buffer, s: number, base: number, fx: number, fy: number, w: number, h: number, P: buffer)
if fx == 0 and fy == 0 then
for r = 0, h - 1 do
local o = base + r * s
local d = r * 16 + 1
local d = r * 16
for c = 0, w - 1 do
P[d + c] = readu8(ref, o + c)
writeu8(P, d + c, readu8(ref, o + c))
end
end
return
@@ -168,69 +170,139 @@ function Inter.chroma(ref: buffer, s: number, base: number, fx: number, fy: numb
local wb = fx * (8 - fy)
local wc = (8 - fx) * fy
local wd = fx * fy
if fx == 0 or fy == 0 then
local dx = if fx == 0 then s else 1
local w2 = if fx == 0 then wc else wb
if fy == 0 then
for r = 0, h - 1 do
local o = base + r * s
local d = r * 16
local a = readu8(ref, o)
for x = 1, w do
local b = readu8(ref, o + x)
writeu8(P, d + x - 1, (wa * a + w2 * b + 32) // 64)
a = b
end
end
return
end
for r = 0, h - 1 do
local o = base + r * s
local d = r * 16
for c = 0, w - 1 do
local p = o + c
writeu8(P, d + c, (wa * readu8(ref, p) + w2 * readu8(ref, p + dx) + 32) // 64)
end
end
return
end
for r = 0, h - 1 do
local o = base + r * s
local d = r * 16 + 1
for c = 0, w - 1 do
local p = o + c
P[d + c] = (wa * readu8(ref, p) + wb * readu8(ref, p + 1) + wc * readu8(ref, p + s) + wd * readu8(ref, p + s + 1) + 32) // 64
local d = r * 16
local a, c = readu8(ref, o), readu8(ref, o + s)
for x = 1, w do
local b, e = readu8(ref, o + x), readu8(ref, o + s + x)
writeu8(P, d + x - 1, (wa * a + wb * b + wc * c + wd * e + 32) // 64)
a, c = b, e
end
end
end
function Inter.put(P: { number }, w: number, h: number, b: buffer, o: number, s: number)
for r = 0, h - 1 do
local d = r * 16 + 1
local q = o + r * s
for c = 0, w - 1 do
writeu8(b, q + c, P[d + c])
function Inter.copy(ref: buffer, rs: number, base: number, w: number, h: number, b: buffer, o: number, s: number)
if w >= 4 then
for r = 0, h - 1 do
local i, q = base + r * rs, o + r * s
for c = 0, w - 4, 4 do
writeu32(b, q + c, readu32(ref, i + c))
end
end
else
for r = 0, h - 1 do
local i, q = base + r * rs, o + r * s
for c = 0, w - 1 do
writeu8(b, q + c, readu8(ref, i + c))
end
end
end
end
-- default bi-prediction (P0 + P1 + 1) >> 1
function Inter.avg(P0: { number }, P1: { number }, w: number, h: number, b: buffer, o: number, s: number)
function Inter.put(P: buffer, w: number, h: number, b: buffer, o: number, s: number)
Inter.copy(P, 16, 0, w, h, b, o, s)
end
-- default bi-prediction (P0 + P1 + 1) >> 1, four samples at a time: (a | b) - ((a ^ b) & 0xFE..) >> 1
function Inter.avg(P0: buffer, P1: buffer, w: number, h: number, b: buffer, o: number, s: number)
if w >= 4 then
for r = 0, h - 1 do
local d = r * 16
local q = o + r * s
for c = 0, w - 4, 4 do
local x, y = readu32(P0, d + c), readu32(P1, d + c)
writeu32(b, q + c, bor(x, y) - rshift(band(bxor(x, y), 0xFEFEFEFE), 1))
end
end
return
end
for r = 0, h - 1 do
local d = r * 16 + 1
local d = r * 16
local q = o + r * s
for c = 0, w - 1 do
writeu8(b, q + c, (P0[d + c] + P1[d + c] + 1) // 2)
writeu8(b, q + c, (readu8(P0, d + c) + readu8(P1, d + c) + 1) // 2)
end
end
end
-- explicit single-list weighting (8-270 / 8-271)
function Inter.weight1(P: { number }, w: number, h: number, b: buffer, o: number, s: number, logWD: number, wt: number, off: number)
function Inter.weight1(P: buffer, w: number, h: number, b: buffer, o: number, s: number, logWD: number, wt: number, off: number)
if off == 0 and wt == 2 ^ logWD then
Inter.copy(P, 16, 0, w, h, b, o, s) -- the identity weight
return
end
if logWD >= 1 then
local rnd = 2 ^ (logWD - 1)
local div = 2 ^ logWD
for r = 0, h - 1 do
local d = r * 16 + 1
local d = r * 16
local q = o + r * s
for c = 0, w - 1 do
writeu8(b, q + c, clip((P[d + c] * wt + rnd) // div + off))
writeu8(b, q + c, clip((readu8(P, d + c) * wt + rnd) // div + off))
end
end
else
for r = 0, h - 1 do
local d = r * 16 + 1
local d = r * 16
local q = o + r * s
for c = 0, w - 1 do
writeu8(b, q + c, clip(P[d + c] * wt + off))
writeu8(b, q + c, clip(readu8(P, d + c) * wt + off))
end
end
end
end
-- explicit / implicit bi-prediction weighting (8-272); off = (o0 + o1 + 1) >> 1
function Inter.weight2(P0: { number }, P1: { number }, w: number, h: number, b: buffer, o: number, s: number, logWD: number, w0: number, w1: number, off: number)
function Inter.weight2(P0: buffer, P1: buffer, w: number, h: number, b: buffer, o: number, s: number, logWD: number, w0: number, w1: number, off: number)
local rnd = 2 ^ logWD
local div = 2 ^ (logWD + 1)
if off == 0 and w0 >= 0 and w1 >= 0 and w0 + w1 <= div then
-- a convex combination: within 0 .. 255 without the clip
if w0 == w1 and w0 * 2 == div then
Inter.avg(P0, P1, w, h, b, o, s)
return
end
for r = 0, h - 1 do
local d = r * 16
local q = o + r * s
for c = 0, w - 1 do
writeu8(b, q + c, (readu8(P0, d + c) * w0 + readu8(P1, d + c) * w1 + rnd) // div)
end
end
return
end
for r = 0, h - 1 do
local d = r * 16 + 1
local d = r * 16
local q = o + r * s
for c = 0, w - 1 do
writeu8(b, q + c, clip((P0[d + c] * w0 + P1[d + c] * w1 + rnd) // div + off))
writeu8(b, q + c, clip((readu8(P0, d + c) * w0 + readu8(P1, d + c) * w1 + rnd) // div + off))
end
end
end
+50 -17
View File
@@ -69,8 +69,8 @@ function Mb.new(): any
done = table.create(16, false),
reg = table.create(64, 0), -- motion compensation regions: x, y, w, h (4x4 units)
nreg = 0,
P0 = table.create(256, 0),
P1 = table.create(256, 0),
P0 = buffer.create(256),
P1 = buffer.create(256),
pcm = buffer.create(384),
}
end
@@ -87,6 +87,7 @@ function Mb.grids(mbW: number, mbH: number): any
qpcTab = table.create(n, 0), -- QPc (Cb)
qpc2Tab = table.create(n, 0), -- QPc (Cr)
t8Tab = table.create(n, 0),
flatTab = table.create(n, 0), -- 1: one motion for the whole macroblock and no luma coefficients
cpmTab = table.create(n, 0),
dcTab = table.create(n, 0), -- CABAC coded_block_flag of the DC blocks: 1 luma, 2 Cb, 4 Cr
qpdTab = table.create(n, 0), -- mb_qp_delta ~= 0
@@ -534,24 +535,28 @@ local function motion(S: any, M: any)
end
Mb.motion = motion
local function predLuma(S: any, ref: any, P: { number }, px: number, py: number, w: number, h: number, mvx: number, mvy: number)
local W, H = S.mbW * 16, S.mbH * 16
local ix = px + mvx // 4
local iy = py + mvy // 4
ix = clip3(2 - PAD, W + PAD - w - 4, ix)
iy = clip3(2 - PAD, H + PAD - h - 4, iy)
local s = ref.ys
Inter.luma(ref.y, s, ref.yo + iy * s + ix, mvx % 4, mvy % 4, w, h, P)
-- the integer sample positions of a prediction block, clamped into the padded reference
local function lumaX(S: any, px: number, mvx: number, w: number): number
return clip3(2 - PAD, S.mbW * 16 + PAD - w - 4, px + mvx // 4)
end
local function lumaY(S: any, py: number, mvy: number, h: number): number
return clip3(2 - PAD, S.mbH * 16 + PAD - h - 4, py + mvy // 4)
end
local function chromaX(S: any, px: number, mvx: number, w: number): number
return clip3(-PADC, S.mbW * 8 + PADC - 1 - w, px + mvx // 8)
end
local function chromaY(S: any, py: number, mvy: number, h: number): number
return clip3(-PADC, S.mbH * 8 + PADC - 1 - h, py + mvy // 8)
end
local function predChroma(S: any, ref: any, P: { number }, plane: buffer, px: number, py: number, w: number, h: number, mvx: number, mvy: number)
local W, H = S.mbW * 8, S.mbH * 8
local ix = px + mvx // 8
local iy = py + mvy // 8
ix = clip3(-PADC, W + PADC - 1 - w, ix)
iy = clip3(-PADC, H + PADC - 1 - h, iy)
local function predLuma(S: any, ref: any, P: buffer, px: number, py: number, w: number, h: number, mvx: number, mvy: number)
local s = ref.ys
Inter.luma(ref.y, s, ref.yo + lumaY(S, py, mvy, h) * s + lumaX(S, px, mvx, w), mvx % 4, mvy % 4, w, h, P)
end
local function predChroma(S: any, ref: any, P: buffer, plane: buffer, px: number, py: number, w: number, h: number, mvx: number, mvy: number)
local s = ref.cs
Inter.chroma(plane, s, ref.co + iy * s + ix, mvx % 8, mvy % 8, w, h, P)
Inter.chroma(plane, s, ref.co + chromaY(S, py, mvy, h) * s + chromaX(S, px, mvx, w), mvx % 8, mvy % 8, w, h, P)
end
local function refPic(list: { any }, r: number): any
@@ -603,6 +608,23 @@ local function mc(S: any, M: any)
elseif plane == 2 then
dst, o, s, bw, bh = pic.v, co, cs, cw, ch
end
if not bi and not explicit then
local ref, mx, my = ref0, m0x, m0y
if ref == nil then
ref, mx, my = ref1, m1x, m1y
end
if plane == 0 then
if mx % 4 == 0 and my % 4 == 0 then
local rs = ref.ys
Inter.copy(ref.y, rs, ref.yo + lumaY(S, py, my, lh) * rs + lumaX(S, px, mx, lw), lw, lh, dst, o, s)
continue
end
elseif mx % 8 == 0 and my % 8 == 0 then
local rs = ref.cs
Inter.copy(((plane == 1) and (ref.u) or (ref.v)), rs, ref.co + chromaY(S, cy, my, ch) * rs + chromaX(S, cx, mx, cw), cw, ch, dst, o, s)
continue
end
end
if ref0 then
if plane == 0 then
predLuma(S, ref0, P0, px, py, lw, lh, m0x, m0y)
@@ -806,6 +828,7 @@ function Mb.recon(S: any, M: any)
S.kind[addr] = kind
S.sliceTab[addr] = S.slice
S.t8Tab[addr] = M.t8
S.flatTab[addr] = 0
S.qpTab[addr] = ((kind == K_PCM) and (0) or (M.qp))
S.qpcTab[addr] = ((kind == K_PCM) and (S.qpcOf[1]) or (M.qpc))
S.qpc2Tab[addr] = ((kind == K_PCM) and (S.qpc2Of[1]) or (M.qpc2))
@@ -856,6 +879,16 @@ function Mb.recon(S: any, M: any)
nzDb[g0 + (blk // 4) * w4 + blk % 4] = ((nz[blk + 1] > 0) and (1) or (0))
end
end
if kind == K_PSKIP or ((kind == K_P or kind == K_B) and M.part == 0) then
local flat = 1
for i = 1, 16 do
if nz[i] > 0 then
flat = 0
break
end
end
S.flatTab[addr] = flat
end
if M.cbp % 16 ~= 0 then
lumaResidual(S, M)
end
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/video/H264/Params: sequence and picture parameter sets (ITU-T H.264 7.3.2.1 / 7.3.2.2 / E.1), scaling lists
-- with the fall-back rules A and B (7.4.2.1.1 / table 7-2), and the dequantisation tables built from them (8.5.9: the
-- LevelScale4x4 / LevelScale8x8 functions, premultiplied by 2^(qP / 6)).
+1
View File
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/video/H264/Slice: the slice header (ITU-T H.264 7.3.3): ref_pic_list_modification, pred_weight_table and
-- dec_ref_pic_marking. Frame coding only: field pictures, MBAFF, SP / SI, separate colour planes and redundant
-- pictures return "unsupported: ...".
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/video/H264/Tables: the constant tables of ITU-T H.264 (CAVLC 9-5, 9-7..9-10; CABAC init 9-12..9-24,
-- rangeTabLPS 9-44, transitions 9-45; chroma QP 8-15; me(v) CBP 9-4; scaling lists 7-3 / 7-4; zig-zag 8-12 / 8-13;
-- 9-43; deblocking 8-16 / 8-17). Values from openh264 2.x (BSD-2-Clause, licenses/openh264-LICENSE.txt).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/H264: an H.264 / AVC (ITU-T H.264 | ISO/IEC 14496-10) video decoder in pure Luau, bit-exact with ffmpeg.
-- Clean-room from the spec; constant tables from openh264 (BSD-2-Clause, licenses/openh264-LICENSE.txt). 8-bit 4:2:0
-- Constrained Baseline, Main and High. UNSUPPORTED: interlaced coding (field pictures, MBAFF), 4:0:0 / 4:2:2 / 4:4:4,
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Legacy: FLI / FLC, Cinepak, Microsoft Video 1, QuickTime RLE / SMC and Smacker video decoders.
-- From the format descriptions (see each submodule), bit-exact with ffmpeg. Palette: track.palette, replaced by a
-- packet's palette (256 u32 entries R, G, B, A); depth: track.bitsPerPixel (AVI biBitCount, QuickTime depth).
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Mpeg1: the MPEG-1 video decoder (ISO/IEC 11172-2), the old name and API over codecs/video/Mpeg2.
-- From mpeg1-luau e2e2898 (same author).
local Mpeg2 = require("@rosetta/codecs/video/Mpeg2")
+76 -56
View File
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- codecs/video/Mpeg2/Decoder: the MPEG-1 / MPEG-2 video decoding core (ISO/IEC 11172-2, 13818-2 Main profile).
-- From mpeg1-luau e2e2898 (same author; a port of jsmpeg's src/mpeg1.js by Dominic Szablewski, MIT,
-- licenses/jsmpeg-MIT.txt). Dequantisation follows ffmpeg's arithmetic; coefficients are saturated to -2048..2047
@@ -48,7 +49,6 @@ for i = 0, 31 do
buffer.writeu8(NON_LINEAR, i, T.NON_LINEAR_QSCALE[i])
end
local COFF = 40000
local MBAI, MBAI_BITS = V.MBAI, V.MBAI_BITS
local CBP, CBP_BITS = V.CBP, V.CBP_BITS
@@ -63,15 +63,20 @@ local MBTYPE_BITS = { V.MBTYPE_I_BITS, V.MBTYPE_P_BITS, V.MBTYPE_B_BITS }
-- BT.601 limited range: R = 1.164 (Y - 16) + 1.596 (Cr - 128), G = 1.164 (Y - 16) - 0.392 (Cb - 128) - 0.813 (Cr -
-- 128), B = 1.164 (Y - 16) + 2.017 (Cb - 128), in 8-bit fixed point. The rounding offsets (-128, -160, -72 / 256)
-- are fitted to ffmpeg's swscale output, which lands about half a level below round-to-nearest.
-- the constant terms, plus COFF * 256 so the sums stay positive for rshift
local RBIAS = COFF * 256 - 298 * 16 - 409 * 128 - 128
local GBIAS = COFF * 256 - 298 * 16 + 100 * 128 + 208 * 128 - 160
local BBIAS = COFF * 256 - 298 * 16 - 516 * 128 - 72
-- clamp by lookup: RGB_CLAMP[v + COFF] for v in [-COFF, COFF + 255]; the colour biases include COFF * 256
local RGB_CLAMP = buffer.create(2 * COFF + 256)
for i = 0, 2 * COFF + 255 do
buffer.writeu8(RGB_CLAMP, i, math.clamp(i - COFF, 0, 255))
-- the constant terms, plus TOFF * 256: (sum + bias) >> 8 lies in 747..1558 for 8-bit samples, the index of the
-- clamped channel already shifted into place (TB carries the alpha)
local TOFF = 1024
local TRB = TOFF * 256 - 298 * 16 - 409 * 128 - 128
local TGB = TOFF * 256 - 298 * 16 + 100 * 128 + 208 * 128 - 160
local TBB = TOFF * 256 - 298 * 16 - 516 * 128 - 72
local TR: { number } = table.create(2048, 0)
local TG: { number } = table.create(2048, 0)
local TB: { number } = table.create(2048, 0)
for i = 1, 2048 do
local v = math.clamp(i - TOFF, 0, 255)
TR[i] = v
TG[i] = v * 256
TB[i] = v * 65536 + 0xFF000000
end
-- 4:2:0 -> RGBA8 with nearest chroma, like ffmpeg's default swscale path; hook is pcall'ed (can't throw through here)
@@ -83,41 +88,56 @@ local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number,
local chw = cw // 2
local w2 = w // 2 -- whole pixel pairs per row
local stride = w * 4
local C = RGB_CLAMP
local tr, tg, tb = TR, TG, TB
local n = 0
for y = r0, r1 - 1, 2 do
local yo = y * cw
local co = (y // 2) * chw
local o = base + (y - r0) * stride
local two = y + 1 < r1
for _ = 1, w2 do
local cb, cr = readu8(Cb, co), readu8(Cr, co)
co += 1
local rr = cr * 409 + RBIAS
local gg = GBIAS - cb * 100 - cr * 208
local bb = cb * 516 + BBIAS
local yv = readu8(Y, yo) * 298
writeu32(out, o, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
yv = readu8(Y, yo + 1) * 298
writeu32(out, o + 4, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
if two then
if y + 1 < r1 then
local o2 = o + stride
for _ = 1, w2 do
local cb, cr = readu8(Cb, co), readu8(Cr, co)
co += 1
local rr = cr * 409 + TRB
local gg = TGB - cb * 100 - cr * 208
local bb = cb * 516 + TBB
local yv = readu8(Y, yo) * 298
writeu32(out, o, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
yv = readu8(Y, yo + 1) * 298
writeu32(out, o + 4, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
yv = readu8(Y, yo + cw) * 298
writeu32(out, o + stride, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
writeu32(out, o2, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
yv = readu8(Y, yo + cw + 1) * 298
writeu32(out, o + stride + 4, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
writeu32(out, o2 + 4, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
yo += 2
o += 8
o2 += 8
end
else
for _ = 1, w2 do
local cb, cr = readu8(Cb, co), readu8(Cr, co)
co += 1
local rr = cr * 409 + TRB
local gg = TGB - cb * 100 - cr * 208
local bb = cb * 516 + TBB
local yv = readu8(Y, yo) * 298
writeu32(out, o, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
yv = readu8(Y, yo + 1) * 298
writeu32(out, o + 4, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
yo += 2
o += 8
end
yo += 2
o += 8
end
if w % 2 == 1 then
-- the last column of an odd width
local cb, cr = readu8(Cb, co), readu8(Cr, co)
local rr = cr * 409 + RBIAS
local gg = GBIAS - cb * 100 - cr * 208
local bb = cb * 516 + BBIAS
for k = 0, if two then 1 else 0 do
local rr = cr * 409 + TRB
local gg = TGB - cb * 100 - cr * 208
local bb = cb * 516 + TBB
for k = 0, if y + 1 < r1 then 1 else 0 do
local yv = readu8(Y, yo + k * cw) * 298
writeu32(out, o + k * stride, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
writeu32(out, o + k * stride, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
end
end
n += 2
@@ -132,16 +152,14 @@ D.toRgba = toRgba
-- YCbCr 4:2:2 planes -> RGBA8: chroma row y for luma row y, chroma column x // 2
local function toRgba422(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number, h: number, out: buffer)
local chw = cw // 2
local tr, tg, tb = TR, TG, TB
for y = 0, h - 1 do
local o = y * w * 4
for x = 0, w - 1 do
local c = y * chw + x // 2
local cb, cr = readu8(Cb, c), readu8(Cr, c)
local yv = readu8(Y, y * cw + x) * 298
local r = math.clamp(rshift(yv + cr * 409 + RBIAS, 8) - COFF, 0, 255)
local g = math.clamp(rshift(yv + GBIAS - cb * 100 - cr * 208, 8) - COFF, 0, 255)
local b = math.clamp(rshift(yv + cb * 516 + BBIAS, 8) - COFF, 0, 255)
writeu32(out, o + x * 4, r + g * 256 + b * 65536 + 0xFF000000)
writeu32(out, o + x * 4, tr[rshift(yv + cr * 409 + TRB, 8)] + tg[rshift(yv + TGB - cb * 100 - cr * 208, 8)] + tb[rshift(yv + cb * 516 + TBB, 8)])
end
end
end
@@ -723,7 +741,7 @@ function D.new(maxPixels: number?): Core
local function decodeBlock(b: number): boolean
local n = 0
local q: buffer
local onlyDc = true
local last = 0 -- the highest natural index written
local luma = b < 4
local parityOdd = 1 -- MPEG-2 mismatch control: 1 while the coefficient sum is even
if mbIntra then
@@ -783,31 +801,30 @@ function D.new(maxPixels: number?): Core
local intra = mbIntra
local qs = qscale
local m2 = mpeg2
local d = data
local p = pos
while true do
local bp = rshift(pos, 3)
local w = bswap(readu32(data, bp))
local s = band(pos, 7)
if s ~= 0 then
w = bor(lshift(w, s), rshift(readu8(data, bp + 4), 8 - s))
end
-- 25 valid bits at least: the longest code (16) and its sign
local w = lshift(bswap(readu32(d, rshift(p, 3))), band(p, 7))
local e = readu32(lut, rshift(w, 24) * 4)
if e >= SUB then
e = readu32(lut, (band(e, 0xFFFF) * 256 + band(rshift(w, 16), 255)) * 4)
end
local l = rshift(e, 16)
if l == 0 then
pos = p
err = true
return false
end
local v = band(e, 0xFFFF)
lut = nextLut
if v == EOB then
pos += l
p += l
break
end
local run, level, neg
if v == ESC then
pos += l
pos = p + l
run = read(6)
if m2 then
-- a 12-bit signed level
@@ -834,14 +851,16 @@ function D.new(maxPixels: number?): Core
err = true
return false
end
p = pos
else
run = rshift(v, 8)
level = band(v, 255)
neg = band(lshift(w, l), 0x80000000) ~= 0 -- the sign bit follows the code
pos += l + 1
p += l + 1
end
n += run
if n > 63 then
pos = p
err = true
return false
end
@@ -849,15 +868,15 @@ function D.new(maxPixels: number?): Core
-- dequantise the magnitude (ffmpeg's arithmetic); MPEG-1 makes it odd toward zero
local m
if intra then
m = (level * qs * readu8(q, zz)) // 16
m = rshift(level * qs * readu8(q, zz), 4)
else
m = ((level * 2 + 1) * qs * readu8(q, zz)) // 32
m = rshift((level * 2 + 1) * qs * readu8(q, zz), 5)
end
if not m2 then
if m == 0 then
m = -1
elseif band(m, 1) == 0 then
m -= 1
else
m = bor(m - 1, 1)
end
end
if neg then
@@ -872,17 +891,18 @@ function D.new(maxPixels: number?): Core
elseif m < -2048 then
m = -2048
end
if zz ~= 0 then
onlyDc = false
if zz > last then
last = zz
end
writei32(blk, zz * 4, m)
n += 1
end
pos = p
if m2 and parityOdd == 1 then
-- mismatch control: toggle the last coefficient's lowest bit
local v = readi32(blk, 252)
writei32(blk, 252, if v % 2 ~= 0 then v - 1 else v + 1)
onlyDc = false
last = 63
end
local dst, off, stride
if luma then
@@ -917,12 +937,12 @@ function D.new(maxPixels: number?): Core
off = (mbRow * 16 + k * 8) * chw + mbCol * 8
end
end
if onlyDc then
if last == 0 then
fillBlock(dst, off, stride, (readi32(blk, 0) + 4) // 8, not intra)
writei32(blk, 0, 0)
else
idct(blk, dst, off, stride, not intra)
buffer.fill(blk, 0, 0, 256)
idct(blk, dst, off, stride, not intra, last)
buffer.fill(blk, 0, 0, (last // 8 + 1) * 32)
end
return true
end
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Mpeg2/Tables: MPEG-1 / MPEG-2 video constant tables.
-- MPEG-1: jsmpeg's (https://github.com/phoboslab/jsmpeg, Dominic Szablewski, MIT, licenses/jsmpeg-MIT.txt), 0-based, via
-- mpeg1-luau e2e2898. MPEG-2 (DC sizes to 11, B-15, non-linear quantiser scale, alternate scan): ISO/IEC 13818-2 annex B / 7.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Mpeg2/Vlc: lookup tables for the MPEG-1 / MPEG-2 variable-length codes, built at load time (Tables.luau).
-- Flat: indexed by the next K bits, len * 65536 + (value + 32768), len 0 = invalid. DCT: two-level (8 + 8 bits),
-- len * 65536 + run * 256 + level, ESC = 0xFFFF, EOB = 0xFFFE, or SUB + subtable for a long-code prefix.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Mpeg2: an MPEG-1 / MPEG-2 video decoder (ISO/IEC 11172-2, ISO/IEC 13818-2 Main profile).
-- Bit-exact with `ffmpeg -idct int`. Grown from mpeg1-luau e2e2898 (same author), a port of jsmpeg's decoder
-- (https://github.com/phoboslab/jsmpeg, Dominic Szablewski, MIT: licenses/jsmpeg-MIT.txt). Frames come out woven (top
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Theora/Header: the three Theora headers (Theora I specification, section 6) and the dequantisation and
-- Huffman lookup tables derived from them. A Luau port of libtheora 1.2.0 (Xiph.Org, BSD 3-clause,
-- licenses/libtheora-BSD.txt): decinfo.c, dequant.c, quant.c (oc_dequant_tables_init), huffdec.c.
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Theora: a Theora video decoder (the Theora I specification, Xiph.Org), for Ogg and Matroska (V_THEORA).
-- A Luau port of the libtheora 1.2.0 decoder (BSD 3-clause, licenses/libtheora-BSD.txt), bit-exact with libtheora /
-- ffmpeg; libtheora's optional post-processing is not supported.
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Video/Source: what a video writer (Matroska / WebM, AVI, Y4M, MP4) gets to write, from a VideoDecoder
-- or a Demuxer: each stream is copied (remuxed) or produced (decoded frames re-encoded, PCM audio).
local Err = require("@rosetta/core/Err")
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/video/Video: the video codec registry and the generic VideoDecoder. New in rosetta.
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- containers/Avi: the AVI RIFF form (Microsoft AVI 1.0 + OpenDML 1.02), read and written. New in rosetta. The chunk
-- index comes from the OpenDML super index (indx -> ix##), else idx1 (movi-relative or absolute, detected), else a
-- scan of the movi lists. Writing: AVI 1.0 (avih, strl, LIST movi, idx1) with an odml / dmlh header.
+1
View File
@@ -1,4 +1,5 @@
--!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,
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- 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
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- containers/MpegPs: the MPEG program stream (ISO/IEC 11172-1 system streams and ISO/IEC 13818-1 program streams:
-- .mpg / .mpeg / .vob). New in rosetta. 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
+1
View File
@@ -1,5 +1,6 @@
--!strict
--!native
--!optimize 2
-- containers/MpegTs: an MPEG transport stream demuxer (ISO/IEC 13818-1). Absorbed from mpegts-luau 6e16197 (same
-- author; no third-party code); changed: the unit queue keeps an explicit tail (#q on a table with holes at its front
-- loses units under LuaJIT's border rule). Never throws: bad input is skipped and counted in `stats`.