merge ws/videoctl
This commit is contained in:
commit
d1e2e25bdf
23 files changed
+3888
-1607
No files matched your search
+47
-7
@@ -42,6 +42,7 @@ Times: `VideoFrame.pts` / `duration`, `seek` and every `duration` field are **se
|
||||
| `v:toRGBA(frameOrImage)` | an rgba8 Image (the frame's own image when it already is rgba8) |
|
||||
| `v:seek(seconds)` | the next frame is the first at or after that time (decoded from the keyframe before it) |
|
||||
| `v:reset()` | back to the start |
|
||||
| `v:release(frame)` | hands a frame's Image back: a later frame may be decoded into it (see [Skipping and reusing frames](#skipping-and-reusing-frames)) |
|
||||
|
||||
### VideoFrame
|
||||
|
||||
@@ -98,7 +99,8 @@ assert(r and g and b)
|
||||
| `codecDelay`, `playDuration` | audio: seconds of encoder priming to drop, and seconds to play after it (an MP4 edit list; Matroska `CodecDelay`) |
|
||||
| `duration`, `language`, `name`, `default` | when the container has them |
|
||||
|
||||
A Packet is `{ track, pts, dts?, duration?, keyframe, data }`: `data` is a buffer of the compressed bytes.
|
||||
A Packet is `{ track, pts, dts?, duration?, keyframe, data, disposable? }`: `data` is a buffer of the compressed
|
||||
bytes; `disposable = true` marks an H.264 or VP8 frame that no later frame predicts from.
|
||||
|
||||
```luau
|
||||
local v = assert(F.video.open(mp4Bytes))
|
||||
@@ -247,7 +249,9 @@ assert(C.entry("h264").name == "H.264 / AVC")
|
||||
```
|
||||
|
||||
`C.register(entry)` adds or replaces a codec at run time. `new(track, opts)` returns a decoder instance with
|
||||
`decode(self, packet) -> err?`, `receive(self) -> image?, pts?, keyframe?`, `flush(self)` and `reset(self)`.
|
||||
`decode(self, packet) -> err?`, `receive(self) -> image?, pts?, keyframe?`, `flush(self)` and `reset(self)`, and
|
||||
optionally `release(self, image) -> boolean` (an Image from `receive` to reuse). With `opts.skipDisposable`, `receive`
|
||||
returns `false` (and the pts) for a picture it skipped.
|
||||
`C.simpleDecoder(decodeOne)` builds one for codecs with one picture per packet and no reordering:
|
||||
|
||||
```luau
|
||||
@@ -386,11 +390,14 @@ editable:WritePixelsBuffer(Vector2.zero, editable.Size, v:toRGBA(frame).pixels)
|
||||
### Slow codecs: a yield hook and smaller frames
|
||||
|
||||
One H.264 picture can take longer than a game frame. `F.video.open(src, { yield = fn })` hands the codec a cooperative
|
||||
yield point: H.264 calls `fn` (through `pcall`) every 16 macroblocks and every macroblock row of the loop filter, so
|
||||
decoding inside a coroutine whose `fn` calls `coroutine.yield()` once a time budget is spent spreads a picture over
|
||||
several frames (a game's worker Actor, or the main thread). While a decode is suspended, run no other H.264 decode in
|
||||
the same Luau VM. `maxSide = n` shrinks the yuv420p frames (bilinear, aspect kept) so the longer side is at most `n`
|
||||
pixels, before any rgba conversion; `v.width` / `v.height` stay the source size.
|
||||
yield point, called through `pcall`: H.264 every 16 macroblocks, every macroblock row of the loop filter and between
|
||||
the allocations of a new picture; VP8 three times per macroblock row and once per loop filter row; MPEG-1 / MPEG-2
|
||||
per slice; the `maxSide` scaling and the `rgba` conversion every 16 rows. Decoding inside a coroutine whose `fn`
|
||||
calls `coroutine.yield()` once a time budget is spent spreads a picture over several frames (a game's worker Actor,
|
||||
or the main thread); at 1080p no stretch between calls is longer than about 3 ms (`luau -O2`). While a decode is
|
||||
suspended, run no other H.264 decode in the same Luau VM (VP8 decodes may interleave). `maxSide = n` shrinks the
|
||||
yuv420p frames (bilinear, aspect kept) so the longer side is at most `n` pixels, before any rgba conversion;
|
||||
`v.width` / `v.height` stay the source size.
|
||||
|
||||
```luau
|
||||
local deadline = 0
|
||||
@@ -412,6 +419,39 @@ end
|
||||
assert(frames > 0)
|
||||
```
|
||||
|
||||
### Skipping and reusing frames
|
||||
|
||||
A player that falls behind can skip the frames nothing else depends on: H.264 pictures with `nal_ref_idc` 0 (most B
|
||||
pictures) and VP8 frames that refresh no reference (temporal layers). `skipDisposable = true` decodes only their
|
||||
headers; the skipped frames keep their place, so the next frame's `pts` and `index` are what a full decode gives, and
|
||||
every frame that comes out is bit-exact. `packet.disposable` (set by the MP4, Matroska / WebM, MPEG-TS, AVI, FLV and
|
||||
H.264 demuxers) and `F.video.codecs.isDisposable(track, packet)` tell them apart without decoding; prefer
|
||||
`skipDisposable` to leaving the packets out, as a VP8 frame header can still carry probability updates.
|
||||
|
||||
Every frame is a new Image, and a 720p decode makes megabytes of garbage per frame. `reuse = true` lets `nextFrame`
|
||||
write into the Image of the frame it returned before (be done with the previous frame by then: draw it, or copy it);
|
||||
`v:release(frame)` hands one back explicitly. Reference pictures are never handed out: Images are copies of them.
|
||||
|
||||
```luau
|
||||
local v = assert(F.video.open(mp4Bytes, { audio = false, rgba = true, reuse = true, skipDisposable = true }))
|
||||
local d = v.demuxer
|
||||
local disposable = 0
|
||||
for p in d:packets(v.track.id) do
|
||||
if p.disposable then disposable += 1 end
|
||||
assert(F.video.codecs.isDisposable(v.track, p) == (p.disposable == true))
|
||||
end
|
||||
v:reset() -- back to the first packet
|
||||
local frames, last = 0, nil
|
||||
while true do
|
||||
local f = v:nextFrame() -- reuses the previous frame's rgba8 Image
|
||||
if not f then break end
|
||||
frames += 1
|
||||
assert(f.image.format == "rgba8" and (last == nil or f.index > last))
|
||||
last = f.index
|
||||
end
|
||||
assert(frames + disposable == v.frames)
|
||||
```
|
||||
|
||||
## Formats
|
||||
|
||||
| Format | Read | Write | Video codecs (decode) |
|
||||
|
||||
Reference in new issue
Block a user