ws-video2: codecs/video/Mpeg2 (MPEG-1/2 core, Main profile, 4:2:2, field pictures, dual prime), bit-exact with ffmpeg -idct int

Mpeg1 is now the Mpeg2 module under its old name (core, tables and VLCs moved to Mpeg2/). Dsp.mc takes separate
source / destination strides; Dsp.edge emulates edges. containers/MpegEs: field-picture pairs are one access unit
(pictureStructure). Registry: mpeg1 and mpeg2 decode through Mpeg2.newVideoDecoder; more MPEG-2 FourCCs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 14:25:51 -05:00
1 parent 4767a63a50
commit c33ffa7343
12 files changed
+2487 -1289

No files matched your search

+35 -15
View File
@@ -16,7 +16,8 @@
--
-- Dsp.idct(blk, dst, off, stride, add) -- blk: 64 i32 (256 bytes, row-major), left holding garbage: clear it
-- Dsp.fill(dst, off, stride, v, add) -- an 8x8 block of constant v (a DC-only block: v = (dc + 4) >> 3)
-- Dsp.mc(src, dst, stride, sOff, dOff, size, hx, hy, avg) -- half-pel prediction, size 8 / 16 (multiple of 4)
-- Dsp.mc(src, sStride, sOff, dst, dStride, dOff, w, h, hx, hy, avg) -- half-pel prediction of a w x h block
-- Dsp.edge(src, stride, base, x, y, w, h, pw, ph, out) -- a block with edge samples repeated (EDGE_STRIDE)
local Dsp = {}
local readu8 = buffer.readu8
@@ -654,20 +655,20 @@ function Dsp.fill(dst: buffer, off: number, stride: number, v: number, add: bool
end
-- ------------------------------------------------------------------------------------------------ motion
-- Motion-compensated prediction of a size x size block (size 8 or 16; 4 for H.263 chroma is fine too: any multiple
-- of 4) from src at sOff to dst at dOff, both with stride (twice the plane width for field prediction), half-pel
-- flags hx / hy, rounding up (MPEG-1 / 2, H.263 rounding_type 0); avg averages into what dst holds (the second
-- prediction of a bidirectional block, rounding up). 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, dst: buffer, stride: number, sOff: number, dOff: number, size: number, hx: number, hy: number, avg: boolean)
for _ = 1, size do
for i = 0, size - 4, 4 do
-- Motion-compensated prediction of a w x h block (w a multiple of 4) from src (rows sStride apart, from sOff) to dst
-- (dStride, dOff): half-pel flags hx / hy, rounding up (MPEG-1 / 2, H.263 rounding_type 0); avg averages into what
-- dst holds (the second prediction of a bidirectional block, rounding up). Strides are twice the plane width for
-- field prediction. 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)
for _ = 1, h do
for i = 0, w - 4, 4 do
local p
if hx == 0 then
if hy == 0 then
p = readu32(src, sOff + i)
else
local a, b = readu32(src, sOff + i), readu32(src, sOff + i + stride)
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
@@ -675,10 +676,10 @@ function Dsp.mc(src: buffer, dst: buffer, stride: number, sOff: number, dOff: nu
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 + stride), readu32(src, sOff + i + stride + 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 h = rshift(band(a, 0xFCFCFCFC), 2) + rshift(band(b, 0xFCFCFCFC), 2) + rshift(band(c, 0xFCFCFCFC), 2) + rshift(band(d, 0xFCFCFCFC), 2)
p = h + band(rshift(l, 2), 0x0F0F0F0F)
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)
end
if avg then
local q = readu32(dst, dOff + i)
@@ -686,8 +687,27 @@ function Dsp.mc(src: buffer, dst: buffer, stride: number, sOff: number, dOff: nu
end
writeu32(dst, dOff + i, p)
end
sOff += stride
dOff += stride
sOff += sStride
dOff += dStride
end
end
-- Edge emulation: the (w + 1) x (h + 1) samples at (x, y) of a pw x ph plane (row r at base + r * stride) copied to
-- out (rows EDGE_STRIDE apart), coordinates clamped into the plane, so a vector pointing outside the picture
-- predicts from repeated edge samples (w + 1 <= EDGE_STRIDE, h + 1 <= 33; out holds EDGE_SIZE bytes).
Dsp.EDGE_STRIDE = 32
Dsp.EDGE_SIZE = 32 * 34 + 4
function Dsp.edge(src: buffer, stride: number, base: number, x: number, y: number, w: number, h: number, pw: number, ph: number, out: buffer)
for j = 0, h do
local sy = y + j
sy = if sy < 0 then 0 elseif sy >= ph then ph - 1 else sy
local row = base + sy * stride
local o = j * 32
for i = 0, w do
local sx = x + i
sx = if sx < 0 then 0 elseif sx >= pw then pw - 1 else sx
writeu8(out, o + i, readu8(src, row + sx))
end
end
end