--!strict --!native --!optimize 2 -- codecs/image/Yuv: planar 4:2:0 Y'CbCr <-> rgba8 for the video codecs and writers (native; never throws). BT.601 or -- BT.709 matrices, limited ("tv", 16..235 / 16..240: VP8, MPEG-1, H.264) or full ("pc", 0..255: MJPEG / JPEG) range. -- yuv -> rgb uses nearest chroma and 16-bit fixed point with swscale's default (non-accurate) coefficients, so the -- result is within about one level of `ffmpeg -pix_fmt rgba`; rgb -> yuv averages each 2x2 block for the chroma -- (ffmpeg's default rgb -> yuv420p chroma siting is close to this). -- -- Yuv.toRgba(planes, w, h, uo, vo, cw, range?, matrix?, out?) -> rgba8 buffer (alpha 255) -- Yuv.fromRgba(rgba, w, h, range?, matrix?, out?) -> yuv420p buffer (Y, then U, then V; ceil(w/2) x ceil(h/2)) -- Yuv.imageToRgba(img) / Yuv.imageFromRgba(img, range?, matrix?) -> Types.Image -- Yuv.scale(src, w, h, dw, dh, out?) -> yuv420p buffer of dw x dh (each plane bilinear; for shrinking by up to 2x per -- axis, larger factors skip source samples); Yuv.imageScale(img, dw, dh) -> Types.Image (yuv420p in and out) local Types = require("@rosetta/Types") local readu8, writeu8, writeu32 = buffer.readu8, buffer.writeu8, buffer.writeu32 local bcreate = buffer.create local floor = math.floor local Yuv = {} -- coefficients (x 65536): r = y' + crv * v; g = y' - cgu * u - cgv * v; b = y' + cbu * u -- limited: y' = (y - 16) * 255 / 219, chroma scaled by 255 / 224 local MATRIX: { [string]: { kr: number, kb: number } } = { bt601 = { kr = 0.299, kb = 0.114 }, bt709 = { kr = 0.2126, kb = 0.0722 }, } type Coeffs = { ys: number, yo: number, crv: number, cgu: number, cgv: number, cbu: number, cs: number } local cache: { [string]: Coeffs } = {} local function coeffs(range: string?, matrix: string?): Coeffs local key = (range or "limited") .. (matrix or "bt601") local c = cache[key] if c then return c end local m = MATRIX[matrix or "bt601"] or MATRIX.bt601 local kr, kb = m.kr, m.kb local kg = 1 - kr - kb local full = range == "full" local ys = if full then 1 else 255 / 219 local cs = if full then 1 else 255 / 224 c = { ys = floor(ys * 65536 + 0.5), yo = if full then 0 else 16, crv = floor(2 * (1 - kr) * cs * 65536 + 0.5), cbu = floor(2 * (1 - kb) * cs * 65536 + 0.5), cgu = floor(2 * (1 - kb) * kb / kg * cs * 65536 + 0.5), cgv = floor(2 * (1 - kr) * kr / kg * cs * 65536 + 0.5), cs = cs, } cache[key] = c return c end -- CLIP[v + 1024] = clamp(v, 0, 255) for v in -1024..1279 local CLIP = bcreate(2304) for i = 0, 2303 do writeu8(CLIP, i, math.clamp(i - 1024, 0, 255)) end -- toRgba(src, w, h, uo, vo, cw, range?, matrix?, out?) -> rgba8 (w * h * 4) from planes in one buffer: Y at 0 with -- stride w, U at uo and V at vo with stride cw (ceil(w/2) rows of ceil(h/2)) function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, cw: number, range: string?, matrix: string?, out: buffer?): buffer local dst = out or bcreate(w * h * 4) local c = coeffs(range, matrix) local ys, yo, crv, cgu, cgv, cbu = c.ys, c.yo, c.crv, c.cgu, c.cgv, c.cbu local ch = (h + 1) // 2 if buffer.len(src) < vo + cw * ch or buffer.len(dst) < w * h * 4 or uo + cw * ch > buffer.len(src) then return dst end for y = 0, h - 1 do local yrow = y * w local crow = (y // 2) * cw local o = yrow * 4 for x = 0, w - 1, 2 do local ci = crow + (x // 2) local u = readu8(src, uo + ci) - 128 local v = readu8(src, vo + ci) - 128 local rv = (crv * v + 32768) // 65536 local gv = (cgu * u + cgv * v + 32768) // 65536 local bu = (cbu * u + 32768) // 65536 local yy = (ys * (readu8(src, yrow + x) - yo) + 32768) // 65536 + 1024 writeu32(dst, o, readu8(CLIP, yy + rv) + readu8(CLIP, yy - gv) * 256 + readu8(CLIP, yy + bu) * 65536 + 0xff000000) if x + 1 < w then yy = (ys * (readu8(src, yrow + x + 1) - yo) + 32768) // 65536 + 1024 writeu32(dst, o + 4, readu8(CLIP, yy + rv) + readu8(CLIP, yy - gv) * 256 + readu8(CLIP, yy + bu) * 65536 + 0xff000000) end o += 8 end end return dst end -- fromRgba(rgba, w, h, range?, matrix?, out?) -> yuv420p function Yuv.fromRgba(src: buffer, w: number, h: number, range: string?, matrix: string?, out: buffer?): buffer local cw, ch = (w + 1) // 2, (h + 1) // 2 local uo, vo = w * h, w * h + cw * ch local dst = out or bcreate(w * h + 2 * cw * ch) if buffer.len(src) < w * h * 4 or buffer.len(dst) < w * h + 2 * cw * ch then return dst end local m = MATRIX[matrix or "bt601"] or MATRIX.bt601 local kr, kb = m.kr, m.kb local kg = 1 - kr - kb local full = range == "full" local ysc = if full then 1 else 219 / 255 local csc = if full then 1 else 224 / 255 local yof = if full then 0.5 else 16.5 -- fixed point x 65536 local yr, yg, yb = floor(kr * ysc * 65536 + 0.5), floor(kg * ysc * 65536 + 0.5), floor(kb * ysc * 65536 + 0.5) local yoff = floor(yof * 65536) local ur = floor(-kr / (2 * (1 - kb)) * csc * 65536 + 0.5) local ug = floor(-kg / (2 * (1 - kb)) * csc * 65536 + 0.5) local ub = floor(0.5 * csc * 65536 + 0.5) local vr = ub local vg = floor(-kg / (2 * (1 - kr)) * csc * 65536 + 0.5) local vb = floor(-kb / (2 * (1 - kr)) * csc * 65536 + 0.5) local coff4 = floor(128.5 * 65536) * 4 for i = 0, w * h - 1 do local o = i * 4 local r, g, b = readu8(src, o), readu8(src, o + 1), readu8(src, o + 2) writeu8(dst, i, readu8(CLIP, (yr * r + yg * g + yb * b + yoff) // 65536 + 1024)) end for cy = 0, ch - 1 do local y0 = cy * 2 local y1 = if y0 + 1 < h then y0 + 1 else y0 for cx = 0, cw - 1 do local x0 = cx * 2 local x1 = if x0 + 1 < w then x0 + 1 else x0 local a, b2, c2, d = (y0 * w + x0) * 4, (y0 * w + x1) * 4, (y1 * w + x0) * 4, (y1 * w + x1) * 4 local r = readu8(src, a) + readu8(src, b2) + readu8(src, c2) + readu8(src, d) local g = readu8(src, a + 1) + readu8(src, b2 + 1) + readu8(src, c2 + 1) + readu8(src, d + 1) local b = readu8(src, a + 2) + readu8(src, b2 + 2) + readu8(src, c2 + 2) + readu8(src, d + 2) local i = cy * cw + cx writeu8(dst, uo + i, readu8(CLIP, (ur * r + ug * g + ub * b + coff4) // 262144 + 1024)) writeu8(dst, vo + i, readu8(CLIP, (vr * r + vg * g + vb * b + coff4) // 262144 + 1024)) end end return dst end -- the range / matrix an Image's meta records (meta.yuv = { range, matrix }); limited BT.601 when absent local function info(img: Types.Image): (string, string) local m = img.meta local y = m and m.yuv if type(y) == "table" then return y.range or "limited", y.matrix or "bt601" end local src = m and m.source if type(src) == "table" and src.range == "full" then return "full", "bt601" end return "limited", "bt601" end Yuv.info = info -- planes(w, h) -> uo, vo, cw, ch of a yuv420p buffer function Yuv.planes(w: number, h: number): (number, number, number, number) local cw, ch = (w + 1) // 2, (h + 1) // 2 return w * h, w * h + cw * ch, cw, ch end -- FULL_Y / FULL_C: limited -> full range sample maps local FULL_Y = bcreate(256) local FULL_C = bcreate(256) for v = 0, 255 do writeu8(FULL_Y, v, math.clamp(floor((v - 16) * 255 / 219 + 0.5), 0, 255)) writeu8(FULL_C, v, math.clamp(floor((v - 128) * 255 / 224 + 128.5), 0, 255)) end -- toFullRange(img) -> a yuv420p Image in full range (a limited-range one remapped; a full-range one returned) function Yuv.toFullRange(img: Types.Image): Types.Image if img.format ~= "yuv420p" or info(img) == "full" then return img end local w, h = img.width, img.height local out = Types.Image.new(w, h, "yuv420p") local src, dst = img.pixels, out.pixels local ny = w * h local n = math.min(buffer.len(src), buffer.len(dst)) for i = 0, math.min(ny, n) - 1 do writeu8(dst, i, readu8(FULL_Y, readu8(src, i))) end for i = ny, n - 1 do writeu8(dst, i, readu8(FULL_C, readu8(src, i))) end out.meta.yuv = { range = "full", matrix = select(2, info(img)) } out.meta.source = img.meta.source return out end -- imageToRgba(img) -> an rgba8 Image (yuv420p converted; rgba8 returned as is; nil for other formats) function Yuv.imageToRgba(img: Types.Image): Types.Image? if img.format == "rgba8" then return img end if img.format ~= "yuv420p" then return nil end local w, h = img.width, img.height local uo, vo, cw = Yuv.planes(w, h) local range, matrix = info(img) local out = Types.Image.new(w, h, "rgba8") Yuv.toRgba(img.pixels, w, h, uo, vo, cw, range, matrix, out.pixels) out.meta.source = img.meta.source return out end -- imageFromRgba(img, range?, matrix?) -> a yuv420p Image (meta.yuv records the range / matrix) function Yuv.imageFromRgba(img: Types.Image, range: string?, matrix: string?): Types.Image? if img.format == "yuv420p" then return img end if img.format ~= "rgba8" then return nil end local out = Types.Image.new(img.width, img.height, "yuv420p") Yuv.fromRgba(img.pixels, img.width, img.height, range, matrix, out.pixels) out.meta.yuv = { range = range or "limited", matrix = matrix or "bt601" } out.meta.source = img.meta.source return out end -- one plane (sw x sh bytes at so) -> (dw x dh at do), bilinear in 8-bit fixed point (source sample centres) local function scalePlane(src: buffer, so: number, sw: number, sh: number, dst: buffer, d0: number, dw: number, dh: number) local fx, fy = sw / dw, sh / dh for y = 0, dh - 1 do local sy = (y + 0.5) * fy - 0.5 if sy < 0 then sy = 0 end local y0 = floor(sy) if y0 > sh - 1 then y0 = sh - 1 end local y1 = if y0 < sh - 1 then y0 + 1 else y0 local wy = floor((sy - y0) * 256) local r0, r1 = so + y0 * sw, so + y1 * sw local o = d0 + y * dw for x = 0, dw - 1 do local sx = (x + 0.5) * fx - 0.5 if sx < 0 then sx = 0 end local x0 = floor(sx) if x0 > sw - 1 then x0 = sw - 1 end local x1 = if x0 < sw - 1 then x0 + 1 else x0 local wx = floor((sx - x0) * 256) local a = readu8(src, r0 + x0) * (256 - wx) + readu8(src, r0 + x1) * wx local b = readu8(src, r1 + x0) * (256 - wx) + readu8(src, r1 + x1) * wx writeu8(dst, o + x, (a * (256 - wy) + b * wy + 32768) // 65536) end end end -- scale(src, w, h, dw, dh, out?) -> a yuv420p buffer of dw x dh (planes Y, U, V; chroma ceil(dw / 2) x ceil(dh / 2)). -- src must hold w x h yuv420p (checked: a short buffer or bad sizes return a zero-filled result of the right size). function Yuv.scale(src: buffer, w: number, h: number, width: number, height: number, out: buffer?): buffer local dw: number = math.max(1, floor(width)) local dh: number = math.max(1, floor(height)) local cw: number = (w + 1) // 2 local ch: number = (h + 1) // 2 local dcw: number = (dw + 1) // 2 local dch: number = (dh + 1) // 2 local size = dw * dh + 2 * dcw * dch local dst = if out and buffer.len(out) >= size then out else bcreate(size) if w < 1 or h < 1 or buffer.len(src) < w * h + 2 * cw * ch then return dst end scalePlane(src, 0, w, h, dst, 0, dw, dh) scalePlane(src, w * h, cw, ch, dst, dw * dh, dcw, dch) scalePlane(src, w * h + cw * ch, cw, ch, dst, dw * dh + dcw * dch, dcw, dch) return dst end function Yuv.imageScale(img: Types.Image, dw: number, dh: number): Types.Image local out = Types.Image.new(math.max(1, floor(dw)), math.max(1, floor(dh)), "yuv420p") Yuv.scale(img.pixels, img.width, img.height, out.width, out.height, out.pixels) out.meta = table.clone(img.meta or {}) return out end return Yuv