merge ws/video2

This commit is contained in:
4DBug committed 2026-10-31 15:31:56 -05:00
commit c5f27770fc
63 files changed
+12207 -1509

No files matched your search

+3 -1
View File
@@ -94,7 +94,9 @@ A format never requires another format, so the api passes these to decoders / en
## Host
`F.host` (`src/Formats/host`): `name` (`"roblox"`, `"love"`, `"luajit"`, `"luau"`), `hasFiles`, `readFile`,
`writeFile`, `exists`; Roblox `toEditableImage(img)`; LÖVE `toImageData(img)`, `fromImageData(imageData)`,
`writeFile`, `exists`; Roblox `toEditableImage(img)`, `meshPages(prim, opts?)`, `pageData(prim, page, opts?)`,
`toEditableMesh(prim, page?, opts?)` (a triangle primitive in pages of at most 20k triangles / 60k vertices, each an
EditableMesh; see the Hosts section of the guide); LÖVE `toImageData(img)`, `fromImageData(imageData)`,
`toSoundData(audioOrDecoder)`.
On LuaJIT the host raises the trace limits at load (`jit.opt.start("maxtrace=8000", "maxmcode=65536")`): the big
+13 -6
View File
@@ -27,12 +27,15 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| audio | voc | .voc | yes | yes | streaming: true, depths: 8, 16, lossy: false | `tests/audio/voc.luau` |
| audio | vorbis | .ogg .oga | yes | yes | streaming: true, seek: true, lossy: true, channels: 255 | `tests/audio/vorbis.luau` |
| audio | wav | .wav .wave .rf64 .bw64 | yes | yes | streaming: true, depths: 8, 16, 24, 32, float: true, rf64: true | `tests/audio/wav.luau` |
| video | avi | .avi | yes | yes | streaming: true, video: "mjpeg", "mpeg1", "rawvideo", audio: true, odml: true | `tests/video/avi.luau` |
| video | avi | .avi | yes | yes | streaming: true, video: "mjpeg", "mpeg1", "mpeg2", "rawvideo", audio: true, odml: true | `tests/video/avi.luau` |
| video | fli | .fli .flc .flx | yes | - | streaming: true, video: "flic", depths: 8, 15, 16, 24 | `tests/video/fli.luau` |
| video | h264 | .h264 .264 .avc .jsv .26l .jvt | yes | yes | streaming: true, video: "h264", depths: 8 | `tests/video/h264.luau` |
| video | mkv | .mkv .webm .mka .mk3d | yes | yes | streaming: true, remux: true, video: "vp8", "mjpeg", "mpeg1", "rawvideo", audio: true, subtitles: true | `tests/video/mkv.luau` |
| video | mp4 | .mp4 .m4v .mov .qt .3gp .3g2 .mj2 | yes | yes | streaming: true, remux: true, video: "mjpeg", "mpeg1", "h264", audio: true, fragments: true | `tests/video/mp4.luau` |
| video | mpegps | .mpg .mpeg .vob .m1v .mpv .dat | yes | - | streaming: true, video: "mpeg1", audio: true | `tests/video/mpegps.luau` |
| video | mpegts | .ts .m2ts .mts .tsv | yes | - | streaming: true, video: "mpeg1", "h264", audio: true | `tests/video/mpegts.luau` |
| video | mkv | .mkv .webm .mka .mk3d | yes | yes | streaming: true, remux: true, video: "vp8", "mjpeg", "mpeg1", "mpeg2", "theora", "rawvideo", audio: true, subtitles: true | `tests/video/mkv.luau` |
| video | mp4 | .mp4 .m4v .mov .qt .3gp .3g2 .mj2 | yes | yes | streaming: true, remux: true, video: "mjpeg", "mpeg1", "mpeg2", "h264", audio: true, fragments: true | `tests/video/mp4.luau` |
| video | mpegps | .mpg .mpeg .vob .m1v .mpv .dat | yes | - | streaming: true, video: "mpeg1", "mpeg2", audio: true | `tests/video/mpegps.luau` |
| video | mpegts | .ts .m2ts .mts .tsv | yes | - | streaming: true, video: "mpeg1", "mpeg2", "h264", audio: true | `tests/video/mpegts.luau` |
| video | ogv | .ogv .ogg | yes | yes | streaming: true, remux: true, video: "theora", audio: true | `tests/video/ogv.luau` |
| video | smk | .smk | yes | - | streaming: true, video: "smackvid", audio: "smackaudio", "pcm_u8", "pcm_s16le" | `tests/video/smk.luau` |
| video | y4m | .y4m | yes | yes | streaming: true, video: "rawvideo", depths: 8, 10, 12, 16 | `tests/video/y4m.luau` |
| model | 3mf | .3mf | yes | yes | materials: true, vertexColors: true, textures: true, units: true | `tests/model/3mf.luau` |
| model | dae | .dae .zae | yes | yes | materials: true, polygons: true, skins: true, morphTargets: true, animation: true, cameras: true, lights: true, zae: true | `tests/model/dae.luau` |
@@ -144,6 +147,10 @@ Codecs (layer 3, shared by formats; not loadable on their own), by domain:
- codecs/text/Url: RFC 3986 reference resolution (section 5.2), the classification of a playlist / manifest location (URL,
- codecs/text/Xml: XML 1.0 parse (DOM and pull events) and serialize, the engine data/xml, SVG, DAE, 3MF, TMX, plist, ...
- **video**
- codecs/video/Dsp: the 8x8 IDCT (bit-exact with ffmpeg -idct int) and half-pel motion compensation of MPEG codecs.
- codecs/video/H264: an H.264 / AVC (ITU-T H.264 | ISO/IEC 14496-10) video decoder in pure Luau. A codec (layer 1): the
- codecs/video/Mpeg1: an MPEG-1 video decoder (ISO/IEC 11172-2) for the MPEG-TS / MPEG-PS video formats. Absorbed from
- codecs/video/Legacy: FLI / FLC, Cinepak, Microsoft Video 1, QuickTime RLE / SMC and Smacker video decoders.
- codecs/video/Mpeg1: the MPEG-1 video decoder (ISO/IEC 11172-2), which is codecs/video/Mpeg2 (an MPEG-2 decoder
- codecs/video/Mpeg2: an MPEG-1 / MPEG-2 video decoder (ISO/IEC 11172-2, ISO/IEC 13818-2 Main profile).
- codecs/video/Theora: a Theora video decoder (the Theora I specification, Xiph.Org), for Ogg (video/ogv) and Matroska
- codecs/video/Video: the video codec registry and the generic VideoDecoder. New in formats-luau.
+28 -1
View File
@@ -275,7 +275,7 @@ single-stream archive such as `.tar.gz` loads as the archive inside. A music fil
| Host | Files | Bridges |
|---|---|---|
| Roblox | none: pass buffers or strings | `F.host.toEditableImage(img)` |
| Roblox | none: pass buffers or strings | `F.host.toEditableImage(img)`, `F.host.meshPages(prim)` / `F.host.toEditableMesh(prim, page)` |
| LÖVE | `love.filesystem` (the game folder and the save directory), then `io` | `F.host.toImageData(img)`, `F.host.fromImageData(imageData)`, `F.host.toSoundData(audioOrDecoder)` |
| LuaJIT | `io.open` | |
| luau CLI | none | |
@@ -304,6 +304,33 @@ label.ImageContent = Content.fromObject(editable)
label.Parent = script.Parent -- a ScreenGui
```
Meshes: one EditableMesh holds about 20k triangles and 60k vertices, and a client has only 8 growable ones at a time.
`F.host.meshPages(prim, opts?)` splits a triangle primitive (`F.ops.mesh.merge` output) into runs that fit;
`F.host.toEditableMesh(prim, page, opts?)` builds one page (positions relative to the returned centre, normals, uvs,
sRGB vertex colours from the linear `COLOR_0`) and returns `editableMesh, centre`. Freeze each page into what you keep
before building the next. Options: `scale` (number or `{ x, y, z }`), `doubleSided` (back faces, counted by
`meshPages` too), `texCoord`, `flipV`, `normals` / `uvs` / `colors = false`, `yield` (called every `yieldEvery` = 2000
triangles), `editableMesh` (append to an existing mesh). `F.host.pageData(prim, page, opts?)` is the same page as flat
number arrays, without engine calls. Roblox is y up like glTF: put z-up scenes (STL, VOX, CAD) through
`F.ops.mesh.setUp(scene, "y")` before merging.
<!-- doc:display needs Roblox -->
```luau
local AssetService = game:GetService("AssetService")
local scene = assert(F.model.load(glbBytes))
for _, prim in assert(F.ops.mesh.merge(scene, { byMaterial = true })) do
for _, page in assert(F.host.meshPages(prim)) do
local em, centre = assert(F.host.toEditableMesh(prim, page, { scale = 1 / 0.28 })) -- metres -> studs
local fixed = AssetService:CreateEditableMeshAsync(Content.fromObject(em), { FixedSize = true })
em:Destroy() -- frees the growable slot
local part = AssetService:CreateMeshPartAsync(Content.fromObject(fixed))
part.Position = Vector3.new(centre[1], centre[2], centre[3])
part.Anchored = true
part.Parent = workspace
end
end
```
**LÖVE.** `toImageData` copies an `rgba8` image into an `ImageData` (through the FFI), `fromImageData` copies back,
and `toSoundData` turns an `Audio` or a `Decoder` (read to its end) into 16-bit `SoundData`:
+126 -5
View File
@@ -2,7 +2,7 @@
A video file opens as a **VideoDecoder**: frames come out one at a time, as Images, with their presentation time.
Underneath sits a **Demuxer**: the container's tracks and their compressed packets. Video tracks decode through a
codec registry (H.264, VP8, MPEG-1, Motion-JPEG, raw), and the first audio track opens as an audio
codec registry (H.264, VP8, MPEG-1, MPEG-2, Motion-JPEG, raw), and the first audio track opens as an audio
[Decoder](audio.md#decoder). Animated GIF, APNG and WebP open as videos too. [docs/video.md](../video.md) covers the
internals and how to add a codec.
@@ -183,6 +183,7 @@ Images and animated images save as videos too.
| Destination | Video | Audio |
|---|---|---|
| webm | VP8 / VP9 / AV1 copied (VP8 is decode only, so the source must be WebM-compatible) | Opus / Vorbis copied |
| ogv | Theora copied (the source must be Theora: ogv or mkv) | Vorbis / Opus copied |
| mkv | any codec copied, else MJPEG | copied, else PCM s16 |
| mp4, mov | H.264 / HEVC / AV1 / MJPEG / MPEG-4 copied, else MJPEG | AAC / ALAC / Opus / FLAC / MP3 copied; PCM in MOV only |
| avi | MJPEG copied, else MJPEG encoded (`quality`) | PCM s16 |
@@ -263,6 +264,54 @@ assert(C.register({
assert(C.fromFourcc("SLID") == "solid" and C.canDecode("solid"))
```
## MPEG-2 (DVD, broadcast TS)
MPEG-1 and MPEG-2 video decode through one decoder (`codecs/video/Mpeg2`; `codecs/video/Mpeg1` is the same module):
Main profile, progressive and interlaced, frame and field pictures, 4:2:0 and 4:2:2. It reads `.mpg` / `.vob`
program streams, transport streams (stream type 0x02, or a private type such as 0x80 carrying a video PES), raw
`.m2v` elementary streams, and MPEG-2 tracks of MP4 / MOV, Matroska and AVI. The output equals `ffmpeg -idct int`
sample for sample (the integer IDCT of the IJG; ffmpeg's default IDCT differs from it by at most a level or two).
```luau
local v = assert(F.video.open(tsBytes)) -- MPEG-TS: interlaced MPEG-2 + MP2
assert(v.format == "mpegts" and v.codec == "mpeg2")
local f = assert(v:nextFrame())
local src = f.image.meta.source
assert(src.format == "mpeg2" and src.interlaced and src.topFieldFirst)
assert(f.image.format == "yuv420p") -- both fields woven: the top field on the even lines
local a = assert(v.audio, tostring(v.audioError)) -- the MP2 track through the audio hook
assert(a.sampleRate == 44100)
```
Interlaced frames come out woven, as they were coded: deinterlace them yourself if needed (`src.interlaced`,
`src.topFieldFirst`). 4:2:2 streams (studio / XDCAM) are converted to 4:2:0 for the frame Image, averaging chroma
lines of the same field. A 720x576 interlaced stream decodes at about 130 frames per second in Luau on one desktop
core (`tools/bench.luau`): real time with room to spare on a PC, less so on phones.
## Ogg Theora
Ogg video (`.ogv`, or `.ogg` with a Theora stream) opens like any other container: the `ogv` format demuxes every
Ogg stream, Theora video decodes bit-exactly (as libtheora / ffmpeg) in all three pixel formats (4:2:2 and 4:4:4
chroma is averaged down to yuv420p), and a Vorbis, Opus or FLAC track becomes `v.audio`. Plain Ogg Vorbis / Opus /
FLAC files stay audio. Theora in Matroska (`V_THEORA`) decodes too, and an Ogg Theora video remuxes to ogv or mkv
(there is no Theora encoder).
```luau
local v = assert(F.video.open(ogvBytes)) -- Theora + Vorbis
assert(v.format == "ogv" and v.codec == "theora" and v.width == 64 and v.height == 48)
assert(v.frameRate == 10 and v.audio ~= nil)
local n = 0
while v:nextFrame() do n += 1 end -- dropped frames repeat the previous picture
assert(n == 10)
local Theora = F.format("ogv") -- the format module: sniff / demux / open / encode
local d = assert(Theora.demux(F.Bytes.reader(ogvBytes)))
local vt = d:track("video")
assert(vt.codec == "theora" and vt.timescale == 10) -- packet times are frame numbers here
v:reset()
local mkv = assert(F.save(v, nil, { format = "mkv" })) -- Theora + Vorbis copied into Matroska
assert(F.video.open(mkv).codec == "theora")
```
## Playing in a game loop
Decode only the frames that are due, and stop when this frame's time budget is used up; a slow frame then makes
@@ -306,16 +355,88 @@ editable:WritePixelsBuffer(Vector2.zero, editable.Size, v:toRGBA(frame).pixels)
| Format | Read | Write | Video codecs (decode) |
|---|---|---|---|
| mp4 / mov / m4v / 3gp | yes | yes | H.264, MJPEG, MPEG-1 |
| mkv / webm | yes | yes | VP8, MJPEG, MPEG-1, raw (+ H.264) |
| avi | yes | yes | MJPEG, MPEG-1, raw (+ H.264) |
| mpegts, mpegps | yes | - | MPEG-1, H.264 |
| mp4 / mov / m4v / 3gp | yes | yes | H.264, MJPEG, MPEG-1, MPEG-2 |
| mkv / webm | yes | yes | VP8, MJPEG, MPEG-1, MPEG-2, raw, Theora (+ H.264) |
| ogv (Ogg Theora) | yes | yes (Theora + Vorbis / Opus copied) | Theora |
| avi | yes | yes | MJPEG, MPEG-1, MPEG-2, raw (+ H.264) |
| mpegts, mpegps | yes | - | MPEG-1, MPEG-2, H.264 |
| y4m | yes | yes | raw |
| h264 (Annex B) | yes | yes (copy) | H.264 |
| gif, png (APNG), webp | as video | as video via the image writers | frames |
See [FORMATS.md](../FORMATS.md). Subtitle tracks of Matroska files are read as cues by `F.format("mkv").subtitles`.
<!-- ws-video2-legacy: formats table addition -->
| 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).
<!-- ws-video2-legacy: section -->
## 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`
+3
View File
@@ -28,6 +28,9 @@ 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/Mpeg2` (`Mpeg1` is the same module), `H264` | MPEG-1 / MPEG-2 and H.264 video decoders |
| `codecs/video/Dsp` | 8x8 IDCT (bit-exact with `ffmpeg -idct int`) and half-pel motion compensation for the MPEG-style codecs |
| `codecs/video/Legacy` | FLIC, Smacker, Cinepak, Microsoft Video 1, QuickTime RLE / SMC: rgba8, palettes from `track.palette` |
| `video/<fmt>` | 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