diff --git a/docs/guide/video.md b/docs/guide/video.md index 99e0395..28e87c7 100644 --- a/docs/guide/video.md +++ b/docs/guide/video.md @@ -288,6 +288,77 @@ editable:WritePixelsBuffer(Vector2.zero, editable.Size, v:toRGBA(frame).pixels) See [FORMATS.md](../FORMATS.md). Subtitle tracks of Matroska files are read as cues by `F.format("mkv").subtitles`. + +| fli / flc / flx | yes | - | FLIC (8-bit palettised, 15 / 16 / 24-bit) | +| smk | yes | - | Smacker video (SMK2 / SMK4) + Smacker audio / PCM | + +AVI, MOV / MP4 and MKV (V_MS/VFW/FOURCC, V_QUICKTIME) also decode the legacy codecs: Cinepak, Microsoft Video 1, +QuickTime Animation (RLE) and QuickTime Graphics (SMC). See [Legacy video](#legacy-video). + + +## Legacy video + +The 1990s formats and codecs decode to **rgba8** frames (they are palettised or RGB, not YUV), each frame painted +over the previous one, as the codecs define. Decoding is bit-exact with ffmpeg's. + +| Codec id | Name | Containers | Depths | +|---|---|---|---| +| `flic` | Autodesk FLI / FLC | fli | 8-bit palettised, 15 / 16 / 24-bit | +| `smackvid` | Smacker video | smk | 8-bit palettised | +| `cinepak` | Cinepak (`cvid`) | avi, mov, mkv | 24-bit, grey, 8-bit palettised | +| `msvideo1` | Microsoft Video 1 (`CRAM`, `MSVC`, `WHAM`) | avi, mov, mkv | 16-bit (5-5-5), 8-bit palettised | +| `qtrle` | QuickTime Animation (`rle `) | mov, mkv | 1, 2, 4, 8 (palettised or grey), 16, 24, 32 (alpha) | +| `smc` | QuickTime Graphics (`smc `) | mov, mkv | 8-bit palettised | + +Palettes come from the container: `track.palette` (256 entries, 4 bytes each: R, G, B, A) holds the AVI +BITMAPINFO palette or the QuickTime colour table (or the Macintosh default table it refers to); FLIC and Smacker +frames carry their own palette changes. + +```luau +local v = assert(F.video.open(cinepakBytes)) -- Cinepak in a QuickTime movie +assert(v.format == "mov" and v.codec == "cinepak") +local frames = 0 +while true do + local f = v:nextFrame() + if not f then break end + assert(f.image.format == "rgba8" and f.image.width == 64) + frames += 1 +end +assert(frames == v.frames) +``` + +FLIC files (`.fli` at 70 Hz ticks, `.flc` / `.flx` in milliseconds) open as videos; the ring frame (an extra frame +leading back to the first, for looping) is not shown unless you ask for it. + +```luau +local v = assert(F.video.open(flcBytes)) +print(v.format, v.codec, v.width, v.height, v.frameRate) -- fli flic 64 48 10 +assert(v.track.bitsPerPixel == 8 and v.frames == 10) +local f = assert(v:nextFrame()) +local r, g, b, a = F.ops.image.getPixel(f.image, 0, 0) -- palette colours, opaque +assert(r and g and b and a == 255) +local all = assert(F.format("fli").open(F.Bytes.reader(flcBytes), { ringFrame = true })) +assert(all.codec == "flic") +``` + +Smacker files carry up to seven audio tracks. The format decodes Smacker's packed audio (and raw PCM tracks) itself: +`v.audio` is the first track, `F.format("smk").openAudio(demuxer, track)` opens any of them. + +```luau +local v = assert(F.video.open(smkBytes)) +assert(v.format == "smk" and v.codec == "smackvid") +local a = assert(v.audio, tostring(v.audioError)) -- packed 16-bit stereo, 22050 Hz +assert(a.sampleRate == 22050 and a.channels == 2) +local Smk = F.format("smk") +for _, t in v.demuxer.tracks do + if t.kind == "audio" then + local dec = assert(Smk.openAudio(v.demuxer, t)) -- smackaudio, pcm_u8 or pcm_s16le + local pcm, n = dec:read(1000) + assert(n == 1000 and buffer.len(pcm) == n * dec.channels * 4) + end +end +``` + ## Gotchas - Pure-Luau decoding is CPU-bound: H.264 and VP8 are practical at low resolutions. Measure with `tools/bench.luau` diff --git a/docs/video.md b/docs/video.md index 184ca45..f2469d6 100644 --- a/docs/video.md +++ b/docs/video.md @@ -28,6 +28,7 @@ F.convert(gif, "avi") -- animated images become videos (un | `codecs/video/Video/Source` | what writers write: copied packets or frames encoded by a registry encoder, PCM from the audio | | `codecs/image/Yuv` | yuv420p <-> rgba8 (BT.601 / 709, limited / full range from `image.meta.yuv`), range remap | | `codecs/image/Vp8`, `Jpeg`, `Mpeg1` | the decoders (and the JPEG encoder) | +| `codecs/video/Legacy` | FLIC, Smacker, Cinepak, Microsoft Video 1, QuickTime RLE / SMC: rgba8, palettes from `track.palette` | | `video/` | the formats: demux into `containers/Demux`, `open` = `Video.open(demuxer, opts)`, `encode` via Source | Audio tracks reach the audio formats through the api hook `opts.openAudio(demuxer, track)` (a format never requires diff --git a/tools/fixtures/docs_legacy.py b/tools/fixtures/docs_legacy.py new file mode 100644 index 0000000..719f8fd --- /dev/null +++ b/tools/fixtures/docs_legacy.py @@ -0,0 +1,33 @@ +# Docs inputs for the legacy video sections of docs/guide/video.md (tools/fixtures.py; tools/docs_check.py reads them): +# a 64x48 FLC (tools/fixtures/fli.py's encoder), a 64x48 SMK4 with a packed stereo and a raw mono audio track +# (tools/fixtures/smk.py's encoder) and a 64x48 Cinepak MOV from ffmpeg. +# tests/gen/docs_legacy.luau { files = { [name] = bytes }, vars = { flcBytes = "anim.flc", ... } } +import importlib.util, os, tempfile + +INPUTS = ["tools/docs_check.py", "tools/fixtures/fli.py", "tools/fixtures/smk.py", "tools/fixtures/_video.py"] + + +def load(ctx, name): + spec = importlib.util.spec_from_file_location("docs_legacy_" + name.replace("/", "_"), os.path.join(ctx.root, "tools", name + ".py")) + m = importlib.util.module_from_spec(spec) + spec.loader.exec_module(m) + return m + + +def generate(ctx): + dc = load(ctx, "docs_check") + fli = load(ctx, "fixtures/fli") + smk = load(ctx, "fixtures/smk") + v = load(ctx, "fixtures/_video") + files = {} + w, h, n = 64, 48, 10 + imgs = fli.idx_frames(w, h, n, 256, 5) + frames = [fli.frame([fli.color([(0, fli.pal(256, 5, False))], False), fli.brun([[bytes([x]) for x in r] for r in imgs[0]], 1, True)])] + frames += [fli.frame([fli.ss2(imgs[t], imgs[t - 1])]) for t in range(1, n)] + files["anim.flc"] = fli.file(0xAF12, w, h, 8, 100, frames) + files["intro.smk"] = smk.smk_file(4, 64, 48, 10, 100, [("packed", 22050, 2, 16), ("raw", 11025, 1, 8)], False, 7) + with tempfile.TemporaryDirectory() as d: + out = os.path.join(d, "cinepak.mov") + v.ffmpeg(["-f", "lavfi", "-i", "testsrc=size=64x48:rate=10:duration=1", "-c:v", "cinepak", out]) + files["cinepak.mov"] = open(out, "rb").read() + dc.emit_inputs(ctx, files, {"flcBytes": "anim.flc", "smkBytes": "intro.smk", "cinepakBytes": "cinepak.mov"})