# FLI / FLC fixtures (tools/fixtures.py): ffmpeg cannot write FLICs, so this encodes them (every chunk type: COLOR_64, # COLOR_256, BRUN, LC / DELTA_FLI, SS2 / DELTA_FLC with line skips and the last-byte opcode, BLACK, COPY, PSTAMP; a # prefix chunk and a ring frame; 15-bit FLC, 16-bit DTA and 24-bit FLC with BRUN / DTA_BRUN / DELTA / DTA_LC / COPY / # DTA_COPY) and uses ffmpeg's decode as the reference (CRC-32 of each frame as rgba). # tests/gen/fli_cases.luau { { name, data, width, height, depth, rate, frames = { crc } } } import importlib.util, os, random, struct, tempfile, zlib _spec = importlib.util.spec_from_file_location("_video_helpers", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_video.py")) V = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(V) INPUTS = ["tools/fixtures/_video.py"] def le16(v): return struct.pack("= 2: out.append(("rep", seq[i], j - i + 1)) i = j + 1 else: k = i while k < len(seq) and k - i < maxrun and not (k + 2 < len(seq) and seq[k] == seq[k + 1] == seq[k + 2]): k += 1 k = max(k, i + 1) out.append(("lit", seq[i:k])) i = k return out def brun_line(units, unit_bytes): """BRUN line body: > 0 repeat, < 0 literal (units are byte strings of unit_bytes each)""" out = bytes([0]) for p in runs(units): if p[0] == "rep": out += bytes([p[2]]) + p[1] else: out += bytes([s8(-len(p[1]))]) + b"".join(p[1]) return out def brun(img, bpp, pixel_runs, typ=15): lines = b"" for row in img: if pixel_runs: units = [row[x] for x in range(len(row))] else: flat = b"".join(row) units = [flat[i:i + 1] for i in range(len(flat))] lines += brun_line(units, bpp) return chunk(typ, lines) def lc(img, prev): """DELTA_FLI (LC): first changed line, line count, per line (skip, run) packets on bytes""" h, w = len(img), len(img[0]) ch = [y for y in range(h) if img[y] != prev[y]] if not ch: return chunk(12, le16(0) + le16(0)) y0, y1 = ch[0], ch[-1] + 1 out = le16(y0) + le16(y1 - y0) for y in range(y0, y1): row, prow = img[y], prev[y] packets = [] x = 0 while x < w: skip = 0 while x < w and row[x] == prow[x] and skip < 255: skip += 1 x += 1 if x >= w: break e = x while e < w and row[e] != prow[e] and e - x < 127: e += 1 seg = [bytes([v]) for v in row[x:e]] pieces = runs(seg) first = True for p in pieces: sk = skip if first else 0 first = False if p[0] == "rep": packets.append(bytes([sk, s8(-p[2])]) + p[1]) else: packets.append(bytes([sk, len(p[1])]) + b"".join(p[1])) x = e out += bytes([len(packets)]) + b"".join(packets) return chunk(12, out) def ss2(img, prev, last_byte=False): """DELTA_FLC (SS2) on 8-bit pixels: word packets, line-skip words, the last-byte opcode on odd lines""" h, w = len(img), len(img[0]) out = b"" count = 0 skip_lines = 0 for y in range(h): row, prow = img[y], prev[y] if row == prow: skip_lines += 1 continue if skip_lines: out += le16(-skip_lines) skip_lines = 0 body = b"" packets = 0 lastop = last_byte and y % 2 == 1 and row[w - 1] != prow[w - 1] ww = w - (1 if lastop else 0) words = [bytes(row[x:x + 2]) for x in range(0, ww - ww % 2, 2)] pwords = [bytes(prow[x:x + 2]) for x in range(0, ww - ww % 2, 2)] i = 0 while i < len(words): skip = 0 while i < len(words) and words[i] == pwords[i] and skip < 127: skip += 1 i += 1 if i >= len(words): break e = i while e < len(words) and words[e] != pwords[e] and e - i < 127: e += 1 first = True for p in runs(words[i:e]): sk = skip * 2 if first else 0 first = False if p[0] == "rep": body += bytes([sk, s8(-p[2])]) + p[1] else: body += bytes([sk, len(p[1])]) + b"".join(p[1]) packets += 1 i = e if lastop: out += le16(0x8000 | row[w - 1]) out += le16(packets) + body count += 1 return chunk(7, le16(count) + out) def delta_deep(img, prev, bpp, typ=7): """DELTA / DTA_LC on 15/16/24-bit pixels: negative words skip lines, packets (skip pixels, run)""" h, w = len(img), len(img[0]) out, count, skip_lines = b"", 0, 0 for y in range(h): row, prow = img[y], prev[y] if row == prow: skip_lines += 1 continue if skip_lines: out += le16(-skip_lines) skip_lines = 0 body, packets, x = b"", 0, 0 while x < w: skip = 0 while x < w and row[x] == prow[x] and skip < 255: skip += 1 x += 1 if x >= w: break e = x while e < w and row[e] != prow[e] and e - x < 127: e += 1 first = True for p in runs(row[x:e]): sk = skip if first else 0 first = False if p[0] == "rep": body += bytes([sk, s8(-p[2])]) + p[1] else: body += bytes([sk, len(p[1])]) + b"".join(p[1]) packets += 1 x = e out += le16(packets) + body count += 1 return chunk(typ, le16(count) + out) def copy8(img): w = len(img[0]) pad = (w + 3) // 4 * 4 - w return le32(6 + len(img) * (w + pad)) + le16(16) + b"".join(bytes(r) + bytes(pad) for r in img) def copy_deep(img, bpp, typ=16): w = len(img[0]) body = b"".join(b"".join(r) + (bytes(bpp) if w % 2 else b"") for r in img) return le32(6 + len(body)) + le16(typ) + body # ---------------------------------------------------------------------------------------------------- pictures def idx_frames(w, h, n, colors, seed): rnd = random.Random(seed) out = [] for t in range(n): img = [[(x // 6 + y // 4 + (t // 4)) % colors for x in range(w)] for y in range(h)] sx, sy = (t * 7) % (w - 16), (t * 3) % (h - 12) for y in range(sy, sy + 12): for x in range(sx, sx + 16): img[y][x] = (200 + t) % colors for y in range(h // 2, h // 2 + 4): for x in range(w // 2, w // 2 + 10): img[y][x] = rnd.randrange(colors) out.append(img) return out def deep_frames(w, h, n, bpp, seed): rnd = random.Random(seed) out = [] for t in range(n): img = [] for y in range(h): row = [] for x in range(w): v = ((x * 37 + y * 11 + t * 3) // 8) & 0x7FFF if bpp == 2 else ((x * 4) & 255, (y * 5) & 255, (t * 20) & 255) if 10 + t <= x < 26 + t and 8 <= y < 20: v = 0x7C00 if bpp == 2 else (250, 10, 10) if bpp == 2 and h // 2 <= y < h // 2 + 3 and w // 2 <= x < w // 2 + 7: v = rnd.randrange(65536) row.append(struct.pack("