# H.263 / Sorenson Spark fixtures for codecs/video/H263 (tools/fixtures.py): streams from ffmpeg's encoders (h263: # sub-QCIF .. 4CIF, GOB headers, adaptive quantisation (DQUANT), low and high bitrates; h263p: PLUSPTYPE with custom # sizes, the rounding type, unrestricted vectors, slice structure; flv: Sorenson Spark with its standard, 8-bit and 16-bit custom # sizes, 11-bit escapes, disposable inter pictures patched in), high and low motion (testsrc2, mandelbrot, scroll), # each with `ffmpeg -flags +bitexact -idct int` decoding as the reference (the integer IDCT codecs/video/Dsp # reproduces; bitexact: ffmpeg's x86 half-sample averages without rounding are approximations otherwise). # tests/gen/h263_.luau { name, flv, width, height, frames, file (base64), units = { { offset, length } } (one # picture each), crcs = { { y, u, v } } (CRC-32 of each frame's planes), first (the first # yuv frame, base64 zlib) } # tests/gen/h263_bad.luau { aic, obmc: one picture each with an unsupported H.263+ option (base64) } # tests/gen/h263_bench.luau { { name, flv, width, height, file, units } }: CIF H.263 and Sorenson Spark import base64, os, shutil, subprocess, tempfile, zlib def b64(b): return base64.b64encode(b).decode() def z64(b): return base64.b64encode(zlib.compress(b, 9)).decode() FF = ["-hide_banner", "-loglevel", "error", "-y"] def src(kind, w, h, t, rate=25, vf=None): s = "%s=size=%dx%d:rate=%d" % (kind, w, h, rate) if vf: s += "," + vf return ["-f", "lavfi", "-i", s, "-t", str(t)] H263 = ["-c:v", "h263", "-f", "h263"] H263P = ["-threads", "1", "-c:v", "h263p", "-f", "rawvideo"] FLV = ["-c:v", "flv", "-f", "flv"] # name: (ffmpeg input + encoder args, flv) CASES = { "qcif": (src("testsrc2", 176, 144, 1.0) + ["-b:v", "200k", "-g", "12"] + H263, False), "cif_mandel": (src("mandelbrot", 352, 288, 0.6) + ["-b:v", "800k", "-g", "8"] + H263, False), "sqcif_scroll": (src("testsrc2", 128, 96, 1.0, vf="scroll=h=0.05:v=0.03") + ["-q:v", "4", "-g", "30"] + H263, False), "qcif_q1": (src("mandelbrot", 176, 144, 0.4) + ["-qmin", "1", "-q:v", "1", "-g", "5"] + H263, False), "qcif_aq": (src("testsrc2", 176, 144, 0.8, vf="scroll=h=0.01") + ["-b:v", "60k", "-lumi_mask", "0.3", "-dark_mask", "0.3", "-p_mask", "0.2", "-g", "20"] + H263, False), "cif_gob": (src("testsrc2", 352, 288, 0.6, vf="scroll=v=0.02") + ["-b:v", "500k", "-ps", "300", "-g", "10"] + H263, False), "4cif_gob": (src("mandelbrot", 704, 576, 0.16) + ["-b:v", "2M", "-ps", "1200", "-g", "4"] + H263, False), "plus_custom": (src("testsrc2", 320, 240, 0.8, vf="scroll=h=0.02:v=0.01") + ["-b:v", "300k", "-g", "15"] + H263P, False), "plus_odd": (src("mandelbrot", 196, 148, 0.6) + ["-b:v", "250k", "-g", "8"] + H263P, False), "plus_umv": (src("testsrc2", 200, 152, 0.8, vf="scroll=h=0.06:v=0.04") + ["-umv", "1", "-q:v", "5", "-g", "25"] + H263P, False), "plus_slices": (src("mandelbrot", 352, 288, 0.48) + ["-b:v", "600k", "-g", "6"] + H263P + ["-threads", "4", "-slices", "4"], False), "plus_slices_umv": (src("testsrc2", 176, 144, 0.8, vf="scroll=h=-0.04:v=0.05") + ["-umv", "1", "-q:v", "4", "-g", "20"] + H263P + ["-threads", "3", "-slices", "3", "-ps", "200"], False), "flv_320": (src("testsrc2", 320, 240, 1.0) + ["-b:v", "300k", "-g", "12"] + FLV, True), "flv_odd": (src("mandelbrot", 100, 75, 0.6) + ["-b:v", "150k", "-g", "8"] + FLV, True), "flv_wide": (src("testsrc2", 400, 224, 0.4, vf="scroll=h=0.03") + ["-b:v", "400k", "-g", "6"] + FLV, True), "flv_q1": (src("mandelbrot", 160, 120, 0.4) + ["-qmin", "1", "-q:v", "1", "-g", "4"] + FLV, True), "flv_scroll": (src("testsrc2", 176, 144, 1.0, vf="scroll=h=0.05:v=-0.04") + ["-q:v", "6", "-g", "50"] + FLV, True), } def h263_units(es): starts = [] i = 0 while True: i = es.find(b"\x00\x00", i) if i < 0 or i + 2 >= len(es): break if es[i + 2] & 0xFC == 0x80: starts.append(i) i += 3 else: i += 1 return [[s, (starts[k + 1] if k + 1 < len(starts) else len(es)) - s] for k, s in enumerate(starts)] def flv_tags(data): """the video tags of an FLV file: [(offset of the tag header, payload offset, payload length)]""" p = int.from_bytes(data[5:9], "big") + 4 out = [] while p + 11 <= len(data): kind = data[p] & 0x1F size = int.from_bytes(data[p + 1:p + 4], "big") if kind == 9 and size > 1: out.append((p, p + 12, size - 1)) p += 11 + size + 4 return out def flv_disposable(data): """every third inter picture of a Sorenson FLV marked disposable (tag frame type 3, picture type 2)""" b = bytearray(data) inter = 0 for tag, off, n in flv_tags(data): if b[tag + 11] >> 4 != 2: continue inter += 1 if inter % 3 != 0: continue b[tag + 11] = (3 << 4) | (b[tag + 11] & 15) # picture size field: bits 30..32 after the start of the picture bits = int.from_bytes(b[off:off + 8], "big") fmt = (bits >> (64 - 33)) & 7 p = 33 + (16 if fmt == 0 else 32 if fmt == 1 else 0) # the picture type's first bit byte, bit = p // 8, p % 8 word = int.from_bytes(b[off + byte:off + byte + 2], "big") shift = 16 - bit - 2 word = (word & ~(3 << shift)) | (2 << shift) b[off + byte:off + byte + 2] = word.to_bytes(2, "big") return bytes(b) def generate(ctx): ffmpeg = shutil.which("ffmpeg") or ctx.nix("ffmpeg", ["sh", "-c", "command -v ffmpeg"]).decode().strip() ffprobe = shutil.which("ffprobe") or os.path.join(os.path.dirname(ffmpeg), "ffprobe") def ff(args): subprocess.run([ffmpeg] + FF + args, check=True, capture_output=True) def size(path): r = subprocess.run([ffprobe, "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", "-of", "csv=p=0", path], check=True, capture_output=True) return tuple(int(x) for x in r.stdout.decode().strip().split(",")[:2]) def reference(path): out = path + ".yuv" ff(["-flags", "+bitexact", "-idct", "int", "-i", path, "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "yuv420p", out]) return open(out, "rb").read() def emit(name, path, flv): data = open(path, "rb").read() if flv: tags = flv_tags(data) file = b"".join(data[o:o + n] for _, o, n in tags) units, o = [], 0 for _, _, n in tags: units.append([o, n]) o += n else: file, units = data, h263_units(data) w, h = size(path) yuv = reference(path) cw, ch = (w + 1) // 2, (h + 1) // 2 fs = w * h + 2 * cw * ch n = len(yuv) // fs crcs = [] for k in range(n): f = yuv[k * fs:(k + 1) * fs] crcs.append([zlib.crc32(f[:w * h]), zlib.crc32(f[w * h:w * h + cw * ch]), zlib.crc32(f[w * h + cw * ch:])]) assert n == len(units), "h263 %s: %d frames for %d pictures" % (name, n, len(units)) ctx.emit("h263_" + name, {"name": name, "flv": flv, "width": w, "height": h, "frames": n, "file": b64(file), "units": units, "crcs": crcs, "first": z64(yuv[:fs])}) return file, units with tempfile.TemporaryDirectory(prefix="formats-h263-") as tmp: for name, (args, flv) in CASES.items(): path = os.path.join(tmp, name + (".flv" if flv else ".h263")) ff(args + [path]) emit(name, path, flv) # Sorenson disposable inter pictures (ffmpeg's encoder writes none): patched into a stream; ffmpeg's decoder # does not keep them as references either path = os.path.join(tmp, "disp.flv") ff(src("testsrc2", 160, 120, 1.0, vf="scroll=h=0.02") + ["-b:v", "200k", "-g", "20"] + FLV + [path]) patched = os.path.join(tmp, "disposable.flv") open(patched, "wb").write(flv_disposable(open(path, "rb").read())) emit("flv_disposable", patched, True) # unsupported H.263+ options: one picture each bad = {} for name, extra in (("aic", ["-flags", "+aic"]), ("obmc", ["-obmc", "1"])): path = os.path.join(tmp, name + ".h263") ff(src("testsrc2", 176, 144, 0.04) + extra + H263P + [path]) bad[name] = b64(open(path, "rb").read()) ctx.emit("h263_bad", bad) bench = [] for name, args, flv in (("h263", H263, False), ("flv1", FLV, True)): path = os.path.join(tmp, "bench_" + name + (".flv" if flv else ".h263")) ff(src("mandelbrot", 352, 288, 1.2) + ["-b:v", "1M", "-g", "12"] + args + [path]) data = open(path, "rb").read() if flv: tags = flv_tags(data) file = b"".join(data[o:o + n] for _, o, n in tags) units, o = [], 0 for _, _, n in tags: units.append([o, n]) o += n else: file, units = data, h263_units(data) bench.append({"name": name, "flv": flv, "width": 352, "height": 288, "file": b64(file), "units": units}) ctx.emit("h263_bench", bench)