# Theora fixtures for codecs/video/Theora and video/ogv (tools/fixtures.py): Ogg Theora files made by ffmpeg's # libtheora encoder (testsrc2 / mandelbrot / static bars, several qualities and bit rates, odd sizes = picture regions # smaller than the coded frame (the encoder keeps even sizes; two cases patch odd ones at offsets), 4:2:2 and 4:4:4, short and long keyframe intervals), one with an empty packet (a # repeated frame) spliced in, an Ogg with Vorbis audio, and Theora in Matroska. The reference is ffmpeg's own decode # (its vp3 decoder, bit-exact with libtheora): `-f framecrc` in the stream's native pixel format, one Adler-32 per # frame over the packed planes of the picture region, with the frame's pts. # tests/gen/theora_.luau { name, width, height, pixfmt, file (base64 .ogv), frames = { { pts, adler } } } # tests/gen/theora_dupe.luau the same, file with an empty packet after the 5th frame (frames: of the original) # tests/gen/theora_av.luau { file (base64 .ogv with Vorbis), frames, pcm (f32le of ffmpeg's audio decode, base64), # rate, channels } # tests/gen/theora_opus.luau, theora_flac.luau the same with mono Opus / FLAC audio # tests/gen/theora_mkv.luau { file (base64 .mkv), frames } # tests/gen/theora_bench.luau { { name, file (base64 .ogv, 320x240), width, height } } for tools/bench.luau import base64, os, shutil, struct, subprocess, tempfile, zlib FF = ["-hide_banner", "-loglevel", "error", "-y"] def b64(b): return base64.b64encode(b).decode() def src(kind, w, h, rate, t): return ["-f", "lavfi", "-i", "%s=size=%dx%d:rate=%s" % (kind, w, h, rate), "-t", str(t)] # name: (input args, encoder args, pixel format) CASES = { "q6": (src("testsrc2", 320, 240, 25, 1.2), ["-q:v", "6", "-g", "12"], "yuv420p"), "q0": (src("testsrc2", 176, 144, 25, 1), ["-q:v", "0", "-g", "30"], "yuv420p"), "q10": (src("mandelbrot", 176, 144, 25, 0.6), ["-q:v", "10", "-g", "8"], "yuv420p"), "motion": (src("mandelbrot", 240, 160, 30, 1.5), ["-b:v", "300k", "-g", "64"], "yuv420p"), "odd": (src("testsrc2", 174, 98, 25, 1), ["-q:v", "5", "-g", "10"], "yuv420p"), "static": (src("smptebars", 160, 120, 25, 1.2), ["-q:v", "4", "-g", "100"], "yuv420p"), "yuv422": (src("testsrc2", 176, 144, 25, 1), ["-q:v", "6", "-g", "12"], "yuv422p"), "yuv444": (src("mandelbrot", 144, 112, 25, 1), ["-q:v", "6", "-g", "12"], "yuv444p"), "odd444": (src("testsrc2", 98, 62, 25, 0.6), ["-q:v", "7", "-g", "6"], "yuv444p"), } # ffmpeg's encoder writes even picture sizes at offset 0: these cases patch the identification header to an odd # picture region at an offset (width, height, x, y from the bottom); the reference is ffmpeg's decode of the patched # file (-flags unaligned: the exact crop) PATCH = { "crop": (src("testsrc2", 176, 112, 25, 0.6), ["-q:v", "6", "-g", "8"], "yuv420p", (171, 97, 3, 5)), "crop422": (src("mandelbrot", 128, 96, 25, 0.6), ["-q:v", "6", "-g", "8"], "yuv422p", (117, 81, 5, 2)), } def crc32(b): return zlib.crc32(b) & 0xFFFFFFFF def ogg_pages(data): """yield (header fields, segment table, body) of each Ogg page""" i = 0 while i + 27 <= len(data): assert data[i:i + 4] == b"OggS" flags, granule, serial, seq = data[i + 5], struct.unpack_from("> 24) ^ x) & 0xFF]) & 0xFFFFFFFF return c def page(flags, granule, serial, seq, packets_segs, body): head = b"OggS" + bytes([0, flags]) + struct.pack("> 5 out = [] seq = 0 frames = [] data_packets = packets[3:] data_packets = data_packets[:after] + [b""] + data_packets[after:] key = 0 for k, p in enumerate(packets[:3]): segs = [255] * (len(p) // 255) + [len(p) % 255] out.append(page(2 if k == 0 else 0, 0, sid, seq, segs, p)) seq += 1 for k, p in enumerate(data_packets): n = k + 1 if len(p) > 0 and not (p[0] & 0x40): key = n segs = [255] * (len(p) // 255) + [len(p) % 255] g = (key << shift) | (n - key) out.append(page(4 if k == len(data_packets) - 1 else 0, g, sid, seq, segs, p)) seq += 1 return b"".join(out) def patch_picture(data, w, h, x, y): """rewrite the first page (the identification header) with another picture region""" out = bytearray(data) n = out[26] o = 27 + n # the packet starts here assert out[o:o + 7] == b"\x80theora" out[o + 14:o + 17] = w.to_bytes(3, "big") out[o + 17:o + 20] = h.to_bytes(3, "big") out[o + 20] = x out[o + 21] = y plen = 27 + n + sum(out[27:27 + n]) out[22:26] = b"\0\0\0\0" struct.pack_into("