ws-video: video foundation: Types video section made concrete (Track / Packet / Demuxer / VideoFrame / VideoDecoder / VideoCodecDecoder), codecs/Video (codec registry by id / FourCC / Matroska id, generic VideoDecoder over a Demuxer, frames-backed videos, Source for writers), codecs/Yuv, containers/Demux (indexed demuxer), containers/Ebml, containers/AudioWrap; video/mkv (Matroska / WebM demux + mux, WIP: tests next)
api (init.luau, small, backward compatible): KIND_CATEGORY.video; withHooks adds opts.openAudio (audio tracks of video containers reach the audio formats through AudioWrap); F.load of a video format returns its VideoDecoder instead of draining it as audio; F.open passes the hooks and opens animated images as frames-backed videos (F.video.open(gif)); F.save of an Image to a video format wraps it (F.convert(gif, 'avi')); F.video.fromImage, F.video.codecs.
This commit is contained in:
1 parent
75c677074a
commit
8a905a4510
13 files changed
+3099
-19
No files matched your search
+76
-4
@@ -19,6 +19,9 @@ local Bytes = require("@formats/core/Bytes")
|
||||
local Input = require("@formats/core/Input")
|
||||
local Types = require("@formats/Types")
|
||||
local Registry = require("@formats/Registry")
|
||||
local Video = require("@formats/codecs/Video")
|
||||
local AudioWrap = require("@formats/containers/AudioWrap")
|
||||
local ImageOps = require("@formats/ops/ImageOps")
|
||||
local host = require("@formats/host")
|
||||
|
||||
local F = {}
|
||||
@@ -35,6 +38,7 @@ F.plugins = {} :: { [string]: any } -- injected optional dependencies (plugins.b
|
||||
local KIND_CATEGORY: { [string]: string } = {
|
||||
image = "image",
|
||||
audio = "audio",
|
||||
video = "video",
|
||||
scene = "model",
|
||||
cad = "cad",
|
||||
font = "font",
|
||||
@@ -255,11 +259,26 @@ end
|
||||
-- so the api hands decoders opts.decodeImage(buf) -> Image | nil, Err and encoders opts.encodeImage(img, format) ->
|
||||
-- buffer | nil, Err (unless the caller passed its own). Formats work without them (returning Err.UNSUPPORTED for
|
||||
-- the embedded parts) when called directly.
|
||||
--
|
||||
-- Audio tracks of video containers: opts.openAudio(demuxer, track) -> audio Decoder | nil, Err. The track's packets
|
||||
-- become a stream an audio format reads (containers/AudioWrap: PCM -> WAV, Opus / Vorbis -> Ogg, AAC -> ADTS, MP3 /
|
||||
-- FLAC as they are), opened through F.open with that format.
|
||||
local function openAudio(demuxer: any, track: any): (any, Err.Err?)
|
||||
local buf, fmt, err = AudioWrap.wrap(demuxer, track)
|
||||
if not buf then
|
||||
return nil, err
|
||||
end
|
||||
return F.open(buf, { format = fmt, category = "audio" })
|
||||
end
|
||||
|
||||
local function withHooks(o: Options): Options
|
||||
if o.decodeImage ~= nil and o.encodeImage ~= nil then
|
||||
if o.decodeImage ~= nil and o.encodeImage ~= nil and o.openAudio ~= nil then
|
||||
return o
|
||||
end
|
||||
local c = table.clone(o)
|
||||
if c.openAudio == nil then
|
||||
c.openAudio = openAudio
|
||||
end
|
||||
if c.decodeImage == nil then
|
||||
c.decodeImage = function(b: buffer): (any, Err.Err?)
|
||||
return F.load(b, { category = "image" })
|
||||
@@ -305,12 +324,12 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
end
|
||||
value, err = v, e
|
||||
elseif m.open then
|
||||
local ok, dec, e = pcall(m.open, Bytes.reader(buf), o)
|
||||
local ok, dec, e = pcall(m.open, Bytes.reader(buf), withHooks(o))
|
||||
if not ok then
|
||||
return nil, Err.wrap(Err.new(Err.CORRUPT, "decoder threw: " .. tostring(dec)), entry.name)
|
||||
end
|
||||
if dec then
|
||||
value = drain(dec)
|
||||
value = if type(dec) == "table" and dec.kind == "video" then dec else drain(dec)
|
||||
end
|
||||
err = e
|
||||
else
|
||||
@@ -333,6 +352,9 @@ function F.open(src: any, opts: Options?): (any, Err.Err?)
|
||||
return nil, ierr
|
||||
end
|
||||
local entry, derr = detect(buf, name, o.category, o.format)
|
||||
if not entry and o.category == "video" and o.format == nil then
|
||||
entry = detect(buf, name, "image", nil) -- animated images open as frames-backed videos
|
||||
end
|
||||
if not entry then
|
||||
return nil, derr
|
||||
end
|
||||
@@ -340,10 +362,20 @@ function F.open(src: any, opts: Options?): (any, Err.Err?)
|
||||
if not m then
|
||||
return nil, merr
|
||||
end
|
||||
if not m.open and entry.category == "image" and (o.category == nil or o.category == "video") then
|
||||
local io = table.clone(o)
|
||||
io.category = "image"
|
||||
io.format = entry.name
|
||||
local img, lerr = F.load(buf, io)
|
||||
if not img then
|
||||
return nil, lerr
|
||||
end
|
||||
return F.video.fromImage(img, o)
|
||||
end
|
||||
if not m.open then
|
||||
return nil, Err.wrap(Err.new(Err.UNSUPPORTED, "no streaming decoder (use load)"), entry.name)
|
||||
end
|
||||
local ok, dec, e = pcall(m.open, Bytes.reader(buf), o)
|
||||
local ok, dec, e = pcall(m.open, Bytes.reader(buf), withHooks(o))
|
||||
if not ok then
|
||||
return nil, Err.wrap(Err.new(Err.CORRUPT, "decoder threw: " .. tostring(dec)), entry.name)
|
||||
end
|
||||
@@ -369,6 +401,9 @@ local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entr
|
||||
return nil, nil, Err.new(Err.ARG, "save: no format (pass opts.format or a destination with an extension)")
|
||||
end
|
||||
local entry = find(key, category)
|
||||
if not entry and category == "image" and type(value) == "table" and value.kind == "image" then
|
||||
entry = find(key, "video") -- an (animated) image written as a video: F.convert(gif, "avi")
|
||||
end
|
||||
if not entry then
|
||||
return nil, nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. key .. (if category then " for " .. category else ""))
|
||||
end
|
||||
@@ -423,6 +458,13 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
|
||||
o.files = {}
|
||||
end
|
||||
end
|
||||
if entry.category == "video" and type(value) == "table" and value.kind == "image" then
|
||||
local v, verr = F.video.fromImage(value)
|
||||
if not v then
|
||||
return nil, verr
|
||||
end
|
||||
value = v
|
||||
end
|
||||
if type(value) == "table" and value.kind == "scene" then
|
||||
local terr = encodeTextures(value)
|
||||
if terr then
|
||||
@@ -539,6 +581,36 @@ for category in Types.CATEGORIES do
|
||||
end
|
||||
F.data.null = Types.null
|
||||
|
||||
-- F.video.fromImage(img, opts?) -> a frames-backed VideoDecoder: an animated GIF / APNG / WebP (img.frames, composited
|
||||
-- onto full canvases by ImageOps.coalesce) or a still image (one frame). opts.rgba is accepted (frames are rgba8).
|
||||
F.video.fromImage = function(img: any, opts: Options?): (any, Err.Err?)
|
||||
if type(img) ~= "table" or img.kind ~= "image" then
|
||||
return nil, Err.new(Err.ARG, "fromImage: expected an Image")
|
||||
end
|
||||
local frames, delays = { img }, { 0.1 }
|
||||
if type(img.frames) == "table" and #img.frames > 0 then
|
||||
local list, cerr = ImageOps.coalesce(img)
|
||||
if not list then
|
||||
return nil, cerr
|
||||
end
|
||||
frames = list
|
||||
delays = {}
|
||||
for i, f in img.frames do
|
||||
delays[i] = if type(f.delay) == "number" and f.delay > 0 then f.delay else 0.1
|
||||
end
|
||||
elseif img.format ~= "rgba8" and img.format ~= "yuv420p" then
|
||||
local c, cerr = ImageOps.convert(img, "rgba8")
|
||||
if not c then
|
||||
return nil, cerr
|
||||
end
|
||||
frames = { c }
|
||||
end
|
||||
local src = type(img.meta) == "table" and img.meta.source
|
||||
return Video.fromFrames(frames, delays, { format = type(src) == "table" and src.format or "image", loops = img.loops, meta = img.meta })
|
||||
end
|
||||
-- F.video.codecs: the video codec registry (codecs/Video: register / entry / fromFourcc / fromMatroska / decoder)
|
||||
F.video.codecs = Video
|
||||
|
||||
-- F.model.loadTextures(scene, opts?) -> scene, { [index]: Err }: decodes each texture's encoded bytes (texture.data, or
|
||||
-- texture.uri through opts.resolve) into texture.image with F.image.load. Model formats never decode images
|
||||
-- themselves (a format does not require another format); this is the lazy step at the api layer. Textures whose
|
||||
|
||||
Reference in new issue
Block a user