--!strict --!native --!optimize 2 -- codecs/video/Legacy/Cinepak: Cinepak ('cvid') frames: 24-bit colour (codebook entries of 4 luma + 2 chroma, or 4 -- luma for grey), and 8-bit palettised streams (entries are palette indexes). Written from the format description -- (MultimediaWiki "Cinepak"): a frame header (flags, size, width, height, strip count), strips with their own V1 / V4 -- codebooks (full or partial updates, kept per strip between frames, inherited from the strip above unless the frame -- flags say otherwise) and the vector chunks (intra, inter with skip flags, V1-only). Colour codebook entries are -- converted once with the Cinepak YUV formula (r = y + 2v, g = y - u/2 - v, b = y + 2u, clamped). Sega FILM's extra -- header bytes are detected on the first frame. Paints over the previous picture. Bit-exact with ffmpeg's cinepak -- decoder. Never throws: every read is range-checked against its chunk. local Picture = require("@formats/codecs/video/Legacy/Picture") local readu8, readu32, writeu32, writeu8 = buffer.readu8, buffer.readu32, buffer.writeu32, buffer.writeu8 local band = bit32.band local Cinepak = {} local MAX_STRIPS = 32 -- a codebook: 256 entries of 4 u32 rgba pixels (16 bytes); palettised streams use the low byte (the index) local ENTRY = 16 local BOOK = 256 * ENTRY export type State = { books: buffer, -- MAX_STRIPS * 2 codebooks (V4 then V1 per strip) filmSkip: number, -- Sega FILM extra header bytes (-1 = not known yet) } function Cinepak.state(): State return { books = buffer.create(MAX_STRIPS * 2 * BOOK), filmSkip = -1 } end local function be16(d: buffer, o: number): number return readu8(d, o) * 256 + readu8(d, o + 1) end local function be24(d: buffer, o: number): number return readu8(d, o) * 65536 + readu8(d, o + 1) * 256 + readu8(d, o + 2) end local function be32(d: buffer, o: number): number return readu8(d, o) * 16777216 + readu8(d, o + 1) * 65536 + readu8(d, o + 2) * 256 + readu8(d, o + 3) end local function clip(v: number): number return if v < 0 then 0 elseif v > 255 then 255 else v end -- a codebook chunk (ids 0x20..0x27): entries of 4 or 6 bytes, all of them or those whose flag bit is set local function codebook(books: buffer, book: number, id: number, d: buffer, p: number, stop: number) local six = band(id, 4) == 0 local partial = band(id, 1) ~= 0 local size = if six then 6 else 4 local flag, mask = 0, 0 for i = 0, 255 do if partial then mask //= 2 if mask == 0 then if p + 4 > stop then return end flag = be32(d, p) p += 4 mask = 0x80000000 end end if not partial or band(flag, mask) ~= 0 then if p + size > stop then return end local e = book + i * ENTRY local y0, y1, y2, y3 = readu8(d, p), readu8(d, p + 1), readu8(d, p + 2), readu8(d, p + 3) if six then local u, v = readu8(d, p + 4), readu8(d, p + 5) if u >= 128 then u -= 256 end if v >= 128 then v -= 256 end local uh = if u < 0 then -((-u) // 2) else u // 2 -- C division (towards zero) local dr, dg, db = 2 * v, -uh - v, 2 * u writeu32(books, e, clip(y0 + dr) + clip(y0 + dg) * 256 + clip(y0 + db) * 65536 + 0xFF000000) writeu32(books, e + 4, clip(y1 + dr) + clip(y1 + dg) * 256 + clip(y1 + db) * 65536 + 0xFF000000) writeu32(books, e + 8, clip(y2 + dr) + clip(y2 + dg) * 256 + clip(y2 + db) * 65536 + 0xFF000000) writeu32(books, e + 12, clip(y3 + dr) + clip(y3 + dg) * 256 + clip(y3 + db) * 65536 + 0xFF000000) else writeu32(books, e, y0 * 0x10101 + 0xFF000000) writeu32(books, e + 4, y1 * 0x10101 + 0xFF000000) writeu32(books, e + 8, y2 * 0x10101 + 0xFF000000) writeu32(books, e + 12, y3 * 0x10101 + 0xFF000000) end p += size end end end -- the vectors of a strip (ids 0x30 intra, 0x31 inter with skip flags, 0x32 V1 only); false on truncated data local function vectors(pic: Picture.Picture, books: buffer, v4: number, v1: number, id: number, d: buffer, p: number, stop: number, x1: number, y1: number, x2: number, y2: number): boolean local stride = pic.stride local pal = pic.idx local px = (pal or pic.rgba) :: buffer local flag, mask = 0, 0 local inter = band(id, 1) ~= 0 local v1only = band(id, 2) ~= 0 for y = y1, y2 - 1, 4 do for x = x1, x2 - 1, 4 do local paint = true if inter then mask //= 2 if mask == 0 then if p + 4 > stop then return false end flag = be32(d, p) p += 4 mask = 0x80000000 end paint = band(flag, mask) ~= 0 end if paint then local useV1 = v1only if not v1only then mask //= 2 if mask == 0 then if p + 4 > stop then return false end flag = be32(d, p) p += 4 mask = 0x80000000 end useV1 = band(flag, mask) == 0 end local o = y * stride + x if useV1 then if p >= stop then return false end local e = v1 + readu8(d, p) * ENTRY p += 1 -- each of the 4 entries fills a 2x2 square local a, b, c, e4 = readu32(books, e), readu32(books, e + 4), readu32(books, e + 8), readu32(books, e + 12) if pal then a, b, c, e4 = a % 256, b % 256, c % 256, e4 % 256 writeu32(px, o, a * 0x0101 + b * 0x01010000) writeu32(px, o + stride, a * 0x0101 + b * 0x01010000) writeu32(px, o + 2 * stride, c * 0x0101 + e4 * 0x01010000) writeu32(px, o + 3 * stride, c * 0x0101 + e4 * 0x01010000) else for r = 0, 3 do local q = (o + r * stride) * 4 local l, rr = if r < 2 then a else c, if r < 2 then b else e4 writeu32(px, q, l) writeu32(px, q + 4, l) writeu32(px, q + 8, rr) writeu32(px, q + 12, rr) end end else if p + 4 > stop then return false end local e0 = v4 + readu8(d, p) * ENTRY local e1 = v4 + readu8(d, p + 1) * ENTRY local e2 = v4 + readu8(d, p + 2) * ENTRY local e3 = v4 + readu8(d, p + 3) * ENTRY p += 4 -- entry k covers the 2x2 square k (top-left, top-right, bottom-left, bottom-right) if pal then for r = 0, 3 do local l, rr = if r < 2 then e0 else e2, if r < 2 then e1 else e3 local off = (r % 2) * 8 local q = o + r * stride writeu8(px, q, readu8(books, l + off)) writeu8(px, q + 1, readu8(books, l + off + 4)) writeu8(px, q + 2, readu8(books, rr + off)) writeu8(px, q + 3, readu8(books, rr + off + 4)) end else for r = 0, 3 do local l, rr = if r < 2 then e0 else e2, if r < 2 then e1 else e3 local off = (r % 2) * 8 local q = (o + r * stride) * 4 writeu32(px, q, readu32(books, l + off)) writeu32(px, q + 4, readu32(books, l + off + 4)) writeu32(px, q + 8, readu32(books, rr + off)) writeu32(px, q + 12, readu32(books, rr + off + 4)) end end end end end end return true end -- decode(pic, data, state, hasPalette) -> picture?, message: true = a picture; false + nil = no picture for this -- packet (an empty frame); false + message = an error function Cinepak.decode(pic: Picture.Picture, d: buffer, st: State, newPalette: boolean): (boolean, string?) local n = buffer.len(d) if n < 10 then return false, "frame too short" end local strips = be16(d, 8) if strips == 0 and not (pic.idx and newPalette) then return false, nil end local size = be24(d, 1) if n < size * 5 // 100 then return false, "frame shorter than its header says" end if st.filmSkip < 0 then if size == 0 then return false, "unsupported: a zero frame size" end if size ~= n and n % size ~= 0 then if n >= 16 and readu8(d, 10) == 0xFE and readu8(d, 11) == 0 and readu8(d, 12) == 0 and readu8(d, 13) == 6 and readu8(d, 14) == 0 and readu8(d, 15) == 0 then st.filmSkip = 6 else st.filmSkip = 2 end else st.filmSkip = 0 end end local skip = st.filmSkip if n < 10 + skip + strips * 12 then return false, "truncated strip headers" end if strips > 0 then local ss = be24(d, 10 + skip + 1) if ss < 12 or ss > size then return false, "bad strip size" end end local flags = readu8(d, 0) local p = 10 + skip strips = math.min(strips, MAX_STRIPS) local books = st.books -- the coded area: the size rounded up to whole blocks (the picture's plane) local cw, ch = pic.stride, pic.rows local y0 = 0 for i = 0, strips - 1 do if p + 12 > n then return true, nil end local sy1 = be16(d, p + 4) local sx1 = be16(d, p + 6) local sy2, sx2 = be16(d, p + 8), be16(d, p + 10) if sy1 == 0 then sy1 = y0 sy2 = y0 + sy2 end local ssize = be24(d, p + 1) - 12 if ssize < 0 then return true, nil end p += 12 local stop = math.min(n, p + ssize) local v4 = i * 2 * BOOK local v1 = v4 + BOOK if i > 0 and band(flags, 1) == 0 then buffer.copy(books, v4, books, v4 - 2 * BOOK, 2 * BOOK) end -- the strip's chunks: codebooks, then one vector chunk -- (strips start on block boundaries in every known stream: the plane holds whole blocks only) if sx2 > cw or sy2 > ch or sx1 >= sx2 or sy1 >= sy2 or band(sx1, 3) ~= 0 or band(sy1, 3) ~= 0 then return true, nil end local q = p local done = false while q + 4 <= stop do local id = readu8(d, q) local csize = be24(d, q + 1) - 4 if csize < 0 then return true, nil end q += 4 local cstop = math.min(stop, q + csize) if id == 0x20 or id == 0x21 or id == 0x24 or id == 0x25 then codebook(books, v4, id, d, q, cstop) elseif id == 0x22 or id == 0x23 or id == 0x26 or id == 0x27 then codebook(books, v1, id, d, q, cstop) elseif id == 0x30 or id == 0x31 or id == 0x32 then if not vectors(pic, books, v4, v1, id, d, q, cstop, sx1, sy1, sx2, sy2) then return true, nil end done = true break end q = cstop end if not done then return true, nil end p = stop y0 = sy2 end return true, nil end return Cinepak