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

+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`