--!strict --!native --!optimize 2 -- codecs/video/Legacy/Msvideo1: Microsoft Video 1 (CRAM / MSVC / WHAM) frames, 8-bit palettised and 16-bit -- X-R-G-B 1-5-5-5. Written from the format description (MultimediaWiki "Microsoft Video 1"): 4x4 blocks, block rows -- bottom-up (rows of a block bottom-up too), each block a 16-bit code: a skip count (0x84xx - 0x87xx), a 2-colour -- block (16 flag bits + 2 colours; at 16 bits a first colour with bit 15 set makes it an 8-colour block, one colour -- pair per quadrant; at 8 bits codes >= 0x9000 are 8-colour), or a 1-colour block. Paints over the previous picture. -- Bit-exact with ffmpeg's msvideo1 decoder. Never throws: every read is range-checked (a damaged frame stops there). local Picture = require("@formats/codecs/video/Legacy/Picture") local readu8, readu16, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32 local band = bit32.band local Msvideo1 = {} -- the colour of pixel (x, y) of a block (y = 0 is the block's bottom row) for flag bit f: 2-colour c0 / c1 -- (bit set = c0), 8-colour: colours[quadrant * 2 + (bit clear)] local function quadrant(x: number, y: number): number return (if y >= 2 then 4 else 0) + (if x >= 2 then 2 else 0) end -- decode(pic, data, bits) -> true | false, message (bits = 8: palettised, else 16-bit) function Msvideo1.decode(pic: Picture.Picture, d: buffer, bits: number): (boolean, string?) local n = buffer.len(d) local bw, bh = pic.w // 4, pic.h // 4 if n < bw * bh // 512 then return false, "frame too small" end local stride = pic.stride local total = bw * bh local skip = 0 local p = 0 local wide = bits ~= 8 local px = (if wide then pic.rgba else pic.idx) :: buffer local lut = if wide then Picture.rgb555() else nil local c = table.create(8, 0) for by = bh - 1, 0, -1 do for bx = 0, bw - 1 do if skip ~= 0 then skip -= 1 total -= 1 continue end if p + 2 > n then return true, nil end local a, b = readu8(d, p), readu8(d, p + 1) p += 2 if a == 0 and b == 0 and total == 0 then return true, nil end -- the block's bottom-left pixel; its rows go up local o = (by * 4 + 3) * stride + bx * 4 if band(b, 0xFC) == 0x84 then skip = (b - 0x84) * 256 + a - 1 elseif b < 0x80 or (not wide and b >= 0x90) then local flags = b * 256 + a local eight if wide then if p + 4 > n then return true, nil end c[1], c[2] = readu16(d, p), readu16(d, p + 2) p += 4 eight = c[1] >= 0x8000 if eight then if p + 12 > n then return true, nil end for k = 3, 8 do c[k] = readu16(d, p) p += 2 end end else eight = b >= 0x90 local cnt = if eight then 8 else 2 if p + cnt > n then return true, nil end for k = 1, cnt do c[k] = readu8(d, p) p += 1 end end for y = 0, 3 do local row = o - y * stride for x = 0, 3 do local k = (if band(flags, 1) == 1 then 1 else 2) if eight then k += quadrant(x, y) end flags = flags // 2 if lut then writeu32(px, (row + x) * 4, readu32(lut, (c[k] % 32768) * 4)) else writeu8(px, row + x, c[k]) end end end else local v = if wide then readu32(lut :: buffer, ((b * 256 + a) % 32768) * 4) else a for y = 0, 3 do local row = o - y * stride if lut then for x = 0, 3 do writeu32(px, (row + x) * 4, v) end else writeu32(px, row, v * 0x01010101) end end end total -= 1 end end return true, nil end return Msvideo1