# AVI fixtures (tools/fixtures.py): files made by ffmpeg (MJPEG + PCM with idx1, the same with OpenDML indexes added # here, MJPEG + IMA # ADPCM, MPEG-1 + MP2, uncompressed BGR24 / BGRA / I420 video) and edits made here (the OpenDML indexes renamed to # JUNK so idx1 is used; idx1 renamed too so the movi list is scanned; a truncated file). For each file: ffprobe's # streams and packets, ffmpeg's frames (CRC-32 of yuv420p, or of rgba for uncompressed RGB; the first frames raw for # PSNR checks) and ffmpeg's decoded audio (f32). # tests/gen/avi_cases.luau { { name, data, streams, packets, frames, yuv, rgba, pcm, ... } } import importlib.util, os, tempfile _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"] W, H = 160, 120 SRC = ["-f", "lavfi", "-i", "testsrc2=size=%dx%d:rate=10" % (W, H)] def sine(rate, ch=1): return ["-f", "lavfi", "-i", "sine=frequency=330:sample_rate=%d" % rate, "-ac", str(ch)] def rename(data, old, new): """every chunk id `old` renamed to `new` (same length), found by walking the RIFF lists""" b = bytearray(data) def walk(p, end): while p + 8 <= end: cid = bytes(b[p:p + 4]) size = int.from_bytes(b[p + 4:p + 8], "little") if cid in (b"RIFF", b"LIST"): walk(p + 12, min(end, p + 8 + size)) elif cid == old: b[p:p + 4] = new p += 8 + size + (size & 1) walk(0, len(b)) return bytes(b) def chunks(b, p, end): out = [] while p + 8 <= end: cid = b[p:p + 4] size = int.from_bytes(b[p + 4:p + 8], "little") out.append((cid, p, size)) p += 8 + size + (size & 1) return out def ck(cid, body): return cid + len(body).to_bytes(4, "little") + body + (b"\0" if len(body) & 1 else b"") def add_odml(data): """the same AVI with OpenDML indexes: an indx super index in every strl pointing at an ix## standard index appended to the movi list (built from idx1, which is kept)""" b = data top = chunks(b, 12, len(b)) hdrl = next(c for c in top if c[0] == b"LIST" and b[c[1] + 8:c[1] + 12] == b"hdrl") movi = next(c for c in top if c[0] == b"LIST" and b[c[1] + 8:c[1] + 12] == b"movi") idx1 = next(c for c in top if c[0] == b"idx1") movi_base = movi[1] + 8 # the 'movi' fourcc streams = {} for k in range(idx1[2] // 16): e = idx1[1] + 8 + k * 16 cid = b[e:e + 4] flags, off, size = (int.from_bytes(b[e + 4 + 4 * i:e + 8 + 4 * i], "little") for i in range(3)) sn = int(cid[:2]) streams.setdefault(sn, (cid, []))[1].append((movi_base + off + 8, size, bool(flags & 0x10))) n = len([c for c in chunks(b, hdrl[1] + 12, hdrl[1] + 8 + hdrl[2]) if c[0] == b"LIST" and b[c[1] + 8:c[1] + 12] == b"strl"]) INDX = 8 + 24 + 16 # one super index entry delta = n * INDX # the ix## chunks at the end of the movi list ixs = [] pos = movi[1] + 8 + movi[2] + delta # where the first ix chunk starts (movi grows by them) for sn in range(n): cid, ents = streams.get(sn, (b"%02dwb" % sn, [])) body = (2).to_bytes(2, "little") + bytes([0, 1]) + len(ents).to_bytes(4, "little") + cid + \ (0).to_bytes(8, "little") + bytes(4) for off, size, key in ents: body += (off + delta).to_bytes(4, "little") + (size | (0 if key else 0x80000000)).to_bytes(4, "little") chunk = ck(b"ix%02d" % sn, body) ixs.append((pos, len(chunk), len(ents), cid, chunk)) pos += len(chunk) # hdrl with an indx in every strl out_h = b"" si = 0 for c in chunks(b, hdrl[1] + 12, hdrl[1] + 8 + hdrl[2]): raw = b[c[1]:c[1] + 8 + c[2] + (c[2] & 1)] if c[0] == b"LIST" and b[c[1] + 8:c[1] + 12] == b"strl": p, ln, cnt, cid, _ = ixs[si] indx = (4).to_bytes(2, "little") + bytes([0, 0]) + (1).to_bytes(4, "little") + cid + bytes(12) + \ p.to_bytes(8, "little") + ln.to_bytes(4, "little") + cnt.to_bytes(4, "little") raw = ck(b"LIST", b[c[1] + 8:c[1] + 8 + c[2]] + ck(b"indx", indx)) si += 1 out_h += raw out = b"" for c in top: if c is hdrl: out += ck(b"LIST", b"hdrl" + out_h) elif c is movi: out += ck(b"LIST", b[c[1] + 8:c[1] + 8 + c[2]] + b"".join(x[4] for x in ixs)) else: out += b[c[1]:c[1] + 8 + c[2] + (c[2] & 1)] res = b"RIFF" + (len(out) + 4).to_bytes(4, "little") + b"AVI " + out assert res[ixs[0][0]:ixs[0][0] + 2] == b"ix", "odml layout" return res def case(tmp, name, rgba=False, raw_frames=2, pcm_frames=8192): path = os.path.join(tmp, name) c = {"name": name, "data": open(path, "rb").read(), "streams": V.streams(path), "packets": V.packets(path)} vs = [s for s in c["streams"] if s["kind"] == "video"] if vs: w, h = vs[0]["width"], vs[0]["height"] if rgba: raw = V.ffmpeg(["-i", path, "-map", "0:v:0", "-f", "rawvideo", "-pix_fmt", "rgba", "-"]) n = w * h * 4 c["rgba"] = [V.crc(raw[i:i + n]) for i in range(0, len(raw) - n + 1, n)] else: frames = V.yuv_frames(path, w, h, codec=vs[0]["codec"]) c["frames"] = [V.crc(f) for f in frames] c["yuv"] = frames[:raw_frames] if any(s["kind"] == "audio" for s in c["streams"]): pcm = V.pcm_f32(path) c["pcm"] = pcm[:pcm_frames * 4] c["pcmBytes"] = len(pcm) return c def generate(ctx): tmp = tempfile.mkdtemp(prefix="avifx_") ff = lambda *a: V.ffmpeg(list(a), cwd=tmp) ff(*SRC, *sine(44100, 2), "-t", "1", "-c:v", "mjpeg", "-q:v", "3", "-c:a", "pcm_s16le", "mjpeg_pcm.avi") ff(*SRC, *sine(22050), "-t", "1", "-c:v", "mjpeg", "-q:v", "5", "-c:a", "adpcm_ima_wav", "mjpeg_ima.avi") ff(*SRC, *sine(44100), "-t", "1", "-r", "25", "-c:v", "mpeg1video", "-b:v", "500k", "-c:a", "mp2", "mpeg1_mp2.avi") ff(*SRC, "-t", "0.5", "-c:v", "rawvideo", "-pix_fmt", "bgr24", "bgr24.avi") ff(*SRC, "-t", "0.5", "-c:v", "rawvideo", "-pix_fmt", "bgra", "bgra.avi") ff(*SRC, "-t", "0.5", "-c:v", "rawvideo", "-pix_fmt", "yuv420p", "i420.avi") ff(*SRC, *sine(8000), "-t", "1", "-c:v", "mjpeg", "-c:a", "pcm_u8", "pcm_u8.avi") full = open(os.path.join(tmp, "mjpeg_pcm.avi"), "rb").read() idx1 = rename(full, b"indx", b"JUNK") open(os.path.join(tmp, "idx1_only.avi"), "wb").write(idx1) open(os.path.join(tmp, "no_index.avi"), "wb").write(rename(idx1, b"idx1", b"JUNK")) open(os.path.join(tmp, "odml.avi"), "wb").write(add_odml(full)) cases = [case(tmp, "mjpeg_pcm.avi"), case(tmp, "mjpeg_ima.avi"), case(tmp, "mpeg1_mp2.avi"), case(tmp, "bgr24.avi", rgba=True), case(tmp, "bgra.avi", rgba=True), case(tmp, "i420.avi"), case(tmp, "pcm_u8.avi"), case(tmp, "idx1_only.avi"), case(tmp, "no_index.avi"), case(tmp, "odml.avi")] cases.append({"name": "truncated.avi", "data": full[: len(full) * 6 // 10], "truncated": True}) ctx.emit("avi_cases", cases)