ws-video-codecs: codecs/Jpeg: yuv output crops four samples per write and reuses one frame-sized IDCT plane between calls

This commit is contained in:
4DBug committed 2026-10-31 09:37:01 -05:00
1 parent 43da7d723c
commit 33a5a5b409
2 files changed
+27 -12

No files matched your search

+16 -1
View File
@@ -679,6 +679,21 @@ end
-- horizontally ("422", 2x1 / 1x1 or ffmpeg's 2x2 / 1x2: each pair of chroma rows averaged, rounding up); at the luma
-- rate ("444", 1x1 or ffmpeg's 1x2 everywhere: each 2 x 2 chroma block averaged, rounding to nearest); grey ("gray":
-- chroma 128). Full scale only (opts.scale = 1).
-- one IDCT plane at a time is live (each is cropped into the output before the next component): frame-sized planes
-- (up to 4 MB) are kept between calls, so a stream of video frames does not allocate them per frame
local scratch: buffer? = nil
local function scratchPlane(n: number): buffer
local sc = scratch
if sc and buffer.len(sc) >= n then
return sc
end
local b = buffer.create(n)
if n <= 4 * 1024 * 1024 then
scratch = b
end
return b
end
function Decode.outputYuv(st: State): (number?, number?, buffer?, string?)
local f: Frame = st.frame
if st.scale ~= 1 or f.precision ~= 8 then
@@ -719,7 +734,7 @@ function Decode.outputYuv(st: State): (number?, number?, buffer?, string?)
local zeroQ: buffer? = nil
for i, c in comps do
local stride, rows = c.bpl * 8, c.bpc * 8
local plane = buffer.create(stride * rows * 2)
local plane = scratchPlane(stride * rows * 2)
local coef = c.coef or buffer.create(c.bpl * c.bpc * 128)
local q = c.quant
if not q then
+11 -11
View File
@@ -5,27 +5,27 @@
-- samples, block-padded) cropped into u8 yuv420p planes without colour conversion or upsampling, 4:2:2 / 4:4:4
-- chroma reduced to 4:2:0 by averaging, and the interleaving of two fields (AVI1 interlaced Motion-JPEG).
-- The callers (codecs/Jpeg/Decode, codecs/Jpeg) size every buffer; each loop reads and writes inside those sizes.
local readu16, writeu8, readu32 = buffer.readu16, buffer.writeu8, buffer.readu32
local readu16, writeu8, readu32, writeu32 = buffer.readu16, buffer.writeu8, buffer.readu32, buffer.writeu32
local Yuv = {}
-- crop(src, stride, w, h, dst, doff): the top-left w x h samples of a u16 plane (stride samples per row) into u8
-- samples at dst[doff], w per row
function Yuv.crop(src: buffer, stride: number, w: number, h: number, dst: buffer, doff: number)
local npairs = w // 2
local quads = w // 4
local rest = w - quads * 4
for y = 0, h - 1 do
local s = y * stride * 2
local d = doff + y * w
-- two samples per u32 read
for _ = 1, npairs do
local v = readu32(src, s)
writeu8(dst, d, v % 65536)
writeu8(dst, d + 1, v // 65536)
s += 4
d += 2
-- four samples (two u32 reads of two u16 samples, each 0..255) per u32 write
for _ = 1, quads do
local a, b = readu32(src, s), readu32(src, s + 4)
writeu32(dst, d, a % 65536 + (a // 65536) * 256 + (b % 65536) * 65536 + (b // 65536) * 16777216)
s += 8
d += 4
end
if w % 2 == 1 then
writeu8(dst, d, readu16(src, s))
for k = 0, rest - 1 do
writeu8(dst, d + k, readu16(src, s + k * 2))
end
end
end