From d15fa26a43858f6e318eff643bf62549d28aa35f Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 13:56:27 -0500 Subject: [PATCH] ws-video2: codecs/video/Dsp (jrevdct IDCT bit-exact with ffmpeg -idct int, shared MC); Mpeg1 uses it MPEG-1 decode is now bit-exact with ffmpeg -idct int (was ~0.6 MAE). ~5% slower on luau (1695 vs 1790 fps at 320x240). The IJG jrevdct.c is IJG-licensed (ffmpeg LICENSE.md), credited in licenses/NOTICE.md. Co-Authored-By: Claude Opus 5.5 --- licenses/NOTICE.md | 1 + src/Formats/codecs/video/Dsp.luau | 693 ++++++++++++++++++++ src/Formats/codecs/video/Mpeg1/Decoder.luau | 204 +----- 3 files changed, 699 insertions(+), 199 deletions(-) create mode 100644 src/Formats/codecs/video/Dsp.luau diff --git a/licenses/NOTICE.md b/licenses/NOTICE.md index d61e1b4..76a1a69 100644 --- a/licenses/NOTICE.md +++ b/licenses/NOTICE.md @@ -37,6 +37,7 @@ | `src/Formats/codecs/Vp8/` inter frames (Decode: inter headers / modes / motion vectors / references, Predict, the inter tables in Tables) | Luau translations of libvpx 1.17 (WebM Project): vp8/decoder/decodeframe.c, decodemv.c, onyxd_if.c; vp8/common/findnearmv.c, reconinter.c, filter.c, vp8_loopfilter.c, entropymode.c, entropymv.c, modecont.c | BSD-3-Clause + patent grant: [libvpx-LICENSE.txt](libvpx-LICENSE.txt) | | `tests/fixtures/vp8video/vp80-*.ivf` | the VP8 test vectors of the libvpx test data (vp80-00-comprehensive-001..018 and others) | WebM Project, BSD-3-Clause + patent grant: [libvpx-LICENSE.txt](libvpx-LICENSE.txt) | | `src/Formats/codecs/H264/` (Tables: CAVLC / CABAC / deblocking / scaling constant tables of ITU-T H.264, extracted from openh264 sources; the decoder itself is written from the spec) | openh264 2.x (Cisco) | BSD-2-Clause: [openh264-LICENSE.txt](openh264-LICENSE.txt) | +| `src/Formats/codecs/video/Dsp.luau` (the 8x8 IDCT: jrevdct.c translated to Luau, branch tree and constants unchanged) | IJG jrevdct.c as shipped in ffmpeg (libavcodec/jrevdct.c; IJG-licensed, not LGPL: ffmpeg's LICENSE.md) | IJG: [libjpeg-README.ijg](libjpeg-README.ijg). This software is based in part on the work of the Independent JPEG Group. | | `tests/gen/h264_jvt` (downloaded at fixture time, not committed) | ITU-T H.264.1 / JVT conformance bitstreams (itu.int) | ITU-T test data | Ports of third-party code (minimp3, stb_vorbis, libopus, ...) add a row here and their upstream licence text in this diff --git a/src/Formats/codecs/video/Dsp.luau b/src/Formats/codecs/video/Dsp.luau new file mode 100644 index 0000000..f449f53 --- /dev/null +++ b/src/Formats/codecs/video/Dsp.luau @@ -0,0 +1,693 @@ +--!strict +--!native +--!optimize 2 +-- codecs/video/Dsp: the 8x8 IDCT (bit-exact with ffmpeg -idct int) and half-pel motion compensation of MPEG codecs. +-- Block DSP shared by the MPEG-style video codecs (MPEG-1 / MPEG-2, H.263 / Sorenson Spark): the IDCT put or added +-- into 8-bit planes, constant (DC-only) blocks, -- half-pel motion compensation four samples at a time. Native, never throws (the caller keeps offsets in range). +-- +-- The IDCT is the Independent JPEG Group's jrevdct.c (the LL&M integer IDCT, CONST_BITS 13, PASS1_BITS 2) as ffmpeg +-- ships it (libavcodec/jrevdct.c, IJG licence: licenses/libjpeg-README.ijg), translated mechanically: rows first, then +-- columns, with every sparse-input special case of the original kept, because several of them use merged constants +-- (FIX_1_306562965 for FIX_0_541196100 - FIX_1_847759065, ...) that round differently from the general case; that +-- is what makes ffmpeg's `-idct int` output reproducible here. Changes: Luau, i32 coefficients in natural order +-- (ffmpeg's LIBMPEG2 permutation is only a storage order), no int16 wrap of the intermediate values (identical for +-- coefficients in the MPEG range), and the put / add clamping fused into the last pass. +-- +-- 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) +local Dsp = {} + +local readu8 = buffer.readu8 +local writeu8 = buffer.writeu8 +local readu32 = buffer.readu32 +local writeu32 = buffer.writeu32 +local readi32 = buffer.readi32 +local writei32 = buffer.writei32 +local band = bit32.band +local bor = bit32.bor +local bxor = bit32.bxor +local rshift = bit32.rshift + +-- clamp to 0..255 by lookup: CLAMP[v + COFF] for v in [-COFF, COFF + 255]; the IDCT output of MPEG-range +-- coefficients is bounded by 64 * 2048 / 8 * sqrt(2)^2 < COFF, plus a prediction of up to 255 +local COFF = 40000 +local CLAMP = buffer.create(2 * COFF + 256) +for i = 0, 2 * COFF + 255 do + buffer.writeu8(CLAMP, i, math.clamp(i - COFF, 0, 255)) +end +Dsp.CLAMP = CLAMP +Dsp.COFF = COFF + +-- ------------------------------------------------------------------------------------------------ IDCT +function Dsp.idct(blk: buffer, dst: buffer, off: number, stride: number, add: boolean) + 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 + -- pass 1: rows, scaled up by 2^PASS1_BITS + for r = 0, 224, 32 do + local d0 = readi32(blk, r) + local d1 = readi32(blk, r + 4) + local d2 = readi32(blk, r + 8) + local d3 = readi32(blk, r + 12) + local d4 = readi32(blk, r + 16) + local d5 = readi32(blk, r + 20) + local d6 = readi32(blk, r + 24) + local d7 = readi32(blk, r + 28) + if d1 == 0 and d2 == 0 and d3 == 0 and d4 == 0 and d5 == 0 and d6 == 0 and d7 == 0 then + if d0 ~= 0 then + local v = d0 * 4 + writei32(blk, r, v) + writei32(blk, r + 4, v) + writei32(blk, r + 8, v) + writei32(blk, r + 12, v) + writei32(blk, r + 16, v) + writei32(blk, r + 20, v) + writei32(blk, r + 24, v) + writei32(blk, r + 28, v) + end + else + if d6 ~= 0 then + if d2 ~= 0 then + z1 = (d2 + d6) * 4433 + tmp2 = z1 + (-d6) * 15137 + tmp3 = z1 + (d2) * 6270 + tmp0 = (d0 + d4) * 8192 + tmp1 = (d0 - d4) * 8192 + tmp10 = tmp0 + tmp3 + tmp13 = tmp0 - tmp3 + tmp11 = tmp1 + tmp2 + tmp12 = tmp1 - tmp2 + else + tmp2 = (-d6) * 10703 + tmp3 = (d6) * 4433 + tmp0 = (d0 + d4) * 8192 + tmp1 = (d0 - d4) * 8192 + tmp10 = tmp0 + tmp3 + tmp13 = tmp0 - tmp3 + tmp11 = tmp1 + tmp2 + tmp12 = tmp1 - tmp2 + end + else + if d2 ~= 0 then + tmp2 = (d2) * 4433 + tmp3 = (d2) * 10703 + tmp0 = (d0 + d4) * 8192 + tmp1 = (d0 - d4) * 8192 + tmp10 = tmp0 + tmp3 + tmp13 = tmp0 - tmp3 + tmp11 = tmp1 + tmp2 + tmp12 = tmp1 - tmp2 + else + tmp13 = (d0 + d4) * 8192 + tmp10 = tmp13 + tmp12 = (d0 - d4) * 8192 + tmp11 = tmp12 + end + end + if d7 ~= 0 then + if d5 ~= 0 then + if d3 ~= 0 then + if d1 ~= 0 then + z1 = d7 + d1 + z2 = d5 + d3 + z3 = d7 + d3 + z4 = d5 + d1 + z5 = (z3 + z4) * 9633 + tmp0 = (d7) * 2446 + tmp1 = (d5) * 16819 + tmp2 = (d3) * 25172 + tmp3 = (d1) * 12299 + z1 = (-z1) * 7373 + z2 = (-z2) * 20995 + z3 = (-z3) * 16069 + z4 = (-z4) * 3196 + z3 += z5 + z4 += z5 + tmp0 += z1 + z3 + tmp1 += z2 + z4 + tmp2 += z2 + z3 + tmp3 += z1 + z4 + else + z2 = d5 + d3 + z3 = d7 + d3 + z5 = (z3 + d5) * 9633 + tmp0 = (d7) * 2446 + tmp1 = (d5) * 16819 + tmp2 = (d3) * 25172 + z1 = (-d7) * 7373 + z2 = (-z2) * 20995 + z3 = (-z3) * 16069 + z4 = (-d5) * 3196 + z3 += z5 + z4 += z5 + tmp0 += z1 + z3 + tmp1 += z2 + z4 + tmp2 += z2 + z3 + tmp3 = z1 + z4 + end + else + if d1 ~= 0 then + z1 = d7 + d1 + z4 = d5 + d1 + z5 = (d7 + z4) * 9633 + tmp0 = (d7) * 2446 + tmp1 = (d5) * 16819 + tmp3 = (d1) * 12299 + z1 = (-z1) * 7373 + z2 = (-d5) * 20995 + z3 = (-d7) * 16069 + z4 = (-z4) * 3196 + z3 += z5 + z4 += z5 + tmp0 += z1 + z3 + tmp1 += z2 + z4 + tmp2 = z2 + z3 + tmp3 += z1 + z4 + else + tmp0 = (-d7) * 4926 + z1 = (-d7) * 7373 + z3 = (-d7) * 16069 + tmp1 = (-d5) * 4176 + z2 = (-d5) * 20995 + z4 = (-d5) * 3196 + z5 = (d5 + d7) * 9633 + z3 += z5 + z4 += z5 + tmp0 += z3 + tmp1 += z4 + tmp2 = z2 + z3 + tmp3 = z1 + z4 + end + end + else + if d3 ~= 0 then + if d1 ~= 0 then + z1 = d7 + d1 + z3 = d7 + d3 + z5 = (z3 + d1) * 9633 + tmp0 = (d7) * 2446 + tmp2 = (d3) * 25172 + tmp3 = (d1) * 12299 + z1 = (-z1) * 7373 + z2 = (-d3) * 20995 + z3 = (-z3) * 16069 + z4 = (-d1) * 3196 + z3 += z5 + z4 += z5 + tmp0 += z1 + z3 + tmp1 = z2 + z4 + tmp2 += z2 + z3 + tmp3 += z1 + z4 + else + z3 = d7 + d3 + tmp0 = (-d7) * 4926 + z1 = (-d7) * 7373 + tmp2 = (d3) * 4176 + z2 = (-d3) * 20995 + z5 = (z3) * 9633 + z3 = (-z3) * 6436 + tmp0 += z3 + tmp1 = z2 + z5 + tmp2 += z3 + tmp3 = z1 + z5 + end + else + if d1 ~= 0 then + z1 = d7 + d1 + z5 = (z1) * 9633 + z1 = (z1) * 2260 + z3 = (-d7) * 16069 + tmp0 = (-d7) * 13623 + z4 = (-d1) * 3196 + tmp3 = (d1) * 9102 + tmp0 += z1 + tmp1 = z4 + z5 + tmp2 = z3 + z5 + tmp3 += z1 + else + tmp0 = (-d7) * 11363 + tmp1 = (d7) * 9633 + tmp2 = (-d7) * 6436 + tmp3 = (d7) * 2260 + end + end + end + else + if d5 ~= 0 then + if d3 ~= 0 then + if d1 ~= 0 then + z2 = d5 + d3 + z4 = d5 + d1 + z5 = (d3 + z4) * 9633 + tmp1 = (d5) * 16819 + tmp2 = (d3) * 25172 + tmp3 = (d1) * 12299 + z1 = (-d1) * 7373 + z2 = (-z2) * 20995 + z3 = (-d3) * 16069 + z4 = (-z4) * 3196 + z3 += z5 + z4 += z5 + tmp0 = z1 + z3 + tmp1 += z2 + z4 + tmp2 += z2 + z3 + tmp3 += z1 + z4 + else + z2 = d5 + d3 + z5 = (z2) * 9633 + tmp1 = (d5) * 13623 + z4 = (-d5) * 3196 + z2 = (-z2) * 11363 + tmp2 = (d3) * 9102 + z3 = (-d3) * 16069 + tmp0 = z3 + z5 + tmp1 += z2 + tmp2 += z2 + tmp3 = z4 + z5 + end + else + if d1 ~= 0 then + z4 = d5 + d1 + z5 = (z4) * 9633 + z1 = (-d1) * 7373 + tmp3 = (d1) * 4926 + tmp1 = (-d5) * 4176 + z2 = (-d5) * 20995 + z4 = (z4) * 6436 + tmp0 = z1 + z5 + tmp1 += z4 + tmp2 = z2 + z5 + tmp3 += z4 + else + tmp0 = (d5) * 9633 + tmp1 = (d5) * 2260 + tmp2 = (-d5) * 11363 + tmp3 = (d5) * 6436 + end + end + else + 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 + else + if 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 + end + end + end + writei32(blk, r, (tmp10 + tmp3 + 1024) // 2048) + writei32(blk, r + 28, (tmp10 - tmp3 + 1024) // 2048) + writei32(blk, r + 4, (tmp11 + tmp2 + 1024) // 2048) + writei32(blk, r + 24, (tmp11 - tmp2 + 1024) // 2048) + writei32(blk, r + 8, (tmp12 + tmp1 + 1024) // 2048) + writei32(blk, r + 20, (tmp12 - tmp1 + 1024) // 2048) + writei32(blk, r + 12, (tmp13 + tmp0 + 1024) // 2048) + writei32(blk, r + 16, (tmp13 - tmp0 + 1024) // 2048) + end + end + -- pass 2: columns, descaled by 2^(CONST_BITS + PASS1_BITS + 3) + 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) + local d4 = readi32(blk, c + 128) + local d5 = readi32(blk, c + 160) + local d6 = readi32(blk, c + 192) + local d7 = readi32(blk, c + 224) + if d6 ~= 0 then + if d2 ~= 0 then + z1 = (d2 + d6) * 4433 + tmp2 = z1 + (-d6) * 15137 + tmp3 = z1 + (d2) * 6270 + tmp0 = (d0 + d4) * 8192 + tmp1 = (d0 - d4) * 8192 + tmp10 = tmp0 + tmp3 + tmp13 = tmp0 - tmp3 + tmp11 = tmp1 + tmp2 + tmp12 = tmp1 - tmp2 + else + tmp2 = (-d6) * 10703 + tmp3 = (d6) * 4433 + tmp0 = (d0 + d4) * 8192 + tmp1 = (d0 - d4) * 8192 + tmp10 = tmp0 + tmp3 + tmp13 = tmp0 - tmp3 + tmp11 = tmp1 + tmp2 + tmp12 = tmp1 - tmp2 + end + else + if d2 ~= 0 then + tmp2 = (d2) * 4433 + tmp3 = (d2) * 10703 + tmp0 = (d0 + d4) * 8192 + tmp1 = (d0 - d4) * 8192 + tmp10 = tmp0 + tmp3 + tmp13 = tmp0 - tmp3 + tmp11 = tmp1 + tmp2 + tmp12 = tmp1 - tmp2 + else + tmp13 = (d0 + d4) * 8192 + tmp10 = tmp13 + tmp12 = (d0 - d4) * 8192 + tmp11 = tmp12 + end + end + if d7 ~= 0 then + if d5 ~= 0 then + if d3 ~= 0 then + if d1 ~= 0 then + z1 = d7 + d1 + z2 = d5 + d3 + z3 = d7 + d3 + z4 = d5 + d1 + z5 = (z3 + z4) * 9633 + tmp0 = (d7) * 2446 + tmp1 = (d5) * 16819 + tmp2 = (d3) * 25172 + tmp3 = (d1) * 12299 + z1 = (-z1) * 7373 + z2 = (-z2) * 20995 + z3 = (-z3) * 16069 + z4 = (-z4) * 3196 + z3 += z5 + z4 += z5 + tmp0 += z1 + z3 + tmp1 += z2 + z4 + tmp2 += z2 + z3 + tmp3 += z1 + z4 + else + z2 = d5 + d3 + z3 = d7 + d3 + z5 = (z3 + d5) * 9633 + tmp0 = (d7) * 2446 + tmp1 = (d5) * 16819 + tmp2 = (d3) * 25172 + z1 = (-d7) * 7373 + z2 = (-z2) * 20995 + z3 = (-z3) * 16069 + z4 = (-d5) * 3196 + z3 += z5 + z4 += z5 + tmp0 += z1 + z3 + tmp1 += z2 + z4 + tmp2 += z2 + z3 + tmp3 = z1 + z4 + end + else + if d1 ~= 0 then + z1 = d7 + d1 + z3 = d7 + z4 = d5 + d1 + z5 = (z3 + z4) * 9633 + tmp0 = (d7) * 2446 + tmp1 = (d5) * 16819 + tmp3 = (d1) * 12299 + z1 = (-z1) * 7373 + z2 = (-d5) * 20995 + z3 = (-d7) * 16069 + z4 = (-z4) * 3196 + z3 += z5 + z4 += z5 + tmp0 += z1 + z3 + tmp1 += z2 + z4 + tmp2 = z2 + z3 + tmp3 += z1 + z4 + else + tmp0 = (-d7) * 4926 + z1 = (-d7) * 7373 + z3 = (-d7) * 16069 + tmp1 = (-d5) * 4176 + z2 = (-d5) * 20995 + z4 = (-d5) * 3196 + z5 = (d5 + d7) * 9633 + z3 += z5 + z4 += z5 + tmp0 += z3 + tmp1 += z4 + tmp2 = z2 + z3 + tmp3 = z1 + z4 + end + end + else + if d3 ~= 0 then + if d1 ~= 0 then + z1 = d7 + d1 + z3 = d7 + d3 + z5 = (z3 + d1) * 9633 + tmp0 = (d7) * 2446 + tmp2 = (d3) * 25172 + tmp3 = (d1) * 12299 + z1 = (-z1) * 7373 + z2 = (-d3) * 20995 + z3 = (-z3) * 16069 + z4 = (-d1) * 3196 + z3 += z5 + z4 += z5 + tmp0 += z1 + z3 + tmp1 = z2 + z4 + tmp2 += z2 + z3 + tmp3 += z1 + z4 + else + z3 = d7 + d3 + tmp0 = (-d7) * 4926 + z1 = (-d7) * 7373 + tmp2 = (d3) * 4176 + z2 = (-d3) * 20995 + z5 = (z3) * 9633 + z3 = (-z3) * 6436 + tmp0 += z3 + tmp1 = z2 + z5 + tmp2 += z3 + tmp3 = z1 + z5 + end + else + if d1 ~= 0 then + z1 = d7 + d1 + z5 = (z1) * 9633 + z1 = (z1) * 2260 + z3 = (-d7) * 16069 + tmp0 = (-d7) * 13623 + z4 = (-d1) * 3196 + tmp3 = (d1) * 9102 + tmp0 += z1 + tmp1 = z4 + z5 + tmp2 = z3 + z5 + tmp3 += z1 + else + tmp0 = (-d7) * 11363 + tmp1 = (d7) * 9633 + tmp2 = (-d7) * 6436 + tmp3 = (d7) * 2260 + end + end + end + else + if d5 ~= 0 then + if d3 ~= 0 then + if d1 ~= 0 then + z2 = d5 + d3 + z4 = d5 + d1 + z5 = (d3 + z4) * 9633 + tmp1 = (d5) * 16819 + tmp2 = (d3) * 25172 + tmp3 = (d1) * 12299 + z1 = (-d1) * 7373 + z2 = (-z2) * 20995 + z3 = (-d3) * 16069 + z4 = (-z4) * 3196 + z3 += z5 + z4 += z5 + tmp0 = z1 + z3 + tmp1 += z2 + z4 + tmp2 += z2 + z3 + tmp3 += z1 + z4 + else + z2 = d5 + d3 + z5 = (z2) * 9633 + tmp1 = (d5) * 13623 + z4 = (-d5) * 3196 + z2 = (-z2) * 11363 + tmp2 = (d3) * 9102 + z3 = (-d3) * 16069 + tmp0 = z3 + z5 + tmp1 += z2 + tmp2 += z2 + tmp3 = z4 + z5 + end + else + if d1 ~= 0 then + z4 = d5 + d1 + z5 = (z4) * 9633 + z1 = (-d1) * 7373 + tmp3 = (d1) * 4926 + tmp1 = (-d5) * 4176 + z2 = (-d5) * 20995 + z4 = (z4) * 6436 + tmp0 = z1 + z5 + tmp1 += z4 + tmp2 = z2 + z5 + tmp3 += z4 + else + tmp0 = (d5) * 9633 + tmp1 = (d5) * 2260 + tmp2 = (-d5) * 11363 + tmp3 = (d5) * 6436 + end + end + else + 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 + else + if 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 + 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 + 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) + 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 + end +end + +-- ------------------------------------------------------------------------------------------------ blocks +-- an 8x8 block of the constant v put into dst (add = false) or added to it, clamped (a DC-only block: the IDCT +-- of a lone DC coefficient dc is (dc + 4) >> 3 everywhere) +function Dsp.fill(dst: buffer, off: number, stride: number, v: number, add: boolean) + if not add then + local c = readu8(CLAMP, v + COFF) * 0x01010101 + for _ = 1, 8 do + writeu32(dst, off, c) + writeu32(dst, off + 4, c) + off += stride + end + else + for _ = 1, 8 do + for i = off, off + 7 do + writeu8(dst, i, readu8(CLAMP, readu8(dst, i) + v + COFF)) + end + off += stride + end + end +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 + 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) + 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 + stride), readu32(src, sOff + i + stride + 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) + end + if avg then + local q = readu32(dst, dOff + i) + p = bor(p, q) - rshift(band(bxor(p, q), 0xFEFEFEFE), 1) + end + writeu32(dst, dOff + i, p) + end + sOff += stride + dOff += stride + end +end + +return Dsp diff --git a/src/Formats/codecs/video/Mpeg1/Decoder.luau b/src/Formats/codecs/video/Mpeg1/Decoder.luau index 5fcdb1f..0431576 100644 --- a/src/Formats/codecs/video/Mpeg1/Decoder.luau +++ b/src/Formats/codecs/video/Mpeg1/Decoder.luau @@ -25,6 +25,7 @@ -- vectors are clamped to the reference picture, and bad data ends the slice instead of raising. local V = require("@formats/codecs/video/Mpeg1/Vlc") local T = require("@formats/codecs/video/Mpeg1/Tables") +local Dsp = require("@formats/codecs/video/Dsp") local readu8 = buffer.readu8 local writeu8 = buffer.writeu8 @@ -82,205 +83,10 @@ local SUB, ESC, EOB = V.SUB, V.ESC, V.EOB local MBTYPE = { V.MBTYPE_I, V.MBTYPE_P, V.MBTYPE_B } local MBTYPE_BITS = { V.MBTYPE_I_BITS, V.MBTYPE_P_BITS, V.MBTYPE_B_BITS } --- Integer IDCT (LL&M factorisation as in the IJG "islow" IDCT: CONST_BITS 13, PASS1_BITS 2) of the 64 i32 --- coefficients in blk (natural order), written to dst (put) or added to it (add), clamped to 0..255. -local function idct(blk: buffer, ws: buffer, dst: buffer, off: number, stride: number, add: boolean) - -- pass 1: columns, into ws scaled by 4 - 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) - local d4 = readi32(blk, c + 128) - local d5 = readi32(blk, c + 160) - local d6 = readi32(blk, c + 192) - local d7 = readi32(blk, c + 224) - if d1 == 0 and d2 == 0 and d3 == 0 and d4 == 0 and d5 == 0 and d6 == 0 and d7 == 0 then - local dc = d0 * 4 - writei32(ws, c, dc) - writei32(ws, c + 32, dc) - writei32(ws, c + 64, dc) - writei32(ws, c + 96, dc) - writei32(ws, c + 128, dc) - writei32(ws, c + 160, dc) - writei32(ws, c + 192, dc) - writei32(ws, c + 224, dc) - else - -- even part - local z1 = (d2 + d6) * 4433 - local t2 = z1 - d6 * 15137 - local t3 = z1 + d2 * 6270 - local t0 = (d0 + d4) * 8192 - local t1 = (d0 - d4) * 8192 - local t10 = t0 + t3 - local t13 = t0 - t3 - local t11 = t1 + t2 - local t12 = t1 - t2 - -- odd part - local o0, o1, o2, o3 = d7, d5, d3, d1 - local y1 = o0 + o3 - local y2 = o1 + o2 - local y3 = o0 + o2 - local y4 = o1 + o3 - local z5 = (y3 + y4) * 9633 - o0 *= 2446 - o1 *= 16819 - o2 *= 25172 - o3 *= 12299 - y1 *= -7373 - y2 *= -20995 - y3 = y3 * -16069 + z5 - y4 = y4 * -3196 + z5 - o0 += y1 + y3 - o1 += y2 + y4 - o2 += y2 + y3 - o3 += y1 + y4 - writei32(ws, c, (t10 + o3 + 1024) // 2048) - writei32(ws, c + 224, (t10 - o3 + 1024) // 2048) - writei32(ws, c + 32, (t11 + o2 + 1024) // 2048) - writei32(ws, c + 192, (t11 - o2 + 1024) // 2048) - writei32(ws, c + 64, (t12 + o1 + 1024) // 2048) - writei32(ws, c + 160, (t12 - o1 + 1024) // 2048) - writei32(ws, c + 96, (t13 + o0 + 1024) // 2048) - writei32(ws, c + 128, (t13 - o0 + 1024) // 2048) - end - end - -- pass 2: rows, descaled by 2^18 (13 + 2 + 3), into the picture - for r = 0, 224, 32 do - local d0 = readi32(ws, r) - local d1 = readi32(ws, r + 4) - local d2 = readi32(ws, r + 8) - local d3 = readi32(ws, r + 12) - local d4 = readi32(ws, r + 16) - local d5 = readi32(ws, r + 20) - local d6 = readi32(ws, r + 24) - local d7 = readi32(ws, r + 28) - local p0, p1, p2, p3, p4, p5, p6, p7 - if d1 == 0 and d2 == 0 and d3 == 0 and d4 == 0 and d5 == 0 and d6 == 0 and d7 == 0 then - local v = (d0 + 16) // 32 - p0, p1, p2, p3, p4, p5, p6, p7 = v, v, v, v, v, v, v, v - else - local z1 = (d2 + d6) * 4433 - local t2 = z1 - d6 * 15137 - local t3 = z1 + d2 * 6270 - local t0 = (d0 + d4) * 8192 - local t1 = (d0 - d4) * 8192 - local t10 = t0 + t3 + 131072 - local t13 = t0 - t3 + 131072 - local t11 = t1 + t2 + 131072 - local t12 = t1 - t2 + 131072 - local o0, o1, o2, o3 = d7, d5, d3, d1 - local y1 = o0 + o3 - local y2 = o1 + o2 - local y3 = o0 + o2 - local y4 = o1 + o3 - local z5 = (y3 + y4) * 9633 - o0 *= 2446 - o1 *= 16819 - o2 *= 25172 - o3 *= 12299 - y1 *= -7373 - y2 *= -20995 - y3 = y3 * -16069 + z5 - y4 = y4 * -3196 + z5 - o0 += y1 + y3 - o1 += y2 + y4 - o2 += y2 + y3 - o3 += y1 + y4 - p0 = (t10 + o3) // 262144 - p7 = (t10 - o3) // 262144 - p1 = (t11 + o2) // 262144 - p6 = (t11 - o2) // 262144 - p2 = (t12 + o1) // 262144 - p5 = (t12 - o1) // 262144 - p3 = (t13 + o0) // 262144 - p4 = (t13 - o0) // 262144 - end - 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) - end - writeu32( - dst, - off, - readu8(CLAMP, p0 + COFF) - + readu8(CLAMP, p1 + COFF) * 256 - + readu8(CLAMP, p2 + COFF) * 65536 - + readu8(CLAMP, p3 + COFF) * 16777216 - ) - writeu32( - dst, - off + 4, - readu8(CLAMP, p4 + COFF) - + readu8(CLAMP, p5 + COFF) * 256 - + readu8(CLAMP, p6 + COFF) * 65536 - + readu8(CLAMP, p7 + COFF) * 16777216 - ) - off += stride - end -end +local idct = Dsp.idct +local fillBlock = Dsp.fill +local mc = Dsp.mc D.idct = idct - --- a constant 8x8 block (DC only): put or add v -local function fillBlock(dst: buffer, off: number, stride: number, v: number, add: boolean) - if not add then - local c = readu8(CLAMP, v + COFF) * 0x01010101 - for _ = 1, 8 do - writeu32(dst, off, c) - writeu32(dst, off + 4, c) - off += stride - end - else - for _ = 1, 8 do - for i = off, off + 7 do - writeu8(dst, i, readu8(CLAMP, readu8(dst, i) + v + COFF)) - end - off += stride - end - end -end - --- Motion-compensated prediction of a size x size block (16 luma, 8 chroma) from src at sOff to dst at dOff with --- half-pel flags hx / hy; avg averages into what dst holds (the second prediction of a B macroblock). Four bytes at --- a time: avg(a, b) rounding up = (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. -local function 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 - 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) - 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 + stride), readu32(src, sOff + i + stride + 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) - end - if avg then - local q = readu32(dst, dOff + i) - p = bor(p, q) - rshift(band(bxor(p, q), 0xFEFEFEFE), 1) - end - writeu32(dst, dOff + i, p) - end - sOff += stride - dOff += stride - end -end D.mc = mc -- YCbCr 4:2:0 planes (coded strides) -> RGBA8 (alpha 255), w x h, nearest chroma like ffmpeg's default swscale path @@ -775,7 +581,7 @@ function D.new(maxPixels: number?): Core fillBlock(dst, off, stride, (readi32(blk, 0) + 4) // 8, not intra) writei32(blk, 0, 0) else - idct(blk, ws, dst, off, stride, not intra) + idct(blk, dst, off, stride, not intra) buffer.fill(blk, 0, 0, 256) end return true