ws-video: bench block (whole pipeline fps for MJPEG AVI / MPEG-1 TS / VP8 WebM / Y4M at 320x240 and 640x480, y4m -> MJPEG AVI), docs/video.md (API, layers, how a codec plugs into the registry), NOTICE row for mpegts-luau
This commit is contained in:
1 parent
26f4e0cb60
commit
0c9094d28a
4 files changed
+139
No files matched your search
@@ -0,0 +1,62 @@
|
||||
# Video
|
||||
|
||||
```luau
|
||||
local v = F.video.open(src) -- MKV / WebM, AVI, MP4 / MOV, MPEG-TS / PS, Y4M, and animated GIF / APNG / WebP
|
||||
while true do
|
||||
local f, err = v:nextFrame() -- f.image: yuv420p (codec output) or rgba8; f.pts / f.duration in seconds
|
||||
if not f then break end
|
||||
local rgba = v:toRGBA(f)
|
||||
end
|
||||
v:seek(12.5) -- the next frame is the first at or after 12.5 s
|
||||
local pcm = v.audio and v.audio:read(4096) -- the first audio track, as an audio Decoder (nil + v.audioError otherwise)
|
||||
for p in v.demuxer:packets(v.track.id) do end -- raw packets (pts / dts / duration in track ticks)
|
||||
F.save(v, "out.webm") -- remux (VP8 / Opus / Vorbis copied), or "out.avi" / ".mkv" / ".mp4" (MJPEG)
|
||||
F.convert(gif, "avi") -- animated images become videos (uneven delays: repeated frames)
|
||||
```
|
||||
|
||||
`F.load` of a video returns the same VideoDecoder (`kind = "video"`, decoded lazily).
|
||||
|
||||
## Layers
|
||||
|
||||
| Module | Role |
|
||||
|---|---|
|
||||
| `Types` (Video section) | `Track`, `Packet`, `Demuxer`, `VideoFrame`, `VideoDecoder`, `VideoCodecDecoder` |
|
||||
| `containers/Demux` | the indexed Demuxer every container builds: per-track cursors, keyframe seek, `count` / `get` / `info` |
|
||||
| `containers/Ebml`, `Avi`, `IsoBmff`, `MpegTs`, `MpegPs`, `MpegEs` | container structures |
|
||||
| `containers/AudioWrap` | an audio track as a stream an audio format reads (PCM -> WAV, Opus / Vorbis -> Ogg, AAC -> ADTS, ...) |
|
||||
| `codecs/Video` | the codec registry and the generic VideoDecoder (`Video.open(demuxer, opts)`, `Video.fromFrames`) |
|
||||
| `codecs/Video/Source` | what writers write: copied packets or frames encoded by a registry encoder, PCM from the audio |
|
||||
| `codecs/Yuv` | yuv420p <-> rgba8 (BT.601 / 709, limited / full range from `image.meta.yuv`), range remap |
|
||||
| `codecs/Vp8`, `Jpeg`, `Mpeg1` | the decoders (and the JPEG encoder) |
|
||||
| `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
|
||||
another format): `F.video.open` / `F.load` set it; calling a video format's `open` directly gives `v.audio = nil` and
|
||||
`v.audioError` unless you pass your own.
|
||||
|
||||
## Adding a codec (H.264, ...)
|
||||
|
||||
The registry is static (Roblox has no dynamic require): add an entry to `BUILTIN` in `src/Formats/codecs/Video/init.luau`
|
||||
with a `new` constructor that requires the codec lazily, or call `Video.register(entry)` at run time
|
||||
(`F.video.codecs.register`).
|
||||
|
||||
```luau
|
||||
{ id = "h264", name = "H.264 / AVC", fourcc = { "avc1", "avc3", "H264", ... }, matroska = { "V_MPEG4/ISO/AVC" },
|
||||
new = function(track, opts) return require("@formats/codecs/H264").newVideoDecoder(track, opts) end }
|
||||
```
|
||||
|
||||
`new(track, opts)` returns a `Types.VideoCodecDecoder`:
|
||||
|
||||
- `decode(self, packet) -> Err?`: one packet in decode order. `track.config` holds the codec configuration (avcC from
|
||||
MP4 / Matroska; nil for Annex B streams from MPEG-TS / AVI, whose packets carry the parameter sets).
|
||||
- `receive(self) -> image?, pts?, keyframe?`: the next picture in display order and the pts (track ticks) of the packet
|
||||
it came from. Return a fresh `yuv420p` Image (set `image.meta.yuv = { range = "limited", matrix = "bt601" }`).
|
||||
- `flush(self)`: end of stream (release pictures held for reordering). `reset(self)`: after a seek.
|
||||
|
||||
`Video.simpleDecoder(decodeOne, reset)` wraps a codec with one picture per packet and no reordering. Encoders are an
|
||||
entry's `encode(image, opts) -> buffer` (MJPEG), used by the writers when they cannot copy the source's packets.
|
||||
|
||||
## Speed (one Zen 5 core, `taskset -c 2`)
|
||||
|
||||
Whole pipeline (`F.video.open` + `nextFrame`, yuv / rgba output): see `tools/bench.luau` (ws-video block) and the
|
||||
codec lines (ws-video-codecs, VP8).
|
||||
@@ -22,6 +22,7 @@
|
||||
| `src/Formats/codecs/Jpeg/` (Decode, Scan, Color, Yuv; moved from image/jpeg), `src/Formats/codecs/Dct/` (inverse DCTs) | jpeg-luau 1.0 (same author); the IDCTs, upsampling, colour conversion and IDCT-size choice are Luau translations of libjpeg / libjpeg-turbo (jidctint.c, jidctred.c, jdsample.c, jdcolor.c, jdmaster.c) | IJG licence + libjpeg-turbo BSD: [libjpeg-turbo-LICENSE.md](libjpeg-turbo-LICENSE.md), [libjpeg-README.ijg](libjpeg-README.ijg). This software is based in part on the work of the Independent JPEG Group. |
|
||||
| `src/Formats/codecs/Jpeg/Arith.luau`, `src/Formats/codecs/Jpeg/Encode.luau`, `src/Formats/codecs/Dct/` (forward DCT) | new; Luau translations of libjpeg-turbo 3.2 jdarith.c / jaricom.c (Guido Vollbeding), jccolor.c, jcsample.c, jccoefct.c, jfdctint.c, jcdctmgr.c, jchuff.c, jcphuff.c, jcparam.c | as above |
|
||||
| `src/Formats/codecs/Mpeg1/` (init, Decoder, Vlc, Tables) | mpeg1-luau e2e2898 (same author), a port of jsmpeg's MPEG-1 video decoder (https://github.com/phoboslab/jsmpeg, src/mpeg1.js; the tables are generated from it) by Dominic Szablewski | MIT: [jsmpeg-MIT.txt](jsmpeg-MIT.txt) |
|
||||
| `src/Formats/containers/MpegTs/` | mpegts-luau 6e16197 (same author), written from ISO/IEC 13818-1 | no licence chosen yet; no third-party code |
|
||||
| `src/Formats/codecs/Aac/Tables.luau` | the Huffman codebooks, scalefactor band offsets and TNS limits of ISO/IEC 14496-3 (Annex 4.A), transcribed mechanically from FFmpeg's `libavcodec/aactab.c` (tables of the standard only; no FFmpeg code) | FFmpeg is LGPL-2.1-or-later; the tables are the standard's data |
|
||||
| `src/Formats/codecs/Aac/`, `codecs/Alac/`, `codecs/Mdct/`, `containers/IsoBmff/`, `audio/m4a`, `audio/aac`, `audio/caf` | written for this library from ISO/IEC 14496-3 / 14496-12 / 23008-12, Apple's ALAC and CAF specifications | no third-party code |
|
||||
| `src/Formats/audio/opus/CeltEncoder.luau`, `src/Formats/audio/opus/RangeEncoder.luau` | the encoder side of libopus 1.6 (celt_encoder.c, entenc.c, quant_bands.c, laplace.c, rate.c, bands.c, vq.c, cwrs.c), ported to mirror the absorbed decoder; analysis simplified (ws-m4a) | libopus BSD 3-clause: [libopus-BSD.txt](libopus-BSD.txt) |
|
||||
|
||||
@@ -825,3 +825,52 @@ do
|
||||
end
|
||||
end
|
||||
end
|
||||
-- ------------------------------------------------------------------------------------------- ws-video: containers
|
||||
do
|
||||
local F = require("@formats")
|
||||
local VB = require("@tests/gen/videobench")
|
||||
local function clock(n: number, f: () -> number): (number, number)
|
||||
local frames = f()
|
||||
local dt = math.huge
|
||||
for _ = 1, n do
|
||||
local t0 = os.clock()
|
||||
frames = f()
|
||||
dt = math.min(dt, os.clock() - t0)
|
||||
end
|
||||
return dt, frames
|
||||
end
|
||||
for _, c in VB do
|
||||
local data = buffer.fromstring(c.data)
|
||||
local dtOpen = clock(3, function(): number
|
||||
F.video.open(data, { audio = false })
|
||||
return 1
|
||||
end)
|
||||
for _, rgba in { false, true } do
|
||||
local ok, dt, frames = pcall(clock, 2, function(): number
|
||||
local v = assert(F.video.open(data, { audio = false, rgba = rgba }))
|
||||
local n = 0
|
||||
while true do
|
||||
local f, err = v:nextFrame()
|
||||
if not f then
|
||||
assert(err == nil, tostring(err))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
end
|
||||
return n
|
||||
end)
|
||||
if ok then
|
||||
print(string.format("%-8s %-30s %8.2f ms %7.1f fps (open %.1f ms)", runtime, string.format("video %s %s %s", c.size, c.name, if rgba then "rgba" else "yuv"), dt * 1000 / frames, frames / dt, dtOpen * 1000))
|
||||
else
|
||||
print(string.format("%-8s %-30s %s", runtime, "video " .. c.size .. " " .. c.name, tostring(dt)))
|
||||
end
|
||||
end
|
||||
if c.name == "raw.y4m" then
|
||||
local dt, frames = clock(2, function(): number
|
||||
assert(F.convert(data, "avi", { quality = 85 }))
|
||||
return 8
|
||||
end)
|
||||
print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, "video " .. c.size .. " y4m -> mjpeg avi", dt * 1000 / frames, frames / dt))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
# Video bench inputs (tools/fixtures.py, for tools/bench.luau): 1 s of testsrc2 at 320x240 and 640x480, 25 fps, as
|
||||
# MJPEG AVI, MPEG-1 TS, VP8 WebM (libvpx) and 8 frames of 320x240 Y4M.
|
||||
# tests/gen/videobench.luau { { name, size, data } }
|
||||
import importlib.util, os, tempfile
|
||||
|
||||
_spec = importlib.util.spec_from_file_location("_video_helpers", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_video.py"))
|
||||
V = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(V)
|
||||
|
||||
INPUTS = ["tools/fixtures/_video.py"]
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
tmp = tempfile.mkdtemp(prefix="videobench_")
|
||||
out = []
|
||||
for size in ("320x240", "640x480"):
|
||||
src = ["-f", "lavfi", "-i", "testsrc2=size=%s:rate=25" % size, "-t", "1"]
|
||||
for name, args in (("mjpeg.avi", ["-c:v", "mjpeg", "-q:v", "4"]),
|
||||
("mpeg1.ts", ["-c:v", "mpeg1video", "-b:v", "2M", "-bf", "2", "-f", "mpegts"]),
|
||||
("vp8.webm", ["-c:v", "libvpx", "-b:v", "1M", "-g", "50"]),
|
||||
("raw.y4m", ["-pix_fmt", "yuv420p", "-frames:v", "8"])):
|
||||
if name == "raw.y4m" and size == "640x480":
|
||||
continue
|
||||
path = os.path.join(tmp, size + "_" + name)
|
||||
V.ffmpeg(src + args + [path])
|
||||
out.append({"name": name, "size": size, "data": open(path, "rb").read()})
|
||||
ctx.emit("videobench", out)
|
||||
Reference in new issue
Block a user