77 lines
3.2 KiB
Luau
77 lines
3.2 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/Jpeg/Yuv: the yuv420p output of the JPEG decoder (native; never throws): the IDCT output planes (u16
|
|
-- 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, 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 quads = w // 4
|
|
local rest = w - quads * 4
|
|
for y = 0, h - 1 do
|
|
local s = y * stride * 2
|
|
local d = doff + y * w
|
|
-- 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
|
|
for k = 0, rest - 1 do
|
|
writeu8(dst, d + k, readu16(src, s + k * 2))
|
|
end
|
|
end
|
|
end
|
|
|
|
-- rows(src, stride, w, h, dst, doff): a u16 plane of w x h samples (4:2:2 chroma) -> u8 w x ceil(h / 2), each output
|
|
-- row the rounded mean of two source rows (the last row alone when h is odd)
|
|
function Yuv.rows(src: buffer, stride: number, w: number, h: number, dst: buffer, doff: number)
|
|
local oh = (h + 1) // 2
|
|
for y = 0, oh - 1 do
|
|
local a = 2 * y * stride * 2
|
|
local b = if 2 * y + 1 < h then a + stride * 2 else a
|
|
local d = doff + y * w
|
|
for x = 0, w - 1 do
|
|
writeu8(dst, d + x, (readu16(src, a + x * 2) + readu16(src, b + x * 2) + 1) // 2)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- box(src, stride, w, h, dst, doff): a u16 plane of w x h samples (4:4:4 chroma) -> u8 ceil(w / 2) x ceil(h / 2),
|
|
-- each output sample the rounded mean of a 2 x 2 block (edges repeat the last row / column)
|
|
function Yuv.box(src: buffer, stride: number, w: number, h: number, dst: buffer, doff: number)
|
|
local ow, oh = (w + 1) // 2, (h + 1) // 2
|
|
for y = 0, oh - 1 do
|
|
local a = 2 * y * stride * 2
|
|
local b = if 2 * y + 1 < h then a + stride * 2 else a
|
|
local d = doff + y * ow
|
|
for x = 0, ow - 1 do
|
|
local x0 = 2 * x * 2
|
|
local x1 = if 2 * x + 1 < w then x0 + 2 else x0
|
|
writeu8(dst, d + x, (readu16(src, a + x0) + readu16(src, a + x1) + readu16(src, b + x0) + readu16(src, b + x1) + 2) // 4)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- weave(top, toff, bottom, boff, rowBytes, rows, dst, doff): interleave rows of two planes (field 1 on the even rows, field
|
|
-- 2 on the odd rows) into rows output rows at dst[doff]; top holds ceil(rows / 2) rows, bottom floor(rows / 2)
|
|
function Yuv.weave(top: buffer, toff: number, bottom: buffer, boff: number, rowBytes: number, rows: number, dst: buffer, doff: number)
|
|
for y = 0, rows - 1 do
|
|
local k = y // 2
|
|
if y % 2 == 0 then
|
|
buffer.copy(dst, doff + y * rowBytes, top, toff + k * rowBytes, rowBytes)
|
|
else
|
|
buffer.copy(dst, doff + y * rowBytes, bottom, boff + k * rowBytes, rowBytes)
|
|
end
|
|
end
|
|
end
|
|
|
|
return Yuv
|