merge ws/video: containers mkv/avi/mp4/ts/ps/y4m (+writers), codecs Jpeg/Mpeg1/Vp8 inter/Video registry, AudioWrap, F.video.open

This commit is contained in:
4DBug committed 2026-10-31 10:22:17 -05:00
commit 2eaed46eea
96 files changed
+13398 -517

No files matched your search

+95 -3
View File
@@ -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,6 +259,7 @@ 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.
<<<<<<< HEAD
-- They also see the injected plug-ins as opts.plugins (F.plugins: the B-rep kernel for cad/step) unless given their own.
local function withHooks(o: Options): Options
if o.decodeImage ~= nil and o.encodeImage ~= nil and o.plugins ~= nil then
@@ -263,6 +268,33 @@ local function withHooks(o: Options): Options
local c = table.clone(o)
if c.plugins == nil then
c.plugins = F.plugins
=======
--
-- 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
local dec, oerr = F.open(buf, { format = fmt, category = "audio" })
-- Matroska CodecDelay: ffmpeg drops it for MPEG audio / AAC; Opus has its own pre-skip and Vorbis's delay is
-- already the decoder's own (its first packet returns nothing)
if dec and fmt ~= "opus" and fmt ~= "vorbis" and type(track.codecDelay) == "number" and track.codecDelay > 0 then
dec = AudioWrap.trim(dec, math.floor(track.codecDelay * dec.sampleRate + 0.5))
end
return dec, oerr
end
local function withHooks(o: Options): Options
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
>>>>>>> ws/video
end
if c.decodeImage == nil then
c.decodeImage = function(b: buffer): (any, Err.Err?)
@@ -309,12 +341,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
@@ -337,6 +369,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
@@ -344,10 +379,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 == "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
@@ -373,6 +418,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
@@ -427,6 +475,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
@@ -543,6 +598,7 @@ for category in Types.CATEGORIES do
end
F.data.null = Types.null
<<<<<<< HEAD
-- F.cad.toBodies(cad, opts?) -> bodies, result | nil, Err; F.cad.fromBodies(bodies, opts?) -> Cad | nil, Err;
-- F.cad.tessellate(cad | bodies, opts?) -> Scene | nil, Err: cad/step's B-rep calls with the injected kernel
-- (F.plugins.brep, unless opts.brep / opts.plugins is given). Without a plug-in they return Err.UNSUPPORTED.
@@ -567,6 +623,42 @@ for _, fn in { "toBodies", "fromBodies", "tessellate" } do
return a, b
end
end
=======
-- 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.fromFrames(images, delays?, opts?) -> a VideoDecoder over a list of same-size images (an image sequence:
-- F.save(F.video.fromFrames(list, 1 / 25), "out.avi")); delays in seconds (a number = every frame, default 0.1)
F.video.fromFrames = function(images: any, delays: any?, opts: Options?): (any, Err.Err?)
return Video.fromFrames(images, delays, opts)
end
-- F.video.codecs: the video codec registry (codecs/Video: register / entry / fromFourcc / fromMatroska / decoder)
F.video.codecs = Video
>>>>>>> ws/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