--!strict --!native --!optimize 2 -- codecs/video/Legacy/Picture: the picture every legacy decoder keeps between frames (they are all inter-frame -- codecs: a packet paints over the previous picture). Palettised codecs keep a u8 index plane and a 256-entry palette, -- direct-colour ones an rgba8 plane; both planes are padded (stride / rows) so the codecs' block and run writes past -- the visible edge land in range. Also the 5-5-5 / 5-6-5 to 8-bit tables: channels are widened by bit replication -- (c << 3 | c >> 2), as ffmpeg's swscale does, so the output equals ffmpeg's rgba conversion. New in formats-luau. local Types = require("@formats/Types") local Pixels = require("@formats/codecs/image/Pixels") local readu32, writeu32 = buffer.readu32, buffer.writeu32 local Picture = {} export type Picture = { w: number, h: number, stride: number, -- index plane: bytes per row; rgba plane: pixels per row rows: number, idx: buffer?, -- u8 index plane, stride * rows bytes (palettised) rgba: buffer?, -- u32 rgba8 plane, stride * rows * 4 bytes (direct colour) pal: buffer, -- 256 u32 entries, bytes R, G, B, A } -- new(w, h, palettised, padW?, padH?, palette?) -> a black picture; padW / padH round the plane up (blocks of 4, ...) function Picture.new(w: number, h: number, palettised: boolean, padW: number?, padH: number?, palette: buffer?): Picture local pw, ph = padW or 1, padH or 1 local stride = ((w + pw - 1) // pw) * pw local rows = ((h + ph - 1) // ph) * ph local pal = buffer.create(1024) if palette and buffer.len(palette) >= 1024 then buffer.copy(pal, 0, palette, 0, 1024) end local p: Picture = { w = w, h = h, stride = stride, rows = rows, pal = pal } if palettised then p.idx = buffer.create(stride * rows) else p.rgba = buffer.create(stride * rows * 4) end return p end -- image(p) -> a fresh rgba8 Image of the visible w x h function Picture.image(p: Picture): Types.Image local w, h = p.w, p.h local img = Types.Image.new(w, h, "rgba8") local idx, rgba = p.idx, p.rgba if idx then Pixels.indexed(idx, 0, p.stride, 8, img.pixels, w, h, true, p.pal) elseif rgba then if p.stride == w then buffer.copy(img.pixels, 0, rgba, 0, w * h * 4) else for y = 0, h - 1 do buffer.copy(img.pixels, y * w * 4, rgba, y * p.stride * 4, w * 4) end end end return img end -- setPalette(p, src): a 1024-byte palette (u32 R, G, B, A entries) from a packet or the track function Picture.setPalette(p: Picture, src: buffer) if buffer.len(src) >= 1024 then buffer.copy(p.pal, 0, src, 0, 1024) end end local lut555: buffer? = nil local lut565: buffer? = nil -- rgb555() -> u32 table: X-R-G-B 1-5-5-5 value -> rgba8 pixel (opaque); 32768 entries function Picture.rgb555(): buffer local t = lut555 if t then return t end local b = buffer.create(32768 * 4) for v = 0, 32767 do local r, g, bl = v // 1024, (v // 32) % 32, v % 32 r, g, bl = r * 8 + r // 4, g * 8 + g // 4, bl * 8 + bl // 4 writeu32(b, v * 4, r + g * 256 + bl * 65536 + 0xFF000000) end lut555 = b return b end -- rgb565() -> u32 table: R-G-B 5-6-5 value -> rgba8 pixel; 65536 entries function Picture.rgb565(): buffer local t = lut565 if t then return t end local b = buffer.create(65536 * 4) for v = 0, 65535 do local r, g, bl = v // 2048, (v // 32) % 64, v % 32 r, g, bl = r * 8 + r // 4, g * 4 + g // 16, bl * 8 + bl // 4 writeu32(b, v * 4, r + g * 256 + bl * 65536 + 0xFF000000) end lut565 = b return b end -- copy of a picture's plane (for decoders that read the previous picture while writing the next) function Picture.clonePlane(p: Picture): buffer local src = (p.idx or p.rgba) :: buffer local c = buffer.create(buffer.len(src)) buffer.copy(c, 0, src, 0) return c end local _ = readu32 return Picture