From 1013925a85fe3c3435cac17823f60a58e0907b1d Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:43:22 -0500 Subject: [PATCH] ws-video2-theora: Theora speed (fused prediction + IDCT add, inlined token decode, table-driven tokens), bench lines, guide section tools/bench.luau: a ws-video2-theora block (320x240 testsrc2 / mandelbrot, decode and decode + image()). docs/guide/video.md: an Ogg Theora section (tested snippet; inputs from the new tools/fixtures/docs_theora.py) and the formats / writers tables. --- docs/guide/video.md | 28 +- src/Formats/codecs/video/Theora/Decoder.luau | 462 ++++++++++--------- src/Formats/codecs/video/Theora/Header.luau | 2 +- tools/bench.luau | 39 ++ tools/fixtures/docs_theora.py | 20 + tools/fixtures/theora.py | 13 +- 6 files changed, 342 insertions(+), 222 deletions(-) create mode 100644 tools/fixtures/docs_theora.py diff --git a/docs/guide/video.md b/docs/guide/video.md index 99e0395..6d2ed39 100644 --- a/docs/guide/video.md +++ b/docs/guide/video.md @@ -179,6 +179,7 @@ Images and animated images save as videos too. | Destination | Video | Audio | |---|---|---| | webm | VP8 / VP9 / AV1 copied (VP8 is decode only, so the source must be WebM-compatible) | Opus / Vorbis copied | +| ogv | Theora copied (the source must be Theora: ogv or mkv) | Vorbis / Opus copied | | mkv | any codec copied, else MJPEG | copied, else PCM s16 | | mp4, mov | H.264 / HEVC / AV1 / MJPEG / MPEG-4 copied, else MJPEG | AAC / ALAC / Opus / FLAC / MP3 copied; PCM in MOV only | | avi | MJPEG copied, else MJPEG encoded (`quality`) | PCM s16 | @@ -235,6 +236,30 @@ assert(C.register({ assert(C.fromFourcc("SLID") == "solid" and C.canDecode("solid")) ``` +## Ogg Theora + +Ogg video (`.ogv`, or `.ogg` with a Theora stream) opens like any other container: the `ogv` format demuxes every +Ogg stream, Theora video decodes bit-exactly (as libtheora / ffmpeg) in all three pixel formats (4:2:2 and 4:4:4 +chroma is averaged down to yuv420p), and a Vorbis, Opus or FLAC track becomes `v.audio`. Plain Ogg Vorbis / Opus / +FLAC files stay audio. Theora in Matroska (`V_THEORA`) decodes too, and an Ogg Theora video remuxes to ogv or mkv +(there is no Theora encoder). + +```luau +local v = assert(F.video.open(ogvBytes)) -- Theora + Vorbis +assert(v.format == "ogv" and v.codec == "theora" and v.width == 64 and v.height == 48) +assert(v.frameRate == 10 and v.audio ~= nil) +local n = 0 +while v:nextFrame() do n += 1 end -- dropped frames repeat the previous picture +assert(n == 10) +local Theora = F.format("ogv") -- the format module: sniff / demux / open / encode +local d = assert(Theora.demux(F.Bytes.reader(ogvBytes))) +local vt = d:track("video") +assert(vt.codec == "theora" and vt.timescale == 10) -- packet times are frame numbers here +v:reset() +local mkv = assert(F.save(v, nil, { format = "mkv" })) -- Theora + Vorbis copied into Matroska +assert(F.video.open(mkv).codec == "theora") +``` + ## Playing in a game loop Decode only the frames that are due, and stop when this frame's time budget is used up; a slow frame then makes @@ -279,7 +304,8 @@ editable:WritePixelsBuffer(Vector2.zero, editable.Size, v:toRGBA(frame).pixels) | Format | Read | Write | Video codecs (decode) | |---|---|---|---| | mp4 / mov / m4v / 3gp | yes | yes | H.264, MJPEG, MPEG-1 | -| mkv / webm | yes | yes | VP8, MJPEG, MPEG-1, raw (+ H.264) | +| mkv / webm | yes | yes | VP8, MJPEG, MPEG-1, raw, Theora (+ H.264) | +| ogv (Ogg Theora) | yes | yes (Theora + Vorbis / Opus copied) | Theora | | avi | yes | yes | MJPEG, MPEG-1, raw (+ H.264) | | mpegts, mpegps | yes | - | MPEG-1, H.264 | | y4m | yes | yes | raw | diff --git a/src/Formats/codecs/video/Theora/Decoder.luau b/src/Formats/codecs/video/Theora/Decoder.luau index d29b8cf..aad8f1c 100644 --- a/src/Formats/codecs/video/Theora/Decoder.luau +++ b/src/Formats/codecs/video/Theora/Decoder.luau @@ -24,9 +24,10 @@ local writeu8 = buffer.writeu8 local readu16 = buffer.readu16 local readi16 = buffer.readi16 local writei16 = buffer.writei16 -local readi32 = buffer.readi32 -local writei32 = buffer.writei32 local readu32 = buffer.readu32 +local writeu32 = buffer.writeu32 +local band = bit32.band +local bxor = bit32.bxor local bcopy = buffer.copy local bfill = buffer.fill local lshift = bit32.lshift @@ -93,10 +94,56 @@ do end -- DCT tokens: extra bits per token -local TOKEN_EXTRA = { - [0] = 0, 0, 0, 2, 3, 4, 12, 3, 6, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 4, 5, 6, 10, 1, 1, 1, 1, 1, 3, 4, 2, 3, +local TOKEN_EXTRA = { -- [token + 1] + 0, 0, 0, 2, 3, 4, 12, 3, 6, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 4, 5, 6, 10, 1, 1, 1, 1, 1, 3, 4, 2, 3, } +-- the zero-run / coefficient tokens (7..31) by token * 1024 + extra bits + 1: run * 4096 + value + 2048, where run is +-- the zeros before the value (value 0: a pure zero run of run + 1) +local TOKEN_VALUE = table.create(32 * 1024, 2048) +for tok = 7, 31 do + local ex = TOKEN_EXTRA[tok + 1] + for eb = 0, 2 ^ ex - 1 do + local v, r = 0, 0 + if tok < 9 then + r = eb -- zero runs: eb + 1 zeros + elseif tok < 13 then + v = if tok == 9 then 1 elseif tok == 10 then -1 elseif tok == 11 then 2 else -2 + elseif tok < 17 then + v = if eb == 1 then -(tok - 10) else tok - 10 + elseif tok < 23 then + local half = 2 ^ (ex - 1) -- the top extra bit is the sign + local base = if tok == 17 then 7 elseif tok == 18 then 9 elseif tok == 19 then 13 elseif tok == 20 then 21 elseif tok == 21 then 37 else 69 + v = base + eb % half + if eb >= half then + v = -v + end + elseif tok < 28 then + r = tok - 22 + v = if eb == 1 then -1 else 1 + elseif tok == 28 then + r = 6 + eb % 4 + v = if eb >= 4 then -1 else 1 + elseif tok == 29 then + r = 10 + eb % 8 + v = if eb >= 8 then -1 else 1 + elseif tok == 30 then + r = 1 + v = 2 + eb % 2 + if eb >= 2 then + v = -v + end + else + r = 2 + eb % 2 + v = 2 + (eb // 2) % 2 + if eb >= 4 then + v = -v + end + end + TOKEN_VALUE[tok * 1024 + eb + 1] = r * 4096 + v + 2048 + end +end + -- natural index of zig-zag position (1-based by position + 1) local ZIGZAG = Header.ZIGZAG @@ -158,9 +205,8 @@ export type State = { lflim: { number }, huff: { number }, roots: { number }, - blk: buffer, -- i32 x 64 work blocks + blk: buffer, -- i16 x 64 work blocks tmp: buffer, - res: buffer, bv: { number }, -- the loop filter's bounding values for the frame, [(f + 4) >> 3 + 129] keyframes: number, } @@ -191,9 +237,8 @@ function D.new(info: Header.Info, setup: Header.Setup): State lflim = setup.lflim, huff = setup.huff, roots = setup.roots, - blk = buffer.create(256), - tmp = buffer.create(256), - res = buffer.create(256), + blk = buffer.create(128), + tmp = buffer.create(128), bv = table.create(257, 0), keyframes = 0, } @@ -366,23 +411,24 @@ local function idct8(x0: number, x1: number, x2: number, x3: number, x4: number, return t0 + t7, t1 + t6, t2 + t5, t3 + t4, t3 - t4, t2 - t5, t1 - t6, t0 - t7 end --- 2-D IDCT of blk (i16, natural order) into res (i32 residuals). Like libtheora, the dequantised coefficients and --- the first pass's outputs are 16-bit (the i16 buffers wrap them), which also bounds every residual to +-24000. -local function idct(blk: buffer, tmp: buffer, res: buffer) +-- the first IDCT pass: the rows of blk (i16, natural order) into the columns of tmp (i16: like libtheora, the +-- dequantised coefficients and these intermediate values are 16-bit, which also bounds every residual) +local function idctRows(blk: buffer, tmp: buffer) for i = 0, 7 do local o = i * 16 local x0, x1, x2, x3 = readi16(blk, o), readi16(blk, o + 2), readi16(blk, o + 4), readi16(blk, o + 6) local x4, x5, x6, x7 = readi16(blk, o + 8), readi16(blk, o + 10), readi16(blk, o + 12), readi16(blk, o + 14) local c = i * 2 - if x0 == 0 and x1 == 0 and x2 == 0 and x3 == 0 and x4 == 0 and x5 == 0 and x6 == 0 and x7 == 0 then - writei16(tmp, c, 0) - writei16(tmp, c + 16, 0) - writei16(tmp, c + 32, 0) - writei16(tmp, c + 48, 0) - writei16(tmp, c + 64, 0) - writei16(tmp, c + 80, 0) - writei16(tmp, c + 96, 0) - writei16(tmp, c + 112, 0) + if x1 == 0 and x2 == 0 and x3 == 0 and x4 == 0 and x5 == 0 and x6 == 0 and x7 == 0 then + local y = (C4 * x0) // 65536 + writei16(tmp, c, y) + writei16(tmp, c + 16, y) + writei16(tmp, c + 32, y) + writei16(tmp, c + 48, y) + writei16(tmp, c + 64, y) + writei16(tmp, c + 80, y) + writei16(tmp, c + 96, y) + writei16(tmp, c + 112, y) else local y0, y1, y2, y3, y4, y5, y6, y7 = idct8(x0, x1, x2, x3, x4, x5, x6, x7) writei16(tmp, c, y0) @@ -395,20 +441,29 @@ local function idct(blk: buffer, tmp: buffer, res: buffer) writei16(tmp, c + 112, y7) end end - for i = 0, 7 do - local o = i * 16 - local x0, x1, x2, x3 = readi16(tmp, o), readi16(tmp, o + 2), readi16(tmp, o + 4), readi16(tmp, o + 6) - local x4, x5, x6, x7 = readi16(tmp, o + 8), readi16(tmp, o + 10), readi16(tmp, o + 12), readi16(tmp, o + 14) - local y0, y1, y2, y3, y4, y5, y6, y7 = idct8(x0, x1, x2, x3, x4, x5, x6, x7) - local c = i * 4 - writei32(res, c, (y0 + 8) // 16) - writei32(res, c + 32, (y1 + 8) // 16) - writei32(res, c + 64, (y2 + 8) // 16) - writei32(res, c + 96, (y3 + 8) // 16) - writei32(res, c + 128, (y4 + 8) // 16) - writei32(res, c + 160, (y5 + 8) // 16) - writei32(res, c + 192, (y6 + 8) // 16) - writei32(res, c + 224, (y7 + 8) // 16) +end + +-- the second pass, added to the prediction already in dst: column c of the block is row c of tmp +local function idctAdd(tmp: buffer, dst: buffer, pos: number, stride: number) + for c = 0, 7 do + local o = c * 16 + local y0, y1, y2, y3, y4, y5, y6, y7 = idct8(readi16(tmp, o), readi16(tmp, o + 2), readi16(tmp, o + 4), readi16(tmp, o + 6), readi16(tmp, o + 8), readi16(tmp, o + 10), readi16(tmp, o + 12), readi16(tmp, o + 14)) + local p = pos + c + writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y0 + 8) // 16 + COFF)) + p += stride + writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y1 + 8) // 16 + COFF)) + p += stride + writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y2 + 8) // 16 + COFF)) + p += stride + writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y3 + 8) // 16 + COFF)) + p += stride + writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y4 + 8) // 16 + COFF)) + p += stride + writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y5 + 8) // 16 + COFF)) + p += stride + writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y6 + 8) // 16 + COFF)) + p += stride + writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y7 + 8) // 16 + COFF)) end end @@ -426,7 +481,8 @@ local function mvSplit(d: number, q: boolean): (number, number) return a, b end --- reconstruct one coded fragment into dst (the SELF reference planes) +-- reconstruct one coded fragment into dst (the SELF reference planes): the prediction (128 for intra blocks, the +-- reference block or the average of two for inter ones) goes to dst first, then the residual is added local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { buffer }) local pli = st.fpl[fi] local mode = st.fmode[fi] @@ -435,12 +491,20 @@ local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { bu local dcq = readu16(dq, (((qis[1] * 3 + pli) * 2 + qti) * 64) * 2) local dc = st.dcv[fi] local last = st.last[fi] - local res = st.res - local flat = false + local stride = st.stride[pli + 1] + local dst = dstPlanes[pli + 1] + local pos = st.fpos[fi] + local ri = st.refi[fi] local fv = 0 if last == 0 then - flat = true fv = wrap16((dc * dcq + 15) // 32) + if ri == SELF then + local v = readu8(CLAMP, fv + 128 + COFF) + for y = 0, 7 do + bfill(dst, pos + y * stride, v, 8) + end + return + end else local blk = st.blk bfill(blk, 0, 0, 128) @@ -456,50 +520,44 @@ local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { bu end end st.last[fi] = 0 - idct(blk, st.tmp, res) + idctRows(blk, st.tmp) end - local stride = st.stride[pli + 1] - local dst = dstPlanes[pli + 1] - local pos = st.fpos[fi] - local ri = st.refi[fi] + -- the prediction if ri == SELF then - if flat then - local v = readu8(CLAMP, fv + 128 + COFF) + for y = 0, 7 do + bfill(dst, pos + y * stride, 128, 8) + end + else + local ref = st.refs[if ri == GOLD then st.gold else st.prev][pli + 1] + local mx, mx2 = mvSplit(st.mvx[fi], pli > 0 and st.hdec == 1) + local my, my2 = mvSplit(st.mvy[fi], pli > 0 and st.vdec == 1) + local s1 = pos + my * stride + mx + if mx2 ~= 0 or my2 ~= 0 then + -- (a + b) >> 1 per byte, four at a time + local s2 = s1 + my2 * stride + mx2 for y = 0, 7 do - bfill(dst, pos + y * stride, v, 8) + local d, a, b = pos + y * stride, s1 + y * stride, s2 + y * stride + local u, v = readu32(ref, a), readu32(ref, b) + writeu32(dst, d, band(u, v) + rshift(band(bxor(u, v), 0xFEFEFEFE), 1)) + u, v = readu32(ref, a + 4), readu32(ref, b + 4) + writeu32(dst, d + 4, band(u, v) + rshift(band(bxor(u, v), 0xFEFEFEFE), 1)) end else for y = 0, 7 do - local d, s = pos + y * stride, y * 32 - for x = 0, 7 do - writeu8(dst, d + x, readu8(CLAMP, readi32(res, s + x * 4) + 128 + COFF)) - end + local d, a = pos + y * stride, s1 + y * stride + writeu32(dst, d, readu32(ref, a)) + writeu32(dst, d + 4, readu32(ref, a + 4)) end end - return end - local ref = st.refs[if ri == GOLD then st.gold else st.prev][pli + 1] - local qx = pli > 0 and st.hdec == 1 - local qy = pli > 0 and st.vdec == 1 - local mx, mx2 = mvSplit(st.mvx[fi], qx) - local my, my2 = mvSplit(st.mvy[fi], qy) - local s1 = pos + my * stride + mx - if mx2 ~= 0 or my2 ~= 0 then - local s2 = s1 + my2 * stride + mx2 + -- the residual + if last ~= 0 then + idctAdd(st.tmp, dst, pos, stride) + elseif fv ~= 0 then for y = 0, 7 do - local d, a, b, r = pos + y * stride, s1 + y * stride, s2 + y * stride, y * 32 - for x = 0, 7 do - local p = (readu8(ref, a + x) + readu8(ref, b + x)) // 2 - local v = if flat then fv else readi32(res, r + x * 4) - writeu8(dst, d + x, readu8(CLAMP, p + v + COFF)) - end - end - else - for y = 0, 7 do - local d, a, r = pos + y * stride, s1 + y * stride, y * 32 - for x = 0, 7 do - local v = if flat then fv else readi32(res, r + x * 4) - writeu8(dst, d + x, readu8(CLAMP, readu8(ref, a + x) + v + COFF)) + local d = pos + y * stride + for x = d, d + 7 do + writeu8(dst, x, readu8(CLAMP, readu8(dst, x) + fv + COFF)) end end end @@ -593,6 +651,88 @@ local function borders(st: State, planes: { buffer }) end end +-- DC prediction (raster order per plane): each coded block's DC adds a prediction from its left / upper neighbours +-- coded from the same reference frame (or the last such DC in the plane) +local function dcPredict(st: State) + local coded, refi, dcv = st.coded, st.refi, st.dcv + for pli = 0, 2 do + local nh, nv = st.nhf[pli + 1], st.nvf[pli + 1] + local base = st.foff[pli + 1] + local pl0, pl1, pl2 = 0, 0, 0 -- the last DC of each reference frame (GOLD, PREV, SELF) + for fy = 0, nv - 1 do + local row = base + fy * nh + local lref, ulref, uref = -1, -1, -1 + if fy > 0 then + uref = refi[row - nh + 1] + end + for fx = 0, nh - 1 do + local fi = row + fx + 1 + local urref = -1 + if fy > 0 and fx + 1 < nh then + urref = refi[fi - nh + 1] + end + if coded[fi] ~= 0 then + local r = refi[fi] + local pred: number + if fy == 0 then + pred = if r == 0 then pl0 elseif r == 1 then pl1 else pl2 + else + local m = (if lref == r then 1 else 0) + (if ulref == r then 2 else 0) + (if uref == r then 4 else 0) + (if urref == r then 8 else 0) + if m == 0 then + pred = if r == 0 then pl0 elseif r == 1 then pl1 else pl2 + elseif m == 1 or m == 3 then + pred = dcv[fi - 1] + elseif m == 2 then + pred = dcv[fi - nh - 1] + elseif m == 4 or m == 6 or m == 12 then + pred = dcv[fi - nh] + elseif m == 5 then + local s = dcv[fi - 1] + dcv[fi - nh] + pred = if s < 0 then -((-s) // 2) else s // 2 + elseif m == 8 then + pred = dcv[fi - nh + 1] + elseif m == 9 or m == 11 or m == 13 then + local s = 75 * dcv[fi - 1] + 53 * dcv[fi - nh + 1] + pred = if s < 0 then -((-s) // 128) else s // 128 + elseif m == 10 then + local s = dcv[fi - nh - 1] + dcv[fi - nh + 1] + pred = if s < 0 then -((-s) // 2) else s // 2 + elseif m == 14 then + local s = 3 * (dcv[fi - nh - 1] + dcv[fi - nh + 1]) + 10 * dcv[fi - nh] + pred = if s < 0 then -((-s) // 16) else s // 16 + else + local p0, p1, p2 = dcv[fi - 1], dcv[fi - nh - 1], dcv[fi - nh] + local s = 29 * (p0 + p2) - 26 * p1 + pred = if s < 0 then -((-s) // 32) else s // 32 + if math.abs(pred - p2) > 128 then + pred = p2 + elseif math.abs(pred - p0) > 128 then + pred = p0 + elseif math.abs(pred - p1) > 128 then + pred = p1 + end + end + end + local v = wrap16(dcv[fi] + pred) + dcv[fi] = v + if r == 0 then + pl0 = v + elseif r == 1 then + pl1 = v + else + pl2 = v + end + lref = r + else + lref = -1 + end + ulref = uref + uref = urref + end + end + end +end + -- ------------------------------------------------------------------------------------------------ frame decode -- decode(st, data, off, len) -> kind ("key" | "inter" | "dup") | nil, message. The picture is then in -- st.refs[st.shown]. @@ -609,31 +749,11 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?, if pos > maxp then pos = maxp end - local w = rshift(lshift(bswap(readu32(bb, pos // 8)), pos % 8), 32 - n) + local w = rshift(lshift(bswap(readu32(bb, rshift(pos, 3))), band(pos, 7)), 32 - n) pos += n return w end local huff = st.huff - local function token(root: number): number - if pos > maxp then - pos = maxp - end - local o = root - while true do - local n = huff[o] - local w = rshift(lshift(bswap(readu32(bb, pos // 8)), pos % 8), 32 - n) - local e = huff[o + 1 + w] - if e >= 0 then - pos += e // 64 - return e % 64 - end - pos += n - o = -e - if pos > maxp then - pos = maxp - end - end - end local function sbRun(): number if bits(1) == 0 then return 1 @@ -1028,9 +1148,28 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?, eobs -= 1 t = 64 else - local tok = token(root) - local ex = TOKEN_EXTRA[tok] - local eb = if ex > 0 then bits(ex) else 0 + -- the token (inlined: the hottest loop), then its extra bits + if pos > maxp then + pos = maxp + end + local o, tok = root, 0 + while true do + local nb = huff[o] + local e = huff[o + 1 + rshift(lshift(bswap(readu32(bb, rshift(pos, 3))), band(pos, 7)), 32 - nb)] + if e >= 0 then + pos += rshift(e, 6) + tok = band(e, 63) + break + end + pos += nb + o = -e + end + local ex = TOKEN_EXTRA[tok + 1] + local eb = 0 + if ex > 0 then + eb = rshift(lshift(bswap(readu32(bb, rshift(pos, 3))), band(pos, 7)), 32 - ex) + pos += ex + end if tok < 7 then local run: number if tok < 3 then @@ -1046,49 +1185,18 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?, end eobs = run - 1 t = 64 - elseif tok < 9 then - t = zzi + eb + 1 else - local v, r = 0, 0 - if tok < 13 then - v = if tok == 9 then 1 elseif tok == 10 then -1 elseif tok == 11 then 2 else -2 - elseif tok < 17 then - v = if eb == 1 then -(tok - 10) else tok - 10 - elseif tok < 23 then - local half = 2 ^ (ex - 1) - local base = if tok == 17 then 7 elseif tok == 18 then 9 elseif tok == 19 then 13 elseif tok == 20 then 21 elseif tok == 21 then 37 else 69 - v = base + eb % half - if eb >= half then - v = -v + -- zero runs and coefficients: run * 4096 + value + 2048 (value 0: a zero run) + local e = TOKEN_VALUE[tok * 1024 + eb + 1] + local p = zzi + rshift(e, 12) + local v = band(e, 4095) - 2048 + if v ~= 0 then + if p == 0 then + dcv[fi] = v + elseif p < 64 then + writei16(coef, (fi - 1) * 128 + p * 2, v) + last[fi] = p end - elseif tok < 28 then - r = tok - 22 - v = if eb == 1 then -1 else 1 - elseif tok == 28 then - r = 6 + eb % 4 - v = if eb >= 4 then -1 else 1 - elseif tok == 29 then - r = 10 + eb % 8 - v = if eb >= 8 then -1 else 1 - elseif tok == 30 then - r = 1 - v = 2 + eb % 2 - if eb >= 2 then - v = -v - end - else - r = 2 + eb % 2 - v = 2 + (eb // 2) % 2 - if eb >= 4 then - v = -v - end - end - local p = zzi + r - if p == 0 then - dcv[fi] = v - elseif p < 64 then - writei16(coef, (fi - 1) * 128 + p * 2, v) - last[fi] = p end t = p + 1 end @@ -1104,83 +1212,7 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?, end end - -- DC prediction (raster order per plane) - for pli = 0, 2 do - local nh, nv = st.nhf[pli + 1], st.nvf[pli + 1] - local base = st.foff[pli + 1] - local pl0, pl1, pl2 = 0, 0, 0 -- the last DC of each reference frame (GOLD, PREV, SELF) - for fy = 0, nv - 1 do - local row = base + fy * nh - local lref, ulref, uref = -1, -1, -1 - if fy > 0 then - uref = refi[row - nh + 1] - end - for fx = 0, nh - 1 do - local fi = row + fx + 1 - local urref = -1 - if fy > 0 and fx + 1 < nh then - urref = refi[fi - nh + 1] - end - if coded[fi] ~= 0 then - local r = refi[fi] - local pred: number - if fy == 0 then - pred = if r == 0 then pl0 elseif r == 1 then pl1 else pl2 - else - local m = (if lref == r then 1 else 0) + (if ulref == r then 2 else 0) + (if uref == r then 4 else 0) + (if urref == r then 8 else 0) - if m == 0 then - pred = if r == 0 then pl0 elseif r == 1 then pl1 else pl2 - elseif m == 1 or m == 3 then - pred = dcv[fi - 1] - elseif m == 2 then - pred = dcv[fi - nh - 1] - elseif m == 4 or m == 6 or m == 12 then - pred = dcv[fi - nh] - elseif m == 5 then - local s = dcv[fi - 1] + dcv[fi - nh] - pred = if s < 0 then -((-s) // 2) else s // 2 - elseif m == 8 then - pred = dcv[fi - nh + 1] - elseif m == 9 or m == 11 or m == 13 then - local s = 75 * dcv[fi - 1] + 53 * dcv[fi - nh + 1] - pred = if s < 0 then -((-s) // 128) else s // 128 - elseif m == 10 then - local s = dcv[fi - nh - 1] + dcv[fi - nh + 1] - pred = if s < 0 then -((-s) // 2) else s // 2 - elseif m == 14 then - local s = 3 * (dcv[fi - nh - 1] + dcv[fi - nh + 1]) + 10 * dcv[fi - nh] - pred = if s < 0 then -((-s) // 16) else s // 16 - else - local p0, p1, p2 = dcv[fi - 1], dcv[fi - nh - 1], dcv[fi - nh] - local s = 29 * (p0 + p2) - 26 * p1 - pred = if s < 0 then -((-s) // 32) else s // 32 - if math.abs(pred - p2) > 128 then - pred = p2 - elseif math.abs(pred - p0) > 128 then - pred = p0 - elseif math.abs(pred - p1) > 128 then - pred = p1 - end - end - end - local v = wrap16(dcv[fi] + pred) - dcv[fi] = v - if r == 0 then - pl0 = v - elseif r == 1 then - pl1 = v - else - pl2 = v - end - lref = r - else - lref = -1 - end - ulref = uref - uref = urref - end - end - end + dcPredict(st) -- reconstruction into a free reference buffer local self = 1 diff --git a/src/Formats/codecs/video/Theora/Header.luau b/src/Formats/codecs/video/Theora/Header.luau index 2d88abd..2259c19 100644 --- a/src/Formats/codecs/video/Theora/Header.luau +++ b/src/Formats/codecs/video/Theora/Header.luau @@ -226,7 +226,7 @@ local function buildTable(out: { number }, kid0: { number }, kid1: { number }, t for i = 0, size - 1 do local cur, d = n, 0 while d < nb and tok[cur] < 0 do - local bit = (i // 2 ^ (nb - 1 - d)) % 2 + local bit = bit32.extract(i, nb - 1 - d, 1) local nx = if bit == 0 then kid0[cur] else kid1[cur] d += 1 cur = nx diff --git a/tools/bench.luau b/tools/bench.luau index b32ee01..afd0966 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -1078,3 +1078,42 @@ do jitTune(false) end end +do + -- ------------------------------------------------------------------------------------- ws-video2-theora: fps + -- Theora (codecs/video/Theora): 320x240 clips, 50 frames, keyframe every 25: testsrc2 at 600 kbit/s (mostly static: + -- few coded blocks) and a mandelbrot zoom at 1200 kbit/s (every block coded, moving); decode alone and decode + + -- image() (the yuv420p copy every VideoDecoder frame makes) + local Theora = require("@formats/codecs/video/Theora") + local Ogg = require("@formats/containers/Ogg") + local Base64 = require("@formats/core/Base64") + local TB = require("@tests/gen/theora_bench") + local function run(tag: string) + for _, c in TB do + local d = Ogg.open(Base64.decode(c.file) :: buffer) + local hdrs, data = {}, {} + for p in d:packets(d:find("theora")) do + table.insert(if #hdrs < 3 then hdrs else data, Ogg.copy(p)) + end + for _, image in { false, true } do + local n = c.width * c.height * 3 // 2 * #data + local dt = bench(string.format("theora %s %dx%d%s%s", c.name, c.width, c.height, if image then " +img" else "", tag), 3, n, function() + local dec = Theora.new(hdrs) :: any + for _, p in data do + dec:decode(p) + if image then + dec:image() + end + end + end) + print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. #data .. " frames)", #data / dt)) + end + end + end + jitTune(false) + run("") + if runtime == "luajit" then + jitTune(true) + run(" (jit tuned)") + jitTune(false) + end +end diff --git a/tools/fixtures/docs_theora.py b/tools/fixtures/docs_theora.py new file mode 100644 index 0000000..1b85b57 --- /dev/null +++ b/tools/fixtures/docs_theora.py @@ -0,0 +1,20 @@ +# Docs inputs for the Ogg Theora section of docs/guide/video.md (tools/fixtures.py; tools/docs_check.py reads them): a +# tiny ffmpeg clip (testsrc 64x48, 10 frames at 10 fps, libtheora + a libvorbis sine track) as Ogg video. +# tests/gen/docs_theora.luau { files = { ["clip.ogv"] = bytes }, vars = { ogvBytes = "clip.ogv" } } +import importlib.util, os, shutil, subprocess, tempfile + +INPUTS = ["tools/docs_check.py"] + + +def generate(ctx): + spec = importlib.util.spec_from_file_location("docs_theora_check", os.path.join(ctx.root, "tools", "docs_check.py")) + dc = importlib.util.module_from_spec(spec) + spec.loader.exec_module(dc) + ffmpeg = shutil.which("ffmpeg") or ctx.nix("ffmpeg-full", ["sh", "-c", "command -v ffmpeg"]).decode().strip() + with tempfile.TemporaryDirectory() as d: + out = os.path.join(d, "clip.ogv") + subprocess.run([ffmpeg, "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=64x48:rate=10:duration=1", + "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=44100:duration=1", "-c:v", "libtheora", + "-q:v", "6", "-g", "5", "-c:a", "libvorbis", "-q:a", "2", out], check=True, capture_output=True) + files = {"clip.ogv": open(out, "rb").read()} + dc.emit_inputs(ctx, files, {"ogvBytes": "clip.ogv"}) diff --git a/tools/fixtures/theora.py b/tools/fixtures/theora.py index 7d05299..6bcfd6a 100644 --- a/tools/fixtures/theora.py +++ b/tools/fixtures/theora.py @@ -10,7 +10,7 @@ # rate, channels } # tests/gen/theora_opus.luau, theora_flac.luau the same with mono Opus / FLAC audio # tests/gen/theora_mkv.luau { file (base64 .mkv), frames } -# tests/gen/theora_bench.luau { file (base64 .ogv, 320x240) } for tools/bench.luau +# tests/gen/theora_bench.luau { { name, file (base64 .ogv, 320x240), width, height } } for tools/bench.luau import base64, os, shutil, struct, subprocess, tempfile, zlib FF = ["-hide_banner", "-loglevel", "error", "-y"] @@ -199,7 +199,10 @@ def generate(ctx): ff(["-i", os.path.join(tmp, "odd.ogv"), "-c", "copy", mkv]) ctx.emit("theora_mkv", {"file": b64(open(mkv, "rb").read()), "frames": framecrc(mkv, "yuv420p"), "width": 174, "height": 98}) - # the bench stream - bench = os.path.join(tmp, "bench.ogv") - ff(src("testsrc2", 320, 240, 25, 2) + ["-c:v", "libtheora", "-b:v", "600k", "-g", "25", bench]) - ctx.emit("theora_bench", {"file": b64(open(bench, "rb").read()), "width": 320, "height": 240}) + # the bench streams: an easy clip (testsrc2, mostly static) and a hard one (a mandelbrot zoom: every block moves) + bench = [] + for kind, rate in (("testsrc2", "600k"), ("mandelbrot", "1200k")): + path = os.path.join(tmp, "bench_" + kind + ".ogv") + ff(src(kind, 320, 240, 25, 2) + ["-c:v", "libtheora", "-b:v", rate, "-g", "25", path]) + bench.append({"name": kind, "file": b64(open(path, "rb").read()), "width": 320, "height": 240}) + ctx.emit("theora_bench", bench)