merge ws/videoctl
This commit is contained in:
commit
d1e2e25bdf
23 files changed
+3888
-1607
No files matched your search
+566
-87
File diff suppressed because it is too large.
Load diff
+616
-614
File diff suppressed because it is too large.
Load diff
Binary file not shown.
@@ -1,15 +1,15 @@
|
||||
{
|
||||
"Core_1": [[8, "src/Rosetta/init.luau"], [925, "src/Rosetta/Registry.luau"], [3542, "src/Rosetta/Types.luau"], [4655, "src/Rosetta/codecs/audio/Adpcm/init.luau"], [5409, "src/Rosetta/codecs/image/Yuv.luau"], [5714, "src/Rosetta/codecs/model/Rbx/init.luau"], [5951, "src/Rosetta/codecs/video/Video/init.luau"], [6509, "src/Rosetta/containers/AudioWrap.luau"], [7019, "src/Rosetta/containers/Ogg/Mux.luau"], [7208, "src/Rosetta/core/Bytes.luau"], [7735, "src/Rosetta/core/Crc32.luau"], [7886, "src/Rosetta/core/Err.luau"], [7968, "src/Rosetta/core/Input.luau"]],
|
||||
"Core_2": [[8, "src/Rosetta/host/init.luau"], [53, "src/Rosetta/ops/ImageOps/init.luau"], [701, "src/Rosetta/ops/ImageOps/Kernels.luau"]],
|
||||
"Mpeg2": [[8, "src/Rosetta/codecs/video/Dsp.luau"], [846, "src/Rosetta/codecs/video/Mpeg1/init.luau"], [859, "src/Rosetta/codecs/video/Mpeg2/init.luau"], [1227, "src/Rosetta/codecs/video/Mpeg2/Decoder.luau"], [2754, "src/Rosetta/codecs/video/Mpeg2/Tables.luau"], [2894, "src/Rosetta/codecs/video/Mpeg2/Vlc.luau"]],
|
||||
"Core_1": [[8, "src/Rosetta/init.luau"], [925, "src/Rosetta/Registry.luau"], [3542, "src/Rosetta/Types.luau"], [4659, "src/Rosetta/codecs/audio/Adpcm/init.luau"], [5413, "src/Rosetta/codecs/image/Yuv.luau"], [5734, "src/Rosetta/codecs/model/Rbx/init.luau"], [5971, "src/Rosetta/codecs/video/Video/init.luau"], [6586, "src/Rosetta/codecs/video/Video/Disposable.luau"], [6797, "src/Rosetta/codecs/video/Video/Pool.luau"], [6849, "src/Rosetta/containers/AudioWrap.luau"], [7359, "src/Rosetta/containers/Ogg/Mux.luau"]],
|
||||
"Core_2": [[8, "src/Rosetta/core/Bytes.luau"], [535, "src/Rosetta/core/Crc32.luau"], [686, "src/Rosetta/core/Err.luau"], [768, "src/Rosetta/core/Input.luau"], [844, "src/Rosetta/host/init.luau"], [889, "src/Rosetta/ops/ImageOps/init.luau"], [1537, "src/Rosetta/ops/ImageOps/Kernels.luau"]],
|
||||
"Mpeg2": [[8, "src/Rosetta/codecs/video/Dsp.luau"], [846, "src/Rosetta/codecs/video/Mpeg1/init.luau"], [859, "src/Rosetta/codecs/video/Mpeg2/init.luau"], [1233, "src/Rosetta/codecs/video/Mpeg2/Decoder.luau"], [2760, "src/Rosetta/codecs/video/Mpeg2/Tables.luau"], [2900, "src/Rosetta/codecs/video/Mpeg2/Vlc.luau"]],
|
||||
"H263": [[8, "src/Rosetta/codecs/video/H263/init.luau"], [165, "src/Rosetta/codecs/video/H263/Decoder.luau"], [1023, "src/Rosetta/codecs/video/H263/Tables.luau"]],
|
||||
"H264": [[8, "src/Rosetta/codecs/video/H264/init.luau"], [382, "src/Rosetta/codecs/video/H264/Bitstream.luau"], [657, "src/Rosetta/codecs/video/H264/Cabac.luau"], [1788, "src/Rosetta/codecs/video/H264/Cavlc.luau"], [2556, "src/Rosetta/codecs/video/H264/Deblock.luau"], [2818, "src/Rosetta/codecs/video/H264/Decoder.luau"], [3828, "src/Rosetta/codecs/video/H264/Inter.luau"], [4154, "src/Rosetta/codecs/video/H264/Intra.luau"], [4561, "src/Rosetta/codecs/video/H264/Mb.luau"], [5500, "src/Rosetta/codecs/video/H264/Params.luau"], [5948, "src/Rosetta/codecs/video/H264/Slice.luau"], [6200, "src/Rosetta/codecs/video/H264/Tables.luau"], [6242, "src/Rosetta/codecs/video/H264/Transform.luau"]],
|
||||
"H264": [[8, "src/Rosetta/codecs/video/H264/init.luau"], [398, "src/Rosetta/codecs/video/H264/Bitstream.luau"], [673, "src/Rosetta/codecs/video/H264/Cabac.luau"], [1804, "src/Rosetta/codecs/video/H264/Cavlc.luau"], [2572, "src/Rosetta/codecs/video/H264/Deblock.luau"], [2834, "src/Rosetta/codecs/video/H264/Decoder.luau"], [3900, "src/Rosetta/codecs/video/H264/Inter.luau"], [4226, "src/Rosetta/codecs/video/H264/Intra.luau"], [4633, "src/Rosetta/codecs/video/H264/Mb.luau"], [5587, "src/Rosetta/codecs/video/H264/Params.luau"], [6035, "src/Rosetta/codecs/video/H264/Slice.luau"], [6287, "src/Rosetta/codecs/video/H264/Tables.luau"], [6329, "src/Rosetta/codecs/video/H264/Transform.luau"]],
|
||||
"Theora": [[8, "src/Rosetta/codecs/video/Theora/init.luau"], [272, "src/Rosetta/codecs/video/Theora/Decoder.luau"], [1536, "src/Rosetta/codecs/video/Theora/Header.luau"]],
|
||||
"Legacy": [[8, "src/Rosetta/codecs/video/Legacy/init.luau"], [207, "src/Rosetta/codecs/video/Legacy/Cinepak.luau"], [504, "src/Rosetta/codecs/video/Legacy/Flic.luau"], [871, "src/Rosetta/codecs/video/Legacy/Msvideo1.luau"], [989, "src/Rosetta/codecs/video/Legacy/Picture.luau"], [1097, "src/Rosetta/codecs/video/Legacy/QtRle.luau"], [1370, "src/Rosetta/codecs/video/Legacy/Smacker.luau"], [1765, "src/Rosetta/codecs/video/Legacy/Smc.luau"]],
|
||||
"MpegTs": [[8, "src/Rosetta/containers/MpegEs.luau"], [175, "src/Rosetta/containers/MpegPs/init.luau"], [396, "src/Rosetta/containers/MpegTs/init.luau"]],
|
||||
"Dct": [[8, "src/Rosetta/codecs/image/Dct/init.luau"]],
|
||||
"Jpeg": [[8, "src/Rosetta/codecs/image/Jpeg/init.luau"], [362, "src/Rosetta/codecs/image/Jpeg/Arith.luau"], [770, "src/Rosetta/codecs/image/Jpeg/Color.luau"], [1135, "src/Rosetta/codecs/image/Jpeg/Decode.luau"], [1900, "src/Rosetta/codecs/image/Jpeg/Encode.luau"], [2781, "src/Rosetta/codecs/image/Jpeg/Scan.luau"], [3250, "src/Rosetta/codecs/image/Jpeg/Tables.luau"], [3328, "src/Rosetta/codecs/image/Jpeg/Yuv.luau"]],
|
||||
"Vp8": [[8, "src/Rosetta/codecs/image/Vp8/init.luau"], [139, "src/Rosetta/codecs/image/Vp8/Decode.luau"], [2095, "src/Rosetta/codecs/image/Vp8/Predict.luau"], [2358, "src/Rosetta/codecs/image/Vp8/Tables.luau"], [2608, "src/Rosetta/codecs/image/Vp8/Yuv.luau"], [2750, "src/Rosetta/codecs/image/Vp8L/init.luau"], [2854, "src/Rosetta/codecs/image/Vp8L/Decode.luau"], [3794, "src/Rosetta/codecs/image/Vp8L/Encode.luau"]],
|
||||
"Vp8": [[8, "src/Rosetta/codecs/image/Vp8/init.luau"], [150, "src/Rosetta/codecs/image/Vp8/Decode.luau"], [2130, "src/Rosetta/codecs/image/Vp8/Predict.luau"], [2393, "src/Rosetta/codecs/image/Vp8/Tables.luau"], [2643, "src/Rosetta/codecs/image/Vp8/Yuv.luau"], [2785, "src/Rosetta/codecs/image/Vp8L/init.luau"], [2889, "src/Rosetta/codecs/image/Vp8L/Decode.luau"], [3829, "src/Rosetta/codecs/image/Vp8L/Encode.luau"]],
|
||||
"Bcn": [[8, "src/Rosetta/codecs/image/Bcn/init.luau"], [115, "src/Rosetta/codecs/image/Bcn/Decode.luau"], [599, "src/Rosetta/codecs/image/Bcn/Encode.luau"]],
|
||||
"Png": [[8, "src/Rosetta/image/png/init.luau"], [588, "src/Rosetta/image/png/Apng.luau"], [668, "src/Rosetta/image/png/Decode.luau"], [1041, "src/Rosetta/image/png/Encode.luau"]],
|
||||
"Deflate": [[8, "src/Rosetta/codecs/compress/Deflate/init.luau"], [334, "src/Rosetta/codecs/compress/Deflate/Compress.luau"], [816, "src/Rosetta/codecs/compress/Deflate/Inflate.luau"]],
|
||||
@@ -40,7 +40,7 @@
|
||||
"codecs_model": [[8, "src/Rosetta/codecs/model/Brush.luau"], [648, "src/Rosetta/codecs/model/Displacement.luau"], [797, "src/Rosetta/codecs/model/DoomMap/init.luau"], [1242, "src/Rosetta/codecs/model/DoomMap/Udmf.luau"], [1492, "src/Rosetta/codecs/model/Mat4.luau"], [1751, "src/Rosetta/codecs/model/MeshFlatten.luau"], [2279, "src/Rosetta/codecs/model/Minecraft/init.luau"], [2831, "src/Rosetta/codecs/model/Minecraft/Colors.luau"], [2910, "src/Rosetta/codecs/model/Minecraft/Legacy.luau"], [3017, "src/Rosetta/codecs/model/Minecraft/Packed.luau"], [3223, "src/Rosetta/codecs/model/Patch.luau"], [3347, "src/Rosetta/codecs/model/Polygon.luau"], [3581, "src/Rosetta/codecs/model/Rbx/Binary.luau"], [4335, "src/Rosetta/codecs/model/Rig.luau"], [4578, "src/Rosetta/codecs/model/Sampler.luau"], [4738, "src/Rosetta/codecs/model/SourceAnim.luau"], [4959, "src/Rosetta/codecs/model/Vrml/init.luau"], [5107, "src/Rosetta/codecs/model/Vrml/Build.luau"], [5681, "src/Rosetta/codecs/model/Vrml/Fields.luau"], [5844, "src/Rosetta/codecs/model/Vrml/Geometry.luau"], [6593, "src/Rosetta/codecs/model/Vrml/Parse.luau"], [7115, "src/Rosetta/codecs/model/Vrml/Write.luau"], [7842, "src/Rosetta/codecs/model/Vrml/Xml.luau"]],
|
||||
"codecs_text": [[8, "src/Rosetta/codecs/text/GodotVariant.luau"], [667, "src/Rosetta/codecs/text/Json/init.luau"], [936, "src/Rosetta/codecs/text/Json/Decode.luau"], [1371, "src/Rosetta/codecs/text/Json/Encode.luau"], [1716, "src/Rosetta/codecs/text/KeyValues/init.luau"], [2286, "src/Rosetta/codecs/text/KeyValues/Binary.luau"], [2592, "src/Rosetta/codecs/text/Lexer/init.luau"], [2724, "src/Rosetta/codecs/text/Nbt/init.luau"], [3077, "src/Rosetta/codecs/text/Nbt/Binary.luau"], [3769, "src/Rosetta/codecs/text/Nbt/Snbt.luau"], [4409, "src/Rosetta/codecs/text/Nbt/Value.luau"], [4647, "src/Rosetta/codecs/text/Text/init.luau"], [4738, "src/Rosetta/codecs/text/Url/init.luau"], [4943, "src/Rosetta/codecs/text/Xml/init.luau"], [5393, "src/Rosetta/codecs/text/Xml/Parse.luau"]],
|
||||
"codecs_video": [[8, "src/Rosetta/codecs/video/Video/Source.luau"]],
|
||||
"containers": [[8, "src/Rosetta/containers/Archive.luau"], [275, "src/Rosetta/containers/Avi/init.luau"], [828, "src/Rosetta/containers/Blend.luau"], [1567, "src/Rosetta/containers/Chunks.luau"], [1702, "src/Rosetta/containers/Cpio.luau"], [2130, "src/Rosetta/containers/Demux.luau"], [2405, "src/Rosetta/containers/Ebml/init.luau"], [2719, "src/Rosetta/containers/Fat.luau"], [3407, "src/Rosetta/containers/Iso9660.luau"], [3912, "src/Rosetta/containers/IsoBmff/init.luau"], [4083, "src/Rosetta/containers/IsoBmff/Box.luau"], [4272, "src/Rosetta/containers/IsoBmff/Demux.luau"], [5118, "src/Rosetta/containers/IsoBmff/Meta.luau"], [5640, "src/Rosetta/containers/IsoBmff/Mux.luau"], [6157, "src/Rosetta/containers/IsoBmff/QtPalette.luau"], [6275, "src/Rosetta/containers/MacFile.luau"], [6424, "src/Rosetta/containers/Ogg/init.luau"], [6849, "src/Rosetta/containers/Ogg/Page.luau"], [7015, "src/Rosetta/containers/Riff/init.luau"], [7332, "src/Rosetta/containers/Tar.luau"], [7746, "src/Rosetta/containers/Zip/init.luau"]],
|
||||
"containers": [[8, "src/Rosetta/containers/Archive.luau"], [275, "src/Rosetta/containers/Avi/init.luau"], [828, "src/Rosetta/containers/Blend.luau"], [1567, "src/Rosetta/containers/Chunks.luau"], [1702, "src/Rosetta/containers/Cpio.luau"], [2130, "src/Rosetta/containers/Demux.luau"], [2416, "src/Rosetta/containers/Ebml/init.luau"], [2730, "src/Rosetta/containers/Fat.luau"], [3418, "src/Rosetta/containers/Iso9660.luau"], [3923, "src/Rosetta/containers/IsoBmff/init.luau"], [4094, "src/Rosetta/containers/IsoBmff/Box.luau"], [4283, "src/Rosetta/containers/IsoBmff/Demux.luau"], [5129, "src/Rosetta/containers/IsoBmff/Meta.luau"], [5651, "src/Rosetta/containers/IsoBmff/Mux.luau"], [6168, "src/Rosetta/containers/IsoBmff/QtPalette.luau"], [6286, "src/Rosetta/containers/MacFile.luau"], [6435, "src/Rosetta/containers/Ogg/init.luau"], [6860, "src/Rosetta/containers/Ogg/Page.luau"], [7026, "src/Rosetta/containers/Riff/init.luau"], [7343, "src/Rosetta/containers/Tar.luau"], [7757, "src/Rosetta/containers/Zip/init.luau"]],
|
||||
"core": [[8, "src/Rosetta/core/Adler32.luau"], [68, "src/Rosetta/core/Base64.luau"], [296, "src/Rosetta/core/Bits.luau"], [589, "src/Rosetta/core/Crc16.luau"], [641, "src/Rosetta/core/Md5.luau"], [799, "src/Rosetta/core/Sniff.luau"]],
|
||||
"data": [[8, "src/Rosetta/data/cbor/init.luau"], [107, "src/Rosetta/data/cbor/Codec.luau"], [676, "src/Rosetta/data/csv/init.luau"], [988, "src/Rosetta/data/ini/init.luau"], [1234, "src/Rosetta/data/json/init.luau"], [1326, "src/Rosetta/data/msgpack/init.luau"], [1415, "src/Rosetta/data/msgpack/Codec.luau"], [1857, "src/Rosetta/data/nbt/init.luau"], [1916, "src/Rosetta/data/plist/init.luau"], [2222, "src/Rosetta/data/plist/Binary.luau"], [2489, "src/Rosetta/data/q3shader/init.luau"], [2728, "src/Rosetta/data/q3skin/init.luau"], [2824, "src/Rosetta/data/snbt/init.luau"], [2887, "src/Rosetta/data/toml/init.luau"], [2954, "src/Rosetta/data/toml/Decode.luau"], [3582, "src/Rosetta/data/toml/Encode.luau"], [3791, "src/Rosetta/data/tres/init.luau"], [3936, "src/Rosetta/data/vdf/init.luau"], [4037, "src/Rosetta/data/xml/init.luau"], [4093, "src/Rosetta/data/yaml/init.luau"], [4166, "src/Rosetta/data/yaml/Emit.luau"], [4456, "src/Rosetta/data/yaml/Parse.luau"]],
|
||||
"font": [[8, "src/Rosetta/font/bdf/init.luau"], [297, "src/Rosetta/font/bmfont/init.luau"], [724, "src/Rosetta/font/bmfont/Parse.luau"], [1177, "src/Rosetta/font/eot/init.luau"], [1409, "src/Rosetta/font/fon/init.luau"], [1692, "src/Rosetta/font/hershey/init.luau"], [1887, "src/Rosetta/font/pcf/init.luau"], [2167, "src/Rosetta/font/psf/init.luau"], [2487, "src/Rosetta/font/ttf/init.luau"], [2538, "src/Rosetta/font/type1/init.luau"], [2897, "src/Rosetta/font/type1/Charstring.luau"], [3175, "src/Rosetta/font/woff/init.luau"], [3307, "src/Rosetta/font/woff2/init.luau"], [3364, "src/Rosetta/font/woff2/Rebuild.luau"]],
|
||||
|
||||
+586
-105
File diff suppressed because it is too large.
Load diff
+565
-86
File diff suppressed because it is too large.
Load diff
+616
-614
File diff suppressed because it is too large.
Load diff
+3
-1
@@ -45,7 +45,9 @@ VideoDecoder saved to an image format (`gif`, `apng`, `webp`) becomes an animate
|
||||
one category. Extras:
|
||||
|
||||
- `F.video.open(src, opts?)`: `opts.maxSide` shrinks the frames (longer side at most n pixels), `opts.yield` is a
|
||||
cooperative yield point for slow codecs (H.264; see the video guide), `opts.audio = false` skips the audio track, `opts.sbr = false` decodes only the AAC-LC core of an HE-AAC track.
|
||||
cooperative yield point for slow codecs (H.264, VP8, MPEG-1 / MPEG-2 and the conversions; see the video guide), `opts.audio = false` skips the audio track, `opts.sbr = false` decodes only the AAC-LC core of an HE-AAC track,
|
||||
`opts.skipDisposable` skips the frames nothing depends on (`packet.disposable`), `opts.reuse` decodes each frame
|
||||
into the previous frame's Image (`v:release(frame)` hands one back).
|
||||
It also opens animated images (GIF / APNG / WebP) as videos; `F.video.fromImage(img)`,
|
||||
`F.video.fromFrames(images, delays?)` (an image sequence: `F.save(F.video.fromFrames(list, 1 / 25), "out.avi")`),
|
||||
`F.video.codecs` (the codec registry `codecs/video/Video`).
|
||||
|
||||
+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) |
|
||||
|
||||
@@ -184,6 +184,7 @@ export type Packet = {
|
||||
duration: number?, -- ticks
|
||||
keyframe: boolean,
|
||||
data: buffer, -- the packet's bytes (its own buffer)
|
||||
disposable: boolean?, -- true: no later frame predicts from this one (H.264 nal_ref_idc 0, VP8 no reference refresh)
|
||||
}
|
||||
export type Demuxer = {
|
||||
kind: "demuxer",
|
||||
@@ -227,9 +228,12 @@ export type VideoDecoder = {
|
||||
-- a codec's decoder instance (codecs/video/Video registry entries create them): packets in, pictures out
|
||||
export type VideoCodecDecoder = {
|
||||
decode: (self: any, packet: Packet) -> Err?, -- feed one packet (decode order)
|
||||
receive: (self: any) -> (Image?, number?, boolean?), -- the next picture, the pts (ticks) it carries, keyframe | nil
|
||||
-- the next picture, the pts (ticks) it carries, keyframe | nil; false for a picture skipped (opts.skipDisposable)
|
||||
receive: (self: any) -> (Image? | false, number?, boolean?),
|
||||
flush: (self: any) -> (), -- end of stream: pictures held for reordering become available to receive()
|
||||
reset: (self: any) -> (), -- after a seek: drop references / held pictures
|
||||
-- an Image from receive() handed back for reuse (the caller is done with it); false if it was not this decoder's
|
||||
release: ((self: any, image: Image) -> boolean)?,
|
||||
}
|
||||
|
||||
-- Indices are 1-based everywhere (node, mesh, material, texture, ...). Accessor data is tightly packed (no stride, no
|
||||
|
||||
@@ -1164,8 +1164,9 @@ end
|
||||
export type Frame = {
|
||||
width: number,
|
||||
height: number,
|
||||
ref: Ref,
|
||||
ref: Ref?, -- nil when skipped
|
||||
header: any,
|
||||
skipped: boolean?,
|
||||
}
|
||||
|
||||
local BORDER = Predict.BORDER
|
||||
@@ -1198,7 +1199,7 @@ local function freeRef(st: State): Ref
|
||||
end
|
||||
|
||||
function Decode.cropTo(f: Frame, dy: buffer, oy: number, du: buffer, ou: number, dv: buffer, ov: number)
|
||||
local r = f.ref
|
||||
local r = f.ref :: Ref
|
||||
local w, h = f.width, f.height
|
||||
local cw, chh = (w + 1) // 2, (h + 1) // 2
|
||||
local Y, U, V, ys, uvs, yb, uvb = r.Y, r.U, r.V, r.ys, r.uvs, r.yb, r.uvb
|
||||
@@ -1211,7 +1212,12 @@ function Decode.cropTo(f: Frame, dy: buffer, oy: number, du: buffer, ou: number,
|
||||
end
|
||||
end
|
||||
|
||||
function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, maxPixels: number): (Frame?, string?)
|
||||
-- hook: called (through pcall) three times per macroblock row (modes, residuals, reconstruction) and once per loop filter
|
||||
-- row, a cooperative yield point; the module
|
||||
-- scratch is per macroblock, so other VP8 decodes may run while one is suspended
|
||||
-- skip: a frame that refreshes no reference and no segment map comes back { skipped = true } after its header (the
|
||||
-- state the header carries is kept, the picture is not reconstructed)
|
||||
function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, maxPixels: number, hook: (() -> ())?, skip: boolean?): (Frame?, string?)
|
||||
local hdr, herr = Decode.header(buf, off, len)
|
||||
if not hdr then
|
||||
return nil, herr
|
||||
@@ -1427,6 +1433,12 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
if br.eof then
|
||||
return fail("truncated: frame header")
|
||||
end
|
||||
if skip and not key and not refreshGolden and not refreshAlt and copyToGolden == 0 and copyToAlt == 0 and not refreshLast and not st.updateMap then
|
||||
if saved then
|
||||
st.proba, st.ymodeProb, st.uvmodeProb, st.mvProb = saved[1], saved[2], saved[3], saved[4]
|
||||
end
|
||||
return { width = width, height = height, ref = nil, header = hdr, skipped = true }, nil
|
||||
end
|
||||
|
||||
-- loop filter levels [segment][reference][mode delta index] and, per level, the interior limit and hev threshold
|
||||
local lvlTab = table.create(64, 0)
|
||||
@@ -1532,6 +1544,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
local miMvR, miMvC, miSubR, miSubC = st.miMvR, st.miMvC, st.miSubR, st.miSubC
|
||||
|
||||
for mby = 0, mbh - 1 do
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local tbr = parts[band(mby, lastPart) + 1]
|
||||
-- modes of the row (first partition)
|
||||
for k = 1, 4 do
|
||||
@@ -1628,6 +1643,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
return fail("truncated: first partition (modes)")
|
||||
end
|
||||
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
-- residuals (token partition)
|
||||
local leftNz, leftNzDc = 0, 0
|
||||
for mbx = 0, mbw - 1 do
|
||||
@@ -1756,6 +1774,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
bfill(yuv, U_OFF - BPS - 1, 127, 8 + 1)
|
||||
bfill(yuv, V_OFF - BPS - 1, 127, 8 + 1)
|
||||
end
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
for mbx = 0, mbw - 1 do
|
||||
local x1 = mbx + 1
|
||||
local co = mbx * 768
|
||||
@@ -1865,6 +1886,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
-- loop filter over the whole frame, macroblocks in raster order (libwebp DoFilter)
|
||||
if filterType > 0 then
|
||||
for mby = 0, mbh - 1 do
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
for mbx = 0, mbw - 1 do
|
||||
local fi = mby * mbw + mbx + 1
|
||||
local limit = fLimit[fi]
|
||||
|
||||
@@ -7,6 +7,7 @@ local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Decode = require("@rosetta/codecs/image/Vp8/Decode")
|
||||
local Yuv = require("@rosetta/codecs/image/Vp8/Yuv")
|
||||
local Pool = require("@rosetta/codecs/video/Video/Pool")
|
||||
|
||||
local Vp8 = {}
|
||||
|
||||
@@ -14,6 +15,9 @@ local DEFAULT_MAX_PIXELS = 2 ^ 28
|
||||
|
||||
export type Options = {
|
||||
maxPixels: number?,
|
||||
yield: (() -> ())?, -- called through pcall a few times per macroblock row (decode and loop filter)
|
||||
-- decodeFrame returns nil, nil for an inter frame that refreshes no reference (its header still applies)
|
||||
skipDisposable: boolean?,
|
||||
}
|
||||
|
||||
local function fail(msg: string?, offset: number?): Err.Err
|
||||
@@ -49,10 +53,10 @@ end
|
||||
local DecoderMeta = {}
|
||||
DecoderMeta.__index = DecoderMeta
|
||||
|
||||
export type Decoder = typeof(setmetatable({} :: { state: Decode.State }, DecoderMeta))
|
||||
export type Decoder = typeof(setmetatable({} :: { state: Decode.State, images: Pool.Pool }, DecoderMeta))
|
||||
|
||||
function Vp8.newDecoder(): Decoder
|
||||
return setmetatable({ state = Decode.new() }, DecoderMeta)
|
||||
return setmetatable({ state = Decode.new(), images = Pool.new() }, DecoderMeta)
|
||||
end
|
||||
|
||||
function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len: number?, opts: Options?): (Types.Image?, Err.Err?)
|
||||
@@ -60,11 +64,13 @@ function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len:
|
||||
return nil, Err.new(Err.ARG, "decodeFrame: expected a buffer")
|
||||
end
|
||||
local o, l = range(buf, off, len)
|
||||
local f, err = Decode.decodeFrame(self.state, buf, o, l, opts and opts.maxPixels or DEFAULT_MAX_PIXELS)
|
||||
local f, err = Decode.decodeFrame(self.state, buf, o, l, opts and opts.maxPixels or DEFAULT_MAX_PIXELS, opts and opts.yield, opts and opts.skipDisposable)
|
||||
if not f then
|
||||
return nil, fail(err, o)
|
||||
elseif f.skipped then
|
||||
return nil, nil
|
||||
end
|
||||
local img = Types.Image.new(f.width, f.height, "yuv420p")
|
||||
local img = self.images:take(f.width, f.height, "yuv420p")
|
||||
local ny = f.width * f.height
|
||||
local nc = ((f.width + 1) // 2) * ((f.height + 1) // 2)
|
||||
Decode.cropTo(f, img.pixels, 0, img.pixels, ny, img.pixels, ny + nc)
|
||||
@@ -73,6 +79,11 @@ function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len:
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- hands a decoded Image back for reuse by a later frame (the caller is done with it); false if it was not this decoder's
|
||||
function DecoderMeta.release(self: Decoder, image: Types.Image): boolean
|
||||
return self.images:give(image)
|
||||
end
|
||||
|
||||
function DecoderMeta.reset(self: Decoder)
|
||||
Decode.reset(self.state)
|
||||
end
|
||||
|
||||
@@ -66,7 +66,8 @@ for i = 1, 2304 do
|
||||
CB[i] = v * 65536 + 0xff000000
|
||||
end
|
||||
|
||||
function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, cw: number, range: string?, matrix: string?, out: buffer?): buffer
|
||||
-- hook: called (through pcall) every 16 rows, a cooperative yield point
|
||||
function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, cw: number, range: string?, matrix: string?, out: buffer?, hook: (() -> ())?): buffer
|
||||
local dst = out or bcreate(w * h * 4)
|
||||
local c = coeffs(range, matrix)
|
||||
local crv, cgu, cgv, cbu = c.crv, c.cgu, c.cgv, c.cbu
|
||||
@@ -78,6 +79,9 @@ function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, c
|
||||
local w2 = w // 2
|
||||
local stride = w * 4
|
||||
for y = 0, h - 1, 2 do
|
||||
if hook and y % 16 == 0 and y > 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
local yrow = y * w
|
||||
local ci = uo + (y // 2) * cw
|
||||
local dv = vo - uo
|
||||
@@ -212,7 +216,8 @@ function Yuv.toFullRange(img: Types.Image): Types.Image
|
||||
return out
|
||||
end
|
||||
|
||||
function Yuv.imageToRgba(img: Types.Image): Types.Image?
|
||||
-- out: an rgba8 Image of the same size to write into (else a new one)
|
||||
function Yuv.imageToRgba(img: Types.Image, out: Types.Image?, hook: (() -> ())?): Types.Image?
|
||||
if img.format == "rgba8" then
|
||||
return img
|
||||
end
|
||||
@@ -222,10 +227,13 @@ function Yuv.imageToRgba(img: Types.Image): Types.Image?
|
||||
local w, h = img.width, img.height
|
||||
local uo, vo, cw = Yuv.planes(w, h)
|
||||
local range, matrix = info(img)
|
||||
local out = Types.Image.new(w, h, "rgba8")
|
||||
Yuv.toRgba(img.pixels, w, h, uo, vo, cw, range, matrix, out.pixels)
|
||||
out.meta.source = img.meta.source
|
||||
return out
|
||||
if not out or out.format ~= "rgba8" or out.width ~= w or out.height ~= h or buffer.len(out.pixels) < w * h * 4 then
|
||||
out = Types.Image.new(w, h, "rgba8")
|
||||
end
|
||||
local o = out :: Types.Image
|
||||
Yuv.toRgba(img.pixels, w, h, uo, vo, cw, range, matrix, o.pixels, hook)
|
||||
o.meta.source = img.meta.source
|
||||
return o
|
||||
end
|
||||
|
||||
function Yuv.imageFromRgba(img: Types.Image, range: string?, matrix: string?): Types.Image?
|
||||
@@ -242,9 +250,12 @@ function Yuv.imageFromRgba(img: Types.Image, range: string?, matrix: string?): T
|
||||
return out
|
||||
end
|
||||
|
||||
local function scalePlane(src: buffer, so: number, sw: number, sh: number, dst: buffer, d0: number, dw: number, dh: number)
|
||||
local function scalePlane(src: buffer, so: number, sw: number, sh: number, dst: buffer, d0: number, dw: number, dh: number, hook: (() -> ())?)
|
||||
local fx, fy = sw / dw, sh / dh
|
||||
for y = 0, dh - 1 do
|
||||
if hook and y % 16 == 15 then
|
||||
pcall(hook)
|
||||
end
|
||||
local sy = (y + 0.5) * fy - 0.5
|
||||
if sy < 0 then
|
||||
sy = 0
|
||||
@@ -275,7 +286,7 @@ local function scalePlane(src: buffer, so: number, sw: number, sh: number, dst:
|
||||
end
|
||||
end
|
||||
|
||||
function Yuv.scale(src: buffer, w: number, h: number, width: number, height: number, out: buffer?): buffer
|
||||
function Yuv.scale(src: buffer, w: number, h: number, width: number, height: number, out: buffer?, hook: (() -> ())?): buffer
|
||||
local dw: number = math.max(1, floor(width))
|
||||
local dh: number = math.max(1, floor(height))
|
||||
local cw: number = (w + 1) // 2
|
||||
@@ -287,17 +298,22 @@ function Yuv.scale(src: buffer, w: number, h: number, width: number, height: num
|
||||
if w < 1 or h < 1 or buffer.len(src) < w * h + 2 * cw * ch then
|
||||
return dst
|
||||
end
|
||||
scalePlane(src, 0, w, h, dst, 0, dw, dh)
|
||||
scalePlane(src, w * h, cw, ch, dst, dw * dh, dcw, dch)
|
||||
scalePlane(src, w * h + cw * ch, cw, ch, dst, dw * dh + dcw * dch, dcw, dch)
|
||||
scalePlane(src, 0, w, h, dst, 0, dw, dh, hook)
|
||||
scalePlane(src, w * h, cw, ch, dst, dw * dh, dcw, dch, hook)
|
||||
scalePlane(src, w * h + cw * ch, cw, ch, dst, dw * dh + dcw * dch, dcw, dch, hook)
|
||||
return dst
|
||||
end
|
||||
|
||||
function Yuv.imageScale(img: Types.Image, dw: number, dh: number): Types.Image
|
||||
local out = Types.Image.new(math.max(1, floor(dw)), math.max(1, floor(dh)), "yuv420p")
|
||||
Yuv.scale(img.pixels, img.width, img.height, out.width, out.height, out.pixels)
|
||||
out.meta = table.clone(img.meta or {})
|
||||
return out
|
||||
-- out: a yuv420p Image of the target size to write into (else a new one)
|
||||
function Yuv.imageScale(img: Types.Image, dw: number, dh: number, out: Types.Image?, hook: (() -> ())?): Types.Image
|
||||
local tw, th = math.max(1, floor(dw)), math.max(1, floor(dh))
|
||||
if not out or out.format ~= "yuv420p" or out.width ~= tw or out.height ~= th then
|
||||
out = Types.Image.new(tw, th, "yuv420p")
|
||||
end
|
||||
local o = out :: Types.Image
|
||||
Yuv.scale(img.pixels, img.width, img.height, tw, th, o.pixels, hook)
|
||||
o.meta = table.clone(img.meta or {})
|
||||
return o
|
||||
end
|
||||
|
||||
return Yuv
|
||||
@@ -12,6 +12,7 @@ local Cavlc = require("@rosetta/codecs/video/H264/Cavlc")
|
||||
local Cabac = require("@rosetta/codecs/video/H264/Cabac")
|
||||
local Deblock = require("@rosetta/codecs/video/H264/Deblock")
|
||||
local Inter = require("@rosetta/codecs/video/H264/Inter")
|
||||
local Pool = require("@rosetta/codecs/video/Video/Pool")
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
local PAD, PADC = Inter.PAD, Inter.PADC
|
||||
@@ -27,6 +28,8 @@ export type Options = {
|
||||
-- yield is pcall'ed (it can't throw through native code); the entropy decoders keep state in module locals, so no
|
||||
-- other H.264 decode may run in the VM while one is suspended
|
||||
yield: (() -> ())?,
|
||||
-- pictures with nal_ref_idc 0 are not reconstructed: they keep their place in output order as { skipped = true }
|
||||
skipDisposable: boolean?,
|
||||
}
|
||||
|
||||
function Decoder.new(opts: Options?): any
|
||||
@@ -52,6 +55,7 @@ function Decoder.new(opts: Options?): any
|
||||
prevFrameNum = 0,
|
||||
maxLongTermIdx = -1,
|
||||
pool = {},
|
||||
images = Pool.new(), -- output Images given back (H264 decoder:release)
|
||||
gray = nil,
|
||||
frames = 0,
|
||||
errors = 0,
|
||||
@@ -76,6 +80,29 @@ local function newPicture(self: any, sps: any): any
|
||||
local ys = w + 2 * PAD
|
||||
local cs = w // 2 + 2 * PADC
|
||||
local n4 = mbW * mbH * 16
|
||||
-- a 1080p picture's tables are 1.6 M slots: allocated in steps between yields
|
||||
local hook = self.opts.yield
|
||||
local y = buffer.create(ys * (h + 2 * PAD))
|
||||
local u, v = buffer.create(cs * (h // 2 + 2 * PADC)), buffer.create(cs * (h // 2 + 2 * PADC))
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local ref0, ref1 = table.create(n4, -1), table.create(n4, -1)
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local uid0, uid1 = table.create(n4, -1), table.create(n4, -1)
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local mv0 = table.create(n4 * 2, 0)
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local mv1 = table.create(n4 * 2, 0)
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
return {
|
||||
mbW = mbW,
|
||||
mbH = mbH,
|
||||
@@ -83,16 +110,16 @@ local function newPicture(self: any, sps: any): any
|
||||
cs = cs,
|
||||
yo = PAD * ys + PAD,
|
||||
co = PADC * cs + PADC,
|
||||
y = buffer.create(ys * (h + 2 * PAD)),
|
||||
u = buffer.create(cs * (h // 2 + 2 * PADC)),
|
||||
v = buffer.create(cs * (h // 2 + 2 * PADC)),
|
||||
y = y,
|
||||
u = u,
|
||||
v = v,
|
||||
mbIntra = table.create(mbW * mbH, true),
|
||||
ref0 = table.create(n4, -1),
|
||||
ref1 = table.create(n4, -1),
|
||||
uid0 = table.create(n4, -1),
|
||||
uid1 = table.create(n4, -1),
|
||||
mv0 = table.create(n4 * 2, 0),
|
||||
mv1 = table.create(n4 * 2, 0),
|
||||
ref0 = ref0,
|
||||
ref1 = ref1,
|
||||
uid0 = uid0,
|
||||
uid1 = uid1,
|
||||
mv0 = mv0,
|
||||
mv1 = mv1,
|
||||
uid = 0,
|
||||
poc = 0,
|
||||
frameNum = 0,
|
||||
@@ -100,12 +127,13 @@ local function newPicture(self: any, sps: any): any
|
||||
short = false,
|
||||
longIdx = 0,
|
||||
waiting = false,
|
||||
skipped = false,
|
||||
pts = 0,
|
||||
}
|
||||
end
|
||||
|
||||
local function release(self: any, p: any)
|
||||
if not p.short and not p.long and not p.waiting and p ~= self.cur and p ~= self.gray then
|
||||
if not p.short and not p.long and not p.waiting and p ~= self.cur and p ~= self.gray and not p.skipped then
|
||||
for _, q in self.pool do
|
||||
if q == p then
|
||||
return
|
||||
@@ -130,11 +158,11 @@ local function grayPicture(self: any, sps: any): any
|
||||
return g
|
||||
end
|
||||
|
||||
local function emit(self: any, p: any)
|
||||
p.waiting = false
|
||||
-- the cropped output copy of a decoded picture
|
||||
local function toImage(self: any, p: any): Types.Image
|
||||
local sps = p.sps
|
||||
local W, H = sps.width, sps.height
|
||||
local img = Types.Image.new(W, H, "yuv420p")
|
||||
local img = self.images:take(W, H, "yuv420p")
|
||||
local px = img.pixels
|
||||
local cx, cy = sps.cropX, sps.cropY
|
||||
for y = 0, H - 1 do
|
||||
@@ -151,7 +179,16 @@ local function emit(self: any, p: any)
|
||||
img.meta.sar = { sps.sarW, sps.sarH }
|
||||
end
|
||||
img.meta.yuv = { range = if sps.fullRange then "full" else "limited", matrix = if sps.matrix == 1 then "bt709" else "bt601" }
|
||||
table.insert(self.out, { image = img, pts = p.pts, duration = 0, poc = p.poc, keyframe = p.idr })
|
||||
return img
|
||||
end
|
||||
|
||||
local function emit(self: any, p: any)
|
||||
p.waiting = false
|
||||
if p.skipped then
|
||||
table.insert(self.out, { image = nil, skipped = true, pts = p.pts, duration = 0, poc = p.poc, keyframe = false })
|
||||
return
|
||||
end
|
||||
table.insert(self.out, { image = toImage(self, p), pts = p.pts, duration = 0, poc = p.poc, keyframe = p.idr })
|
||||
release(self, p)
|
||||
end
|
||||
|
||||
@@ -571,7 +608,7 @@ local function setupGrids(self: any, sps: any)
|
||||
if self.S and self.S.mbW == mbW and self.S.mbH == mbH then
|
||||
return
|
||||
end
|
||||
local S = Mb.grids(mbW, mbH)
|
||||
local S = Mb.grids(mbW, mbH, self.opts.yield)
|
||||
S.mbW, S.mbH = mbW, mbH
|
||||
S.w4 = mbW * 4
|
||||
S.cw4 = mbW * 2
|
||||
@@ -643,7 +680,9 @@ local function startPicture(self: any, sh: any, pts: number?)
|
||||
fillGap(self, sh)
|
||||
end
|
||||
end
|
||||
local p = newPicture(self, sps)
|
||||
local skip = sh.nalRefIdc == 0 and self.opts.skipDisposable == true
|
||||
-- a skipped picture only carries what output order and POC need
|
||||
local p = if skip then { skipped = true, short = false, long = false, waiting = false } else newPicture(self, sps)
|
||||
self.uid += 1
|
||||
p.uid = self.uid
|
||||
p.sps = sps
|
||||
@@ -660,6 +699,10 @@ local function startPicture(self: any, sh: any, pts: number?)
|
||||
self.decoded = 0
|
||||
self.sliceNum = 0
|
||||
self.sliceParams = {}
|
||||
if skip then
|
||||
return
|
||||
end
|
||||
p.skipped = false
|
||||
local S = self.S
|
||||
local st = S.sliceTab
|
||||
for i = 1, #st do
|
||||
@@ -720,20 +763,22 @@ local function finishPicture(self: any)
|
||||
end
|
||||
local sh = self.curSh
|
||||
local S = self.S
|
||||
if self.decoded < S.mbW * S.mbH then
|
||||
conceal(self, p)
|
||||
if not p.skipped then
|
||||
if self.decoded < S.mbW * S.mbH then
|
||||
conceal(self, p)
|
||||
end
|
||||
local hook = self.opts.yield
|
||||
if self.opts.deblock ~= false then
|
||||
Deblock.picture(S, p, self.sliceParams)
|
||||
end
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local w, h = S.mbW * 16, S.mbH * 16
|
||||
Inter.extend(p.y, p.yo, p.ys, w, h, PAD)
|
||||
Inter.extend(p.u, p.co, p.cs, w // 2, h // 2, PADC)
|
||||
Inter.extend(p.v, p.co, p.cs, w // 2, h // 2, PADC)
|
||||
end
|
||||
local hook = self.opts.yield
|
||||
if self.opts.deblock ~= false then
|
||||
Deblock.picture(S, p, self.sliceParams)
|
||||
end
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local w, h = S.mbW * 16, S.mbH * 16
|
||||
Inter.extend(p.y, p.yo, p.ys, w, h, PAD)
|
||||
Inter.extend(p.u, p.co, p.cs, w // 2, h // 2, PADC)
|
||||
Inter.extend(p.v, p.co, p.cs, w // 2, h // 2, PADC)
|
||||
local mmco5 = false
|
||||
if sh.nalRefIdc ~= 0 then
|
||||
mmco5 = mark(self, p, sh)
|
||||
@@ -979,6 +1024,10 @@ function Decoder.nal(self: any, buf: buffer, off: number, len: number, pts: numb
|
||||
end
|
||||
startPicture(self, sh, pts)
|
||||
end
|
||||
if self.cur.skipped then
|
||||
self.decoded = 1
|
||||
return
|
||||
end
|
||||
decodeSlice(self, sh, r)
|
||||
if self.decoded >= self.S.mbW * self.S.mbH then
|
||||
finishPicture(self)
|
||||
@@ -994,6 +1043,13 @@ function Decoder.nal(self: any, buf: buffer, off: number, len: number, pts: numb
|
||||
-- SEI (6), filler (12), SPS extension (13), prefix / SVC / MVC (14, 15, 20) and reserved types are ignored
|
||||
end
|
||||
|
||||
-- the end of a packet holding one access unit: a skipped picture is complete
|
||||
function Decoder.packetEnd(self: any)
|
||||
if self.cur and self.cur.skipped then
|
||||
finishPicture(self)
|
||||
end
|
||||
end
|
||||
|
||||
function Decoder.flush(self: any)
|
||||
finishPicture(self)
|
||||
bump(self, 0)
|
||||
|
||||
@@ -75,11 +75,11 @@ function Mb.new(): any
|
||||
}
|
||||
end
|
||||
|
||||
function Mb.grids(mbW: number, mbH: number): any
|
||||
function Mb.grids(mbW: number, mbH: number, hook: (() -> ())?): any
|
||||
local n = mbW * mbH
|
||||
local n4 = n * 16
|
||||
local nc = n * 4
|
||||
return {
|
||||
local S = {
|
||||
sliceTab = table.create(n, 0),
|
||||
kind = table.create(n, 0),
|
||||
cbpTab = table.create(n, 0),
|
||||
@@ -95,12 +95,27 @@ function Mb.grids(mbW: number, mbH: number): any
|
||||
nzcCb = table.create(nc, 0),
|
||||
nzcCr = table.create(nc, 0),
|
||||
nzDb = table.create(n4, 0),
|
||||
ipm = table.create(n4, 2),
|
||||
mvd0 = table.create(n4 * 2, 0),
|
||||
mvd1 = table.create(n4 * 2, 0),
|
||||
rgt0 = table.create(n4, 0),
|
||||
rgt1 = table.create(n4, 0),
|
||||
ipm = false :: any,
|
||||
mvd0 = false :: any,
|
||||
mvd1 = false :: any,
|
||||
rgt0 = false :: any,
|
||||
rgt1 = false :: any,
|
||||
}
|
||||
-- 1080p: 1.5 M slots, allocated in steps between yields (Decoder Options.yield)
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
S.ipm = table.create(n4, 2)
|
||||
S.mvd0 = table.create(n4 * 2, 0)
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
S.mvd1 = table.create(n4 * 2, 0)
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
S.rgt0, S.rgt1 = table.create(n4, 0), table.create(n4, 0)
|
||||
return S
|
||||
end
|
||||
|
||||
function Mb.start(S: any, M: any, addr: number)
|
||||
|
||||
@@ -19,14 +19,16 @@ export type Options = {
|
||||
lowDelay: boolean?,
|
||||
deblock: boolean?,
|
||||
yield: (() -> ())?,
|
||||
skipDisposable: boolean?, -- pictures with nal_ref_idc 0 come out as { skipped = true } frames without an image
|
||||
}
|
||||
|
||||
export type Frame = {
|
||||
image: Types.Image,
|
||||
image: Types.Image, -- (nil when skipped)
|
||||
pts: number,
|
||||
duration: number,
|
||||
poc: number,
|
||||
keyframe: boolean,
|
||||
skipped: boolean?,
|
||||
}
|
||||
|
||||
local DecoderMeta = {}
|
||||
@@ -129,6 +131,7 @@ function DecoderMeta.decode(self: H264Decoder, data: any, pts: number?, off: num
|
||||
break
|
||||
end
|
||||
end
|
||||
Decoder.packetEnd(st)
|
||||
local frames = Decoder.take(st)
|
||||
local e = status(self)
|
||||
if e then
|
||||
@@ -137,6 +140,11 @@ function DecoderMeta.decode(self: H264Decoder, data: any, pts: number?, off: num
|
||||
return frames, nil
|
||||
end
|
||||
|
||||
-- hands a frame's Image back for reuse by a later frame (the caller is done with it); false if it was not this decoder's
|
||||
function DecoderMeta.release(self: H264Decoder, image: Types.Image): boolean
|
||||
return self.state.images:give(image)
|
||||
end
|
||||
|
||||
function DecoderMeta.flush(self: H264Decoder): { Frame }
|
||||
Decoder.flush(self.state)
|
||||
status(self)
|
||||
@@ -172,7 +180,7 @@ end
|
||||
|
||||
function H264.newVideoDecoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local o: Options = { maxPixels = opts and opts.maxPixels, deblock = opts and opts.deblock, lowDelay = opts and opts.lowDelay,
|
||||
yield = opts and opts.yield }
|
||||
yield = opts and opts.yield, skipDisposable = opts and opts.skipDisposable }
|
||||
local function make(): (H264Decoder?, Err.Err?)
|
||||
local d, err = H264.newDecoder(o)
|
||||
if d and track.config then
|
||||
@@ -190,7 +198,7 @@ function H264.newVideoDecoder(track: Types.Track, opts: any?): (Types.VideoCodec
|
||||
local queue: { any } = {}
|
||||
local pool: { number } = {}
|
||||
local outIndex: number? = if track.ptsInOutputOrder then 0 else nil
|
||||
local self: any = {}
|
||||
local self: any = { skipped = 0 }
|
||||
local function push(frames: { Frame })
|
||||
for _, f in frames do
|
||||
local pts = f.pts
|
||||
@@ -201,7 +209,12 @@ function H264.newVideoDecoder(track: Types.Track, opts: any?): (Types.VideoCodec
|
||||
-- pictures take the packet timestamps in increasing order (right for MP4 / MKV / TS pts and AVI's decode-order times)
|
||||
pts = table.remove(pool, 1)
|
||||
end
|
||||
table.insert(queue, { f.image, pts, f.keyframe })
|
||||
if f.skipped then
|
||||
self.skipped += 1
|
||||
table.insert(queue, { false, pts, false })
|
||||
else
|
||||
table.insert(queue, { f.image, pts, f.keyframe })
|
||||
end
|
||||
end
|
||||
end
|
||||
function self.decode(_: any, packet: Types.Packet): Err.Err?
|
||||
@@ -225,7 +238,7 @@ function H264.newVideoDecoder(track: Types.Track, opts: any?): (Types.VideoCodec
|
||||
push(frames)
|
||||
return nil
|
||||
end
|
||||
function self.receive(_: any): (Types.Image?, number?, boolean?)
|
||||
function self.receive(_: any): (Types.Image? | false, number?, boolean?)
|
||||
local q = table.remove(queue, 1)
|
||||
if q then
|
||||
return q[1], q[2], q[3]
|
||||
@@ -237,6 +250,9 @@ function H264.newVideoDecoder(track: Types.Track, opts: any?): (Types.VideoCodec
|
||||
push((dec :: H264Decoder):flush())
|
||||
end
|
||||
end
|
||||
function self.release(_: any, image: Types.Image): boolean
|
||||
return dec ~= nil and (dec :: H264Decoder):release(image)
|
||||
end
|
||||
function self.reset(_: any)
|
||||
dec = make()
|
||||
table.clear(queue)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
local Types = require("@rosetta/Types")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local D = require("@rosetta/codecs/video/Mpeg2/Decoder")
|
||||
local Pool = require("@rosetta/codecs/video/Video/Pool")
|
||||
|
||||
local Mpeg2 = {}
|
||||
|
||||
@@ -248,7 +249,8 @@ function Mpeg2.toRgba(y: buffer, cb: buffer, cr: buffer, codedWidth: number, wid
|
||||
return true
|
||||
end
|
||||
|
||||
function Mpeg2.frameImage(frame: Frame, rgba: boolean?): Types.Image?
|
||||
-- pool: where the Image comes from (a recycled one when the caller released one of the right size)
|
||||
function Mpeg2.frameImage(frame: Frame, rgba: boolean?, pool: Pool.Pool?): Types.Image?
|
||||
if type(frame) ~= "table" or type(frame.y) ~= "buffer" or type(frame.cb) ~= "buffer" or type(frame.cr) ~= "buffer" then
|
||||
return nil
|
||||
end
|
||||
@@ -264,14 +266,14 @@ function Mpeg2.frameImage(frame: Frame, rgba: boolean?): Types.Image?
|
||||
end
|
||||
local img
|
||||
if rgba then
|
||||
img = Types.Image.new(w, h)
|
||||
img = if pool then pool:take(w, h, "rgba8") else Types.Image.new(w, h)
|
||||
if c422 then
|
||||
D.toRgba422(frame.y, frame.cb, frame.cr, cw, w, h, img.pixels)
|
||||
else
|
||||
D.toRgba(frame.y, frame.cb, frame.cr, cw, w, h, img.pixels)
|
||||
end
|
||||
else
|
||||
img = Types.Image.new(w, h, "yuv420p")
|
||||
img = if pool then pool:take(w, h, "yuv420p") else Types.Image.new(w, h, "yuv420p")
|
||||
local px = img.pixels
|
||||
for y = 0, h - 1 do
|
||||
buffer.copy(px, y * w, frame.y, y * cw, w)
|
||||
@@ -317,10 +319,11 @@ function Mpeg2.frameImage(frame: Frame, rgba: boolean?): Types.Image?
|
||||
end
|
||||
|
||||
function Mpeg2.newVideoDecoder(_track: any, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local dec = Mpeg2.new({ reorder = true, rgba = false, maxPixels = opts and opts.maxPixels })
|
||||
local dec = Mpeg2.new({ reorder = true, rgba = false, maxPixels = opts and opts.maxPixels, yield = opts and opts.yield })
|
||||
local queue: { any } = {}
|
||||
local images = Pool.new()
|
||||
local function push(f: Frame)
|
||||
local img = Mpeg2.frameImage(f)
|
||||
local img = Mpeg2.frameImage(f, false, images)
|
||||
if img then
|
||||
img.meta.yuv = { range = "limited", matrix = "bt601" }
|
||||
local tag = f.tag
|
||||
@@ -360,6 +363,9 @@ function Mpeg2.newVideoDecoder(_track: any, opts: any?): (Types.VideoCodecDecode
|
||||
dec:reset()
|
||||
table.clear(queue)
|
||||
end
|
||||
function self.release(_: any, image: Types.Image): boolean
|
||||
return images:give(image)
|
||||
end
|
||||
return self :: any, nil
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/video/Video/Disposable: packets whose frame no later frame predicts from (a player may skip decoding them).
|
||||
-- H.264: the first VCL NAL unit has nal_ref_idc 0 (7.4.1: then every slice of the picture has), and no parameter
|
||||
-- set comes before it; one access unit per packet, length-prefixed (avcC lengthSizeMinusOne) or Annex B.
|
||||
-- VP8 (RFC 6386 9.3-9.11, 19.2): an inter frame that refreshes no reference (last / golden / altref, no copies) and
|
||||
-- leaves the segment map; its header may still change decoder state (probabilities, loop filter deltas).
|
||||
local readu8 = buffer.readu8
|
||||
|
||||
local Disposable = {}
|
||||
|
||||
-- the offset of the first byte after the first start code at or after p, or -1
|
||||
local function nextStart(b: buffer, p: number, e: number): number
|
||||
local i = p
|
||||
while i + 2 < e do
|
||||
local b2 = readu8(b, i + 2)
|
||||
if b2 > 1 then
|
||||
i += 3
|
||||
elseif readu8(b, i + 1) ~= 0 then
|
||||
i += 2
|
||||
elseif b2 == 1 and readu8(b, i) == 0 then
|
||||
return i + 3
|
||||
else
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
return -1
|
||||
end
|
||||
|
||||
-- nal_unit_type 1..5: slices (decided by nal_ref_idc); 7, 8, 13, 15: parameter sets (never dropped)
|
||||
local function nalVerdict(hdr: number): boolean?
|
||||
local t = hdr % 32
|
||||
if t >= 1 and t <= 5 then
|
||||
return hdr // 32 % 4 == 0
|
||||
elseif t == 7 or t == 8 or t == 13 or t == 15 then
|
||||
return false
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function Disposable.h264(b: buffer, lengthSize: number?): boolean
|
||||
local n = buffer.len(b)
|
||||
if lengthSize then
|
||||
local p = 0
|
||||
while p + lengthSize < n do
|
||||
local len = 0
|
||||
for k = 0, lengthSize - 1 do
|
||||
len = len * 256 + readu8(b, p + k)
|
||||
end
|
||||
p += lengthSize
|
||||
if len > n - p then
|
||||
return false
|
||||
end
|
||||
if len > 0 then
|
||||
local v = nalVerdict(readu8(b, p))
|
||||
if v ~= nil then
|
||||
return v
|
||||
end
|
||||
end
|
||||
p += len
|
||||
end
|
||||
return false
|
||||
end
|
||||
if n >= 3 and readu8(b, 0) == 0 and readu8(b, 1) == 0 then
|
||||
local p = nextStart(b, 0, n)
|
||||
while p >= 0 and p < n do
|
||||
local v = nalVerdict(readu8(b, p))
|
||||
if v ~= nil then
|
||||
return v
|
||||
end
|
||||
p = nextStart(b, p, n)
|
||||
end
|
||||
return false
|
||||
end
|
||||
return n > 0 and nalVerdict(readu8(b, 0)) == true
|
||||
end
|
||||
|
||||
-- the boolean decoder of RFC 6386 7.3 (a 2-byte window), enough for the frame header
|
||||
type Bool = { b: buffer, p: number, e: number, value: number, range: number, count: number }
|
||||
|
||||
local function byteAt(d: Bool): number
|
||||
local p = d.p
|
||||
d.p = p + 1
|
||||
return if p < d.e then readu8(d.b, p) else 0
|
||||
end
|
||||
|
||||
local function boolNew(b: buffer, p: number, e: number): Bool
|
||||
local d: Bool = { b = b, p = p, e = e, value = 0, range = 255, count = 0 }
|
||||
d.value = byteAt(d) * 256 + byteAt(d)
|
||||
return d
|
||||
end
|
||||
|
||||
local function boolRead(d: Bool, prob: number): number
|
||||
local split = 1 + (d.range - 1) * prob // 256
|
||||
local big = split * 256
|
||||
local bit = 0
|
||||
if d.value >= big then
|
||||
bit = 1
|
||||
d.range -= split
|
||||
d.value -= big
|
||||
else
|
||||
d.range = split
|
||||
end
|
||||
while d.range < 128 do
|
||||
d.value *= 2
|
||||
d.range *= 2
|
||||
d.count += 1
|
||||
if d.count == 8 then
|
||||
d.count = 0
|
||||
d.value += byteAt(d)
|
||||
end
|
||||
end
|
||||
return bit
|
||||
end
|
||||
|
||||
local function lit(d: Bool, n: number): number
|
||||
local v = 0
|
||||
for _ = 1, n do
|
||||
v = v * 2 + boolRead(d, 128)
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- skips flag ? (n bits + sign) : nothing
|
||||
local function optSigned(d: Bool, n: number)
|
||||
if lit(d, 1) == 1 then
|
||||
lit(d, n + 1)
|
||||
end
|
||||
end
|
||||
|
||||
-- disposable: no reference refresh or copy and no segment map update; droppable: also no state the decoder keeps
|
||||
-- (refresh_entropy_probs 0, no segment feature or loop filter delta update)
|
||||
function Disposable.vp8(b: buffer): (boolean, boolean)
|
||||
local n = buffer.len(b)
|
||||
if n < 4 then
|
||||
return false, false
|
||||
end
|
||||
local tag = readu8(b, 0) + readu8(b, 1) * 256 + readu8(b, 2) * 65536
|
||||
if tag % 2 == 0 then
|
||||
return false, false -- key frame
|
||||
end
|
||||
local first = tag // 32
|
||||
if first > n - 3 then
|
||||
return false, false
|
||||
end
|
||||
local d = boolNew(b, 3, 3 + first)
|
||||
local keep = true
|
||||
if lit(d, 1) == 1 then -- segmentation_enabled
|
||||
local updateMap, updateData = lit(d, 1), lit(d, 1)
|
||||
if updateMap == 1 then
|
||||
return false, false
|
||||
end
|
||||
if updateData == 1 then
|
||||
keep = false
|
||||
lit(d, 1) -- segment_feature_mode
|
||||
for _ = 1, 4 do
|
||||
optSigned(d, 7)
|
||||
end
|
||||
for _ = 1, 4 do
|
||||
optSigned(d, 6)
|
||||
end
|
||||
end
|
||||
end
|
||||
lit(d, 1 + 6 + 3) -- filter_type, loop_filter_level, sharpness_level
|
||||
if lit(d, 1) == 1 and lit(d, 1) == 1 then -- loop_filter_adj_enable, mode_ref_lf_delta_update
|
||||
keep = false
|
||||
for _ = 1, 8 do
|
||||
optSigned(d, 6)
|
||||
end
|
||||
end
|
||||
lit(d, 2 + 7) -- log2_nbr_of_dct_partitions, y_ac_qi
|
||||
for _ = 1, 5 do
|
||||
optSigned(d, 4)
|
||||
end
|
||||
local refreshGolden, refreshAlt = lit(d, 1), lit(d, 1)
|
||||
if refreshGolden == 1 or refreshAlt == 1 then
|
||||
return false, false
|
||||
end
|
||||
if lit(d, 2) ~= 0 or lit(d, 2) ~= 0 then -- copy_buffer_to_golden, copy_buffer_to_alternate
|
||||
return false, false
|
||||
end
|
||||
lit(d, 2) -- sign_bias_golden, sign_bias_alternate
|
||||
local refreshEntropy, refreshLast = lit(d, 1), lit(d, 1)
|
||||
if refreshLast == 1 or d.p > d.e + 2 then
|
||||
return false, false
|
||||
end
|
||||
return true, keep and refreshEntropy == 0
|
||||
end
|
||||
|
||||
-- the avcC NAL length size of a track (nil: Annex B or one bare NAL unit per packet)
|
||||
function Disposable.lengthSize(config: buffer?): number?
|
||||
if config and buffer.len(config) >= 7 and readu8(config, 0) == 1 then
|
||||
return readu8(config, 4) % 4 + 1
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- disposable: no later frame predicts from it; droppable: it can also be left out without telling the decoder
|
||||
function Disposable.check(codec: string?, data: buffer, config: buffer?): (boolean, boolean)
|
||||
if codec == "h264" then
|
||||
local v = Disposable.h264(data, Disposable.lengthSize(config))
|
||||
return v, v
|
||||
elseif codec == "vp8" then
|
||||
return Disposable.vp8(data)
|
||||
end
|
||||
return false, false
|
||||
end
|
||||
|
||||
return Disposable
|
||||
@@ -0,0 +1,50 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/video/Video/Pool: output Images handed back by the caller (decoder:release), reused for later frames. Only
|
||||
-- Images this pool made are taken back; a reused Image gets a fresh meta table.
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
local Pool = {}
|
||||
Pool.__index = Pool
|
||||
|
||||
export type Pool = typeof(setmetatable({} :: { free: { Types.Image }, made: { [Types.Image]: boolean }, max: number }, Pool))
|
||||
|
||||
function Pool.new(max: number?): Pool
|
||||
return setmetatable({ free = {}, made = setmetatable({}, { __mode = "k" }) :: any, max = max or 3 }, Pool)
|
||||
end
|
||||
|
||||
function Pool.take(self: Pool, w: number, h: number, format: Types.PixelFormat): Types.Image
|
||||
local free = self.free
|
||||
for i = #free, 1, -1 do
|
||||
local img = free[i]
|
||||
if img.width == w and img.height == h and img.format == format then
|
||||
table.remove(free, i)
|
||||
img.meta = {}
|
||||
return img
|
||||
end
|
||||
end
|
||||
local img = Types.Image.new(w, h, format)
|
||||
self.made[img] = true
|
||||
return img
|
||||
end
|
||||
|
||||
-- true when the Image was this pool's (it is kept for reuse, or dropped when the pool is full)
|
||||
function Pool.give(self: Pool, img: any): boolean
|
||||
if type(img) ~= "table" or not self.made[img] then
|
||||
return false
|
||||
end
|
||||
local free = self.free
|
||||
for _, q in free do
|
||||
if q == img then
|
||||
return true
|
||||
end
|
||||
end
|
||||
if #free < self.max then
|
||||
table.insert(free, img)
|
||||
else
|
||||
self.made[img] = nil
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return Pool
|
||||
@@ -4,6 +4,8 @@
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Yuv = require("@rosetta/codecs/image/Yuv")
|
||||
local Disposable = require("@rosetta/codecs/video/Video/Disposable")
|
||||
local Pool = require("@rosetta/codecs/video/Video/Pool")
|
||||
|
||||
local Video = {}
|
||||
|
||||
@@ -16,8 +18,8 @@ export type Entry = {
|
||||
[string]: any,
|
||||
}
|
||||
|
||||
-- decodeOne -> nil, nil: a packet with no picture to show (a hidden VP8 altref frame)
|
||||
function Video.simpleDecoder(decodeOne: (data: buffer, packet: Types.Packet) -> (Types.Image?, Err.Err?), resetFn: (() -> ())?): Types.VideoCodecDecoder
|
||||
-- decodeOne -> nil, nil: a packet with no picture to show (a hidden VP8 altref frame); false: a picture skipped
|
||||
function Video.simpleDecoder(decodeOne: (data: buffer, packet: Types.Packet) -> (Types.Image? | false, Err.Err?), resetFn: (() -> ())?): Types.VideoCodecDecoder
|
||||
local queue: { any } = {}
|
||||
local self = {}
|
||||
function self.decode(_: any, packet: Types.Packet): Err.Err?
|
||||
@@ -25,12 +27,12 @@ function Video.simpleDecoder(decodeOne: (data: buffer, packet: Types.Packet) ->
|
||||
if not ok then
|
||||
return Err.new(Err.CORRUPT, "decoder threw: " .. tostring(img))
|
||||
end
|
||||
if img then
|
||||
if img ~= nil then
|
||||
table.insert(queue, { img, packet.pts, packet.keyframe })
|
||||
end
|
||||
return err
|
||||
end
|
||||
function self.receive(_: any): (Types.Image?, number?, boolean?)
|
||||
function self.receive(_: any): (Types.Image? | false, number?, boolean?)
|
||||
local q = table.remove(queue, 1)
|
||||
if q then
|
||||
return q[1], q[2], q[3]
|
||||
@@ -176,11 +178,17 @@ end
|
||||
local function vp8Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local Vp8 = require("@rosetta/codecs/image/Vp8")
|
||||
local dec = Vp8.newDecoder()
|
||||
local o = { maxPixels = opts and opts.maxPixels }
|
||||
return Video.simpleDecoder(function(data: buffer): (Types.Image?, Err.Err?)
|
||||
local o = { maxPixels = opts and opts.maxPixels, yield = opts and opts.yield, skipDisposable = opts and opts.skipDisposable }
|
||||
local self: any
|
||||
self = Video.simpleDecoder(function(data: buffer): (Types.Image? | false, Err.Err?)
|
||||
local img, err = dec:decodeFrame(data, 0, buffer.len(data), o)
|
||||
if not img then
|
||||
return nil, err
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
self.skipped += 1
|
||||
-- show_frame (RFC 6386 9.1): a shown frame keeps its place
|
||||
return if buffer.readu8(data, 0) // 16 % 2 == 1 then false else nil, nil
|
||||
end
|
||||
local src = img.meta.source
|
||||
if type(src) == "table" and src.show == false then
|
||||
@@ -194,7 +202,12 @@ local function vp8Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecDec
|
||||
else
|
||||
dec = Vp8.newDecoder()
|
||||
end
|
||||
end), nil
|
||||
end) :: any
|
||||
self.skipped = 0
|
||||
function self.release(_: any, image: Types.Image): boolean
|
||||
return dec:release(image)
|
||||
end
|
||||
return self, nil
|
||||
end
|
||||
|
||||
-- Motion JPEG: yuv420p straight from the planes (full range) when the layout allows, else rgba8
|
||||
@@ -321,6 +334,18 @@ function Video.entry(id: string): Entry?
|
||||
return byId[id]
|
||||
end
|
||||
|
||||
-- disposable: no later frame predicts from the packet's; droppable: it may also be left out unread (VP8 headers can
|
||||
-- carry probability and loop filter updates, so a disposable VP8 frame is droppable only without them)
|
||||
function Video.isDisposable(track: Types.Track | string, packet: Types.Packet | buffer): (boolean, boolean)
|
||||
local codec = if type(track) == "table" then track.codec else track
|
||||
local config = if type(track) == "table" then track.config else nil
|
||||
local data = if type(packet) == "table" then packet.data else packet
|
||||
if type(data) ~= "buffer" or type(codec) ~= "string" then
|
||||
return false, false
|
||||
end
|
||||
return Disposable.check(codec, data, config)
|
||||
end
|
||||
|
||||
function Video.canDecode(id: string): boolean
|
||||
local e = byId[id]
|
||||
return e ~= nil and e.new ~= nil
|
||||
@@ -352,6 +377,8 @@ export type OpenOptions = {
|
||||
audio: boolean?,
|
||||
maxSide: number?,
|
||||
yield: (() -> ())?,
|
||||
skipDisposable: boolean?,
|
||||
reuse: boolean?, -- nextFrame may overwrite the Image of the frame it returned before
|
||||
[string]: any,
|
||||
}
|
||||
|
||||
@@ -417,14 +444,34 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
|
||||
v.startTime = start
|
||||
local defaultDur = if v.frameRate > 0 then 1 / v.frameRate else 0
|
||||
local lastPts = nil :: number?
|
||||
-- rgba8 / scaled Images made here; the codec's own go back through codec:release
|
||||
local own = Pool.new(2)
|
||||
local function recycle(img: any)
|
||||
if img and not own:give(img) and codec and codec.release then
|
||||
codec:release(img)
|
||||
end
|
||||
end
|
||||
local lastFrame: any = nil
|
||||
function v.release(self: any, frame: any)
|
||||
recycle(if type(frame) == "table" and frame.kind == "image" then frame else type(frame) == "table" and frame.image)
|
||||
end
|
||||
function v.nextFrame(self: any): (Types.VideoFrame?, Err.Err?)
|
||||
if not codec then
|
||||
return nil, codecErr
|
||||
end
|
||||
if lastFrame then
|
||||
recycle(lastFrame.image)
|
||||
lastFrame = nil
|
||||
end
|
||||
local dec = codec :: Types.VideoCodecDecoder
|
||||
while true do
|
||||
local img, pts, key = dec:receive()
|
||||
if img then
|
||||
if img == false then
|
||||
-- a picture skipped (skipDisposable): it keeps its place in output order
|
||||
if index >= 0 then
|
||||
index += 1
|
||||
end
|
||||
elseif img then
|
||||
local t = (pts or 0) / scale - start
|
||||
if t + 1e-9 >= skipUntil then
|
||||
if index < 0 then
|
||||
@@ -438,18 +485,27 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
|
||||
end
|
||||
lastPts = t
|
||||
local maxSide = o.maxSide
|
||||
local hook = o.yield
|
||||
if hook and (o.rgba or maxSide) then
|
||||
pcall(hook)
|
||||
end
|
||||
if maxSide and maxSide >= 1 and img.format == "yuv420p" and math.max(img.width, img.height) > maxSide then
|
||||
local hook: any = o.yield
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local s = maxSide / math.max(img.width, img.height)
|
||||
img = Yuv.imageScale(img, math.max(1, math.floor(img.width * s + 0.5)), math.max(1, math.floor(img.height * s + 0.5)))
|
||||
local dw, dh = math.max(1, math.floor(img.width * s + 0.5)), math.max(1, math.floor(img.height * s + 0.5))
|
||||
local src = img
|
||||
img = Yuv.imageScale(src, dw, dh, own:take(dw, dh, "yuv420p"), o.yield)
|
||||
recycle(src) -- (never seen by the caller)
|
||||
end
|
||||
if o.rgba then
|
||||
img = Yuv.imageToRgba(img) or img
|
||||
if o.rgba and img.format == "yuv420p" then
|
||||
local src = img
|
||||
img = Yuv.imageToRgba(src, own:take(src.width, src.height, "rgba8"), o.yield) or src
|
||||
recycle(src)
|
||||
end
|
||||
return { image = img, pts = t, duration = d, keyframe = key, index = index }, nil
|
||||
local frame = { image = img, pts = t, duration = d, keyframe = key, index = index }
|
||||
if o.reuse then
|
||||
lastFrame = frame
|
||||
end
|
||||
return frame, nil
|
||||
end
|
||||
else
|
||||
local p = demuxer:read(tid)
|
||||
@@ -469,6 +525,7 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
|
||||
end
|
||||
end
|
||||
function v.seek(self: any, seconds: number)
|
||||
lastFrame = nil
|
||||
demuxer:seek(seconds + start)
|
||||
if codec then
|
||||
codec:reset()
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
-- containers/Demux: the indexed Types.Demuxer every video container builds (Matroska, AVI, MPEG-TS / PS, Y4M, MP4).
|
||||
-- New in rosetta. Packets are offset + length into the source buffer (or own bytes); per-track cursors are independent.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Disposable = require("@rosetta/codecs/video/Video/Disposable")
|
||||
|
||||
local Demux = {}
|
||||
local D = {}
|
||||
@@ -171,6 +172,16 @@ function D._packet(self: Demux, i: number): Types.Packet
|
||||
p[k] = v
|
||||
end
|
||||
end
|
||||
local tid = p.track
|
||||
for _, t in self.tracks do
|
||||
if t.id == tid then
|
||||
local c = t.codec
|
||||
if (c == "h264" or c == "vp8") and Disposable.check(c, data :: buffer, t.config) then
|
||||
p.disposable = true
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
return p
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
-- video decoder control tests: the cooperative yield hook (H.264, VP8, MPEG-1, rgba / scale conversions), disposable
|
||||
-- packets and skipDisposable, frame reuse (opts.reuse, v:release, decoder:release); outputs against ffmpeg's CRCs.
|
||||
-- Fixtures: tools/fixtures/h264.py (h264_containers), tools/fixtures/videoctl.py, tools/fixtures/videobench.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@rosetta")
|
||||
local Crc32 = require("@rosetta/core/Crc32")
|
||||
local Video = require("@rosetta/codecs/video/Video")
|
||||
local Disposable = require("@rosetta/codecs/video/Video/Disposable")
|
||||
local H264 = require("@rosetta/codecs/video/H264")
|
||||
local Vp8 = require("@rosetta/codecs/image/Vp8")
|
||||
local Yuv = require("@rosetta/codecs/image/Yuv")
|
||||
local H264Cases = require("@tests/gen/h264_containers")
|
||||
local Vp8Cases = require("@tests/gen/videoctl_vp8")
|
||||
local Bench = require("@tests/gen/videobench")
|
||||
|
||||
local t = T.suite("video/control")
|
||||
|
||||
local function crcs(v: any, rgba: boolean?): ({ number }, { number }, any)
|
||||
local out, idx = {}, {}
|
||||
while true do
|
||||
local f, err = v:nextFrame()
|
||||
if not f then
|
||||
return out, idx, err
|
||||
end
|
||||
table.insert(out, Crc32.of(f.image.pixels))
|
||||
table.insert(idx, f.index)
|
||||
end
|
||||
end
|
||||
|
||||
local function same(a: { number }, b: { number }): boolean
|
||||
if #a ~= #b then
|
||||
return false
|
||||
end
|
||||
for i = 1, #a do
|
||||
if a[i] ~= b[i] then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function ivfFrames(b: buffer): { buffer }
|
||||
local out = {}
|
||||
local p = buffer.readu16(b, 6)
|
||||
while p + 12 <= buffer.len(b) do
|
||||
local n = buffer.readu32(b, p)
|
||||
if p + 12 + n > buffer.len(b) then
|
||||
break
|
||||
end
|
||||
local f = buffer.create(n)
|
||||
buffer.copy(f, 0, b, p + 12, n)
|
||||
table.insert(out, f)
|
||||
p += 12 + n
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- yield: the hook suspends a coroutine at every call; frames stay identical
|
||||
local function stepped(data: any, opts: { [string]: any }): ({ number }, number, number)
|
||||
local calls = 0
|
||||
local o = table.clone(opts)
|
||||
o.audio = false
|
||||
o.yield = function()
|
||||
calls += 1
|
||||
coroutine.yield()
|
||||
end
|
||||
local v = assert(F.video.open(data, o))
|
||||
local got: { number } = {}
|
||||
local co = coroutine.create(function()
|
||||
got = crcs(v)
|
||||
end)
|
||||
local steps = 0
|
||||
while coroutine.status(co) ~= "dead" do
|
||||
local ok, err = coroutine.resume(co)
|
||||
assert(ok, err)
|
||||
steps += 1
|
||||
end
|
||||
return got, calls, steps
|
||||
end
|
||||
|
||||
local yieldInputs = {
|
||||
{ "h264 mp4", H264Cases[2].data },
|
||||
{ "vp8 webm", Vp8Cases[1].webm },
|
||||
}
|
||||
for _, c in Bench do
|
||||
if c.size == "320x240" and (c.name == "mpeg1.ts" or c.name == "vp8.webm") then
|
||||
table.insert(yieldInputs, { c.name, c.data })
|
||||
end
|
||||
end
|
||||
for _, c in yieldInputs do
|
||||
for _, opts in { {}, { rgba = true }, { maxSide = 64 }, { maxSide = 64, rgba = true } } do
|
||||
local tag = string.format("%s rgba=%s maxSide=%s", c[1], tostring(opts.rgba), tostring(opts.maxSide))
|
||||
local o = table.clone(opts)
|
||||
o.audio = false
|
||||
local want = crcs(assert(F.video.open(c[2], o)))
|
||||
local got, calls, steps = stepped(c[2], opts)
|
||||
t:check(#want > 0 and same(got, want), tag .. ": frames identical under the yield hook")
|
||||
t:check(calls > #want and steps == calls + 1, string.format("%s: hook called %d times for %d frames, %d steps", tag, calls, #want, steps))
|
||||
end
|
||||
end
|
||||
|
||||
do -- Yuv.toRgba: the hook every 16 rows, same pixels
|
||||
local img = F.Types.Image.new(40, 70, "yuv420p")
|
||||
for i = 0, buffer.len(img.pixels) - 1 do
|
||||
buffer.writeu8(img.pixels, i, (i * 7) % 256)
|
||||
end
|
||||
local calls = 0
|
||||
local a = assert(Yuv.imageToRgba(img))
|
||||
local out = F.Types.Image.new(40, 70, "rgba8")
|
||||
local b = assert(Yuv.imageToRgba(img, out, function()
|
||||
calls += 1
|
||||
end))
|
||||
t:check(b == out and buffer.tostring(a.pixels) == buffer.tostring(b.pixels), "imageToRgba into an out Image, with a hook")
|
||||
t:eq(calls, 4, "toRgba hook calls (rows 16, 32, 48, 64)")
|
||||
local s1 = Yuv.imageScale(img, 20, 30)
|
||||
calls = 0
|
||||
local s2 = Yuv.imageScale(img, 20, 30, F.Types.Image.new(20, 30, "yuv420p"), function()
|
||||
calls += 1
|
||||
end)
|
||||
t:check(buffer.tostring(s1.pixels) == buffer.tostring(s2.pixels) and calls > 0, "imageScale into an out Image, with a hook")
|
||||
end
|
||||
|
||||
-- disposable packets: H.264 (nal_ref_idc 0) in every container, and skipDisposable keeps the rest bit-exact
|
||||
for _, c in H264Cases do
|
||||
local v = assert(F.video.open(c.data, { audio = false }))
|
||||
local d = v.demuxer
|
||||
local n, nd, agree = 0, 0, true
|
||||
for p in d:packets(v.track.id) do
|
||||
n += 1
|
||||
if p.disposable then
|
||||
nd += 1
|
||||
end
|
||||
if (p.disposable == true) ~= (Video.isDisposable(v.track, p)) then
|
||||
agree = false
|
||||
end
|
||||
end
|
||||
local want = if string.sub(c.name, 1, 7) == "cabac_b" then 9 else 0
|
||||
t:eq(nd, want, c.name .. ": disposable packets")
|
||||
t:check(agree, c.name .. ": packet.disposable == Video.isDisposable")
|
||||
local v2 = assert(F.video.open(c.data, { audio = false, skipDisposable = true }))
|
||||
local got, idx, err = crcs(v2)
|
||||
local good = 0
|
||||
for i, crc in got do
|
||||
if c.frames[idx[i]] == crc then
|
||||
good += 1
|
||||
end
|
||||
end
|
||||
t:check(err == nil and #got == #c.frames - nd and good == #got, string.format("%s: skipDisposable %d frames shown of %d, %d bit-exact at their index", c.name, #got, #c.frames, good))
|
||||
end
|
||||
|
||||
do -- H.264 decoder API: skipped frames keep their place
|
||||
local c = H264Cases[1]
|
||||
assert(c.name == "cabac_b.h264")
|
||||
local all = assert(H264.decodeAll(c.data))
|
||||
local some = assert(H264.decodeAll(c.data, { skipDisposable = true }))
|
||||
local skipped, good = 0, 0
|
||||
for i, f in some do
|
||||
if f.skipped then
|
||||
skipped += 1
|
||||
t:check(f.image == nil and f.pts == all[i].pts, "skipped frame " .. i .. ": no image, its pts")
|
||||
elseif Crc32.of(f.image.pixels) == c.frames[i] then
|
||||
good += 1
|
||||
end
|
||||
end
|
||||
t:check(#some == #all and skipped == 9 and good == #all - 9, string.format("decodeAll skipDisposable: %d skipped, %d / %d bit-exact", skipped, good, #all - 9))
|
||||
local au = H264.accessUnits(c.data)
|
||||
local hits = 0
|
||||
for i = 1, #au, 3 do
|
||||
local b = buffer.create(au[i + 1])
|
||||
buffer.copy(b, 0, buffer.fromstring(c.data), au[i], au[i + 1])
|
||||
if Video.isDisposable("h264", b) then
|
||||
hits += 1
|
||||
end
|
||||
end
|
||||
t:eq(hits, 9, "Annex B access units: disposable")
|
||||
end
|
||||
|
||||
-- VP8: frames refreshing no reference (two temporal layers)
|
||||
for _, c in Vp8Cases do
|
||||
local frames = ivfFrames(buffer.fromstring(c.ivf))
|
||||
local nd, ndrop = 0, 0
|
||||
local dec, skipDec = Vp8.newDecoder(), Vp8.newDecoder()
|
||||
local exact, skipped, wrong = 0, 0, 0
|
||||
for i, f in frames do
|
||||
local disp, drop = Disposable.vp8(f)
|
||||
nd += if disp then 1 else 0
|
||||
ndrop += if drop then 1 else 0
|
||||
local full = assert(dec:decodeFrame(f))
|
||||
local img, err = skipDec:decodeFrame(f, nil, nil, { skipDisposable = true })
|
||||
if img then
|
||||
exact += if Crc32.of(img.pixels) == Crc32.of(full.pixels) and Crc32.of(full.pixels) == c.crcs[i] then 1 else 0
|
||||
elseif err == nil then
|
||||
skipped += 1
|
||||
wrong += if disp then 0 else 1
|
||||
end
|
||||
end
|
||||
t:check(nd >= 8 and ndrop == 0, string.format("%s: %d of %d frames disposable, %d droppable", c.name, nd, #frames, ndrop))
|
||||
t:check(skipped == nd and wrong == 0 and exact == #frames - nd, string.format("%s: decodeFrame skipDisposable skipped %d, %d others bit-exact", c.name, skipped, exact))
|
||||
local v = assert(F.video.open(c.webm, { audio = false }))
|
||||
local pd = 0
|
||||
for p in v.demuxer:packets(v.track.id) do
|
||||
pd += if p.disposable then 1 else 0
|
||||
end
|
||||
t:eq(pd, nd, c.name .. ": webm packet.disposable")
|
||||
local got, idx = crcs(assert(F.video.open(c.webm, { audio = false, skipDisposable = true })))
|
||||
local good = 0
|
||||
for i, crc in got do
|
||||
good += if c.crcs[idx[i]] == crc then 1 else 0
|
||||
end
|
||||
t:check(#got == #frames - nd and good == #got, string.format("%s: webm skipDisposable %d shown, %d bit-exact at their index", c.name, #got, good))
|
||||
end
|
||||
|
||||
-- reuse: frames identical, Images recycled
|
||||
local reuseInputs = {
|
||||
{ "h264 mp4", H264Cases[2].data },
|
||||
{ "h264 ts", H264Cases[4].data },
|
||||
{ "vp8 webm", Vp8Cases[1].webm },
|
||||
}
|
||||
for _, c in Bench do
|
||||
if c.size == "320x240" and c.name ~= "raw.y4m" then
|
||||
table.insert(reuseInputs, { c.name, c.data })
|
||||
end
|
||||
end
|
||||
for _, c in reuseInputs do
|
||||
for _, opts in { {}, { rgba = true }, { maxSide = 64 } } do
|
||||
local tag = string.format("%s rgba=%s maxSide=%s", c[1], tostring(opts.rgba), tostring(opts.maxSide))
|
||||
local o = table.clone(opts)
|
||||
o.audio = false
|
||||
local want = crcs(assert(F.video.open(c[2], o)))
|
||||
o.reuse = true
|
||||
local v = assert(F.video.open(c[2], o))
|
||||
local got, seen, again = {}, {}, 0
|
||||
while true do
|
||||
local f = v:nextFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
table.insert(got, Crc32.of(f.image.pixels))
|
||||
if seen[f.image.pixels] then
|
||||
again += 1
|
||||
end
|
||||
seen[f.image.pixels] = true
|
||||
end
|
||||
t:check(same(got, want), tag .. ": reuse frames identical")
|
||||
local recycles = c[1] ~= "mjpeg.avi" or opts.rgba or opts.maxSide
|
||||
t:check(not recycles or again > 0, string.format("%s: %d of %d frames in a recycled buffer", tag, again, #got))
|
||||
end
|
||||
end
|
||||
|
||||
do -- v:release and decoder:release
|
||||
local c = H264Cases[2]
|
||||
local v = assert(F.video.open(c.data, { audio = false }))
|
||||
local a = assert(v:nextFrame())
|
||||
local ca = Crc32.of(a.image.pixels)
|
||||
v:release(a)
|
||||
local b = assert(v:nextFrame())
|
||||
t:check(b.image == a.image and Crc32.of(b.image.pixels) == c.frames[2] and ca == c.frames[1], "v:release: the next frame reuses the Image")
|
||||
local dec = assert(H264.newDecoder())
|
||||
t:check(not dec:release(F.Types.Image.new(176, 144, "yuv420p")), "H264 decoder:release refuses a foreign Image")
|
||||
local vd = Vp8.newDecoder()
|
||||
local f1 = ivfFrames(buffer.fromstring(Vp8Cases[1].ivf))
|
||||
local i1 = assert(vd:decodeFrame(f1[1]))
|
||||
local k1 = Crc32.of(i1.pixels)
|
||||
t:check(vd:release(i1), "Vp8 decoder:release")
|
||||
local i2 = assert(vd:decodeFrame(f1[2]))
|
||||
t:check(i2 == i1 and k1 == Vp8Cases[1].crcs[1] and Crc32.of(i2.pixels) == Vp8Cases[1].crcs[2], "Vp8: the released Image holds the next frame")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in H264Cases do
|
||||
table.insert(samples, buffer.fromstring(string.sub(c.data, 1, 4000)))
|
||||
end
|
||||
for _, f in ivfFrames(buffer.fromstring(Vp8Cases[1].ivf)) do
|
||||
if #samples < 12 then
|
||||
table.insert(samples, f)
|
||||
end
|
||||
end
|
||||
t:fuzz("Disposable.check", function(b: buffer)
|
||||
Disposable.check("h264", b, nil)
|
||||
Disposable.check("h264", b, buffer.fromstring("\1\100\0\31\255\225\0"))
|
||||
return Disposable.check("vp8", b)
|
||||
end, samples)
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,37 @@
|
||||
# Decoder control fixtures (tools/fixtures.py, for tests/video/control.luau): VP8 with two temporal layers (ffmpeg's
|
||||
# libvpx, ts_layering_mode 2: every other inter frame refreshes no reference), as IVF and WebM, with and without error
|
||||
# resilience; ffmpeg's vp8 decode gives the CRC-32 of every shown yuv420p frame.
|
||||
# tests/gen/videoctl_vp8.luau { { name, ivf, webm, width, height, crcs } }
|
||||
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"]
|
||||
|
||||
LAYERS = ["-ts-parameters", "ts_number_layers=2:ts_target_bitrate=150,300:ts_rate_decimator=2,1:ts_periodicity=2:"
|
||||
"ts_layer_id=0,1:ts_layering_mode=2"]
|
||||
|
||||
|
||||
def crcs(path):
|
||||
out = V.ffmpeg(["-c:v", "vp8", "-i", path, "-fps_mode", "passthrough", "-f", "framehash", "-hash", "crc32", "-"])
|
||||
res = []
|
||||
for line in out.decode().splitlines():
|
||||
if line and not line.startswith("#"):
|
||||
res.append(int(line.split(",")[5].strip(), 16))
|
||||
return res
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
tmp = tempfile.mkdtemp(prefix="videoctl_")
|
||||
out = []
|
||||
for name, extra in (("layers", []), ("layers_er", ["-error-resilient", "1"])):
|
||||
ivf = os.path.join(tmp, name + ".ivf")
|
||||
webm = os.path.join(tmp, name + ".webm")
|
||||
V.ffmpeg(["-f", "lavfi", "-i", "testsrc2=size=176x144:rate=25", "-frames:v", "20", "-pix_fmt", "yuv420p",
|
||||
"-c:v", "libvpx", "-b:v", "300k"] + extra + LAYERS + ["-f", "ivf", ivf])
|
||||
V.ffmpeg(["-i", ivf, "-c", "copy", webm])
|
||||
out.append({"name": name, "ivf": open(ivf, "rb").read(), "webm": open(webm, "rb").read(), "width": 176,
|
||||
"height": 144, "crcs": crcs(ivf)})
|
||||
ctx.emit("videoctl_vp8", out)
|
||||
Reference in new issue
Block a user