ws-video: writers checked by ffprobe / ffmpeg (tools/fixtures/videoenc.py + _video_encode.luau, tests/video/encode.luau); constant-rate output for AVI / Y4M shared in Source.constantRate (uneven GIF delays repeat frames)
This commit is contained in:
1 parent
bd06545e2e
commit
8d2deb13ab
6 files changed
+248
-59
No files matched your search
@@ -232,6 +232,51 @@ function Source.streams(value: any, opts: any?, want: Want): ({ Stream }?, Err.E
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- rational(fps) -> num, den: integer rates as n / 1, NTSC rates as n * 1000 / 1001, else to a millisecond
|
||||
function Source.rational(fps: number): (number, number)
|
||||
if fps ~= fps or fps <= 0 then
|
||||
return 25, 1
|
||||
end
|
||||
if math.abs(fps - math.floor(fps + 0.5)) < 1e-6 then
|
||||
return math.floor(fps + 0.5), 1
|
||||
end
|
||||
local ntsc = fps * 1001 / 1000
|
||||
if math.abs(ntsc - math.floor(ntsc + 0.5)) < 1e-4 then
|
||||
return math.floor(ntsc + 0.5) * 1000, 1001
|
||||
end
|
||||
return math.floor(fps * 1000 + 0.5), 1000
|
||||
end
|
||||
|
||||
-- constantRate(stream, maxFps?) -> num, den, { OutPacket }: a video stream at a constant frame rate for the
|
||||
-- containers that have one (AVI, Y4M): the rate of the shortest frame (at most maxFps, default 100), each packet
|
||||
-- repeated to fill its duration (a GIF's 100 / 50 / 200 ms frames become 2, 1 and 4 frames at 20 fps)
|
||||
function Source.constantRate(s: Stream, maxFps: number?): (number, number, { OutPacket })
|
||||
local fd = math.huge
|
||||
for _, p in s.packets do
|
||||
if p.duration and p.duration > 0 and p.duration < fd then
|
||||
fd = p.duration
|
||||
end
|
||||
end
|
||||
if fd == math.huge then
|
||||
fd = if s.frameRate and s.frameRate > 0 then 1 / s.frameRate else 0.04
|
||||
end
|
||||
fd = math.max(fd, 1 / (maxFps or 100))
|
||||
local num, den = Source.rational(1 / fd)
|
||||
local step = den / num
|
||||
local out: { OutPacket } = {}
|
||||
local slots = 0
|
||||
local t0 = if s.packets[1] then s.packets[1].pts else 0
|
||||
for _, p in s.packets do
|
||||
local stop = p.pts - t0 + (if p.duration and p.duration > 0 then p.duration else step)
|
||||
local n = math.max(1, math.floor(stop / step + 0.5) - slots)
|
||||
for _ = 1, n do
|
||||
table.insert(out, { pts = t0 + slots * step, duration = step, keyframe = p.keyframe, data = p.data })
|
||||
slots += 1
|
||||
end
|
||||
end
|
||||
return num, den, out
|
||||
end
|
||||
|
||||
-- interleave(streams) -> { { stream index, packet } } merged by decode time (dts, else pts); each stream keeps its
|
||||
-- own order
|
||||
function Source.interleave(streams: { Stream }): { { any } }
|
||||
|
||||
@@ -223,17 +223,6 @@ function AviFormat.demux(r: Bytes.Reader, _opts: OpenOptions?): (any, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function rational(fps: number): (number, number)
|
||||
if math.abs(fps - math.floor(fps + 0.5)) < 1e-6 then
|
||||
return math.floor(fps + 0.5), 1
|
||||
end
|
||||
local ntsc = fps * 1001 / 1000
|
||||
if math.abs(ntsc - math.floor(ntsc + 0.5)) < 1e-4 then
|
||||
return math.floor(ntsc + 0.5) * 1000, 1001
|
||||
end
|
||||
return math.floor(fps * 1000 + 0.5), 1000
|
||||
end
|
||||
|
||||
-- encode(video, opts?) -> buffer | nil, Err
|
||||
function AviFormat.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local o: any = table.clone(opts or {})
|
||||
@@ -251,18 +240,8 @@ function AviFormat.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
for _, s in streams do
|
||||
if s.kind == "video" then
|
||||
-- a constant frame rate: the shortest frame duration (variable delays repeat frames)
|
||||
local fd = math.huge
|
||||
for _, p in s.packets do
|
||||
if p.duration and p.duration > 0 and p.duration < fd then
|
||||
fd = p.duration
|
||||
end
|
||||
end
|
||||
local maxFps = o.maxFps or 100
|
||||
if fd == math.huge then
|
||||
fd = if s.frameRate and s.frameRate > 0 then 1 / s.frameRate else 0.04
|
||||
end
|
||||
fd = math.max(fd, 1 / maxFps)
|
||||
local num, den = rational(1 / fd)
|
||||
local num, den, packets = Source.constantRate(s, o.maxFps)
|
||||
s.packets = packets
|
||||
frameDur = den / num
|
||||
width, height = s.width or 0, s.height or 0
|
||||
usPerFrame = 1e6 * frameDur
|
||||
@@ -295,23 +274,10 @@ function AviFormat.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
table.insert(wstreams, { type = "auds", handler = "\0\0\0\0", scale = align, rate = rate * align, sampleSize = align, strf = wf:finish(), ckid = "wb" })
|
||||
end
|
||||
end
|
||||
-- chunks: video packets repeated to fill their duration at the constant rate
|
||||
local order = Source.interleave(streams)
|
||||
-- chunks in time order (the video packets already repeated to the constant rate)
|
||||
local chunks: { { any } } = {}
|
||||
local vtime = 0 -- frames written so far (video slots)
|
||||
for _, item in order do
|
||||
local si, p = item[1], item[2]
|
||||
local s = streams[si]
|
||||
if s.kind == "video" then
|
||||
local stop = p.pts + (if p.duration and p.duration > 0 then p.duration else frameDur)
|
||||
local slots = math.max(1, math.floor(stop / frameDur + 0.5) - vtime)
|
||||
for _ = 1, slots do
|
||||
table.insert(chunks, { si, p.data, true })
|
||||
end
|
||||
vtime += slots
|
||||
else
|
||||
table.insert(chunks, { si, p.data, true })
|
||||
end
|
||||
for _, item in Source.interleave(streams) do
|
||||
table.insert(chunks, { item[1], item[2].data, true })
|
||||
end
|
||||
local ok, buf = pcall(Avi.write, { streams = wstreams, microSecPerFrame = usPerFrame, width = width, height = height }, chunks)
|
||||
if not ok then
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
-- with yuv420p frames (C420 / C420jpeg / C420mpeg2 / C420paldv as they are; C422 / C444 chroma averaged down; Cmono
|
||||
-- with neutral chroma; 9..16-bit samples (C420p10, ...) scaled to 8 bits). Y4m.demux(r) gives the Demuxer.
|
||||
-- Write: yuv420p 8-bit frames ("C420jpeg", as ffmpeg writes for yuv420p) from any video or animated image; rgba8
|
||||
-- frames are converted (BT.601 limited range). Not supported: interlaced field handling (the I tag is kept in meta),
|
||||
-- frames are converted (BT.601 limited range); uneven frame times (a GIF's) become a constant rate with repeated frames. Not supported: interlaced field handling (the I tag is kept in meta),
|
||||
-- alpha (C444alpha drops it).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
@@ -31,7 +31,9 @@ export type OpenOptions = {
|
||||
maxPixels: number?, -- default 2^26
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
frameRate: { number }?, -- { num, den } for the F tag (default: from the source's frame rate)
|
||||
frameRate: { number }?, -- { num, den } for the F tag, one frame per source frame (default: the constant rate of the
|
||||
-- shortest frame, longer frames repeated)
|
||||
maxFps: number?, -- the highest rate uneven frame times may need (default 100)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -173,20 +175,6 @@ function Y4m.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function rational(fps: number): (number, number)
|
||||
if fps <= 0 or fps ~= fps then
|
||||
return 25, 1
|
||||
end
|
||||
if math.abs(fps - math.floor(fps + 0.5)) < 1e-6 then
|
||||
return math.floor(fps + 0.5), 1
|
||||
end
|
||||
local ntsc = fps * 1001 / 1000
|
||||
if math.abs(ntsc - math.floor(ntsc + 0.5)) < 1e-4 then
|
||||
return math.floor(ntsc + 0.5) * 1000, 1001
|
||||
end
|
||||
return math.floor(fps * 1000 + 0.5), 1000
|
||||
end
|
||||
|
||||
-- encode(video, opts?) -> buffer | nil, Err
|
||||
function Y4m.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local streams, err = Source.streams(value, { audio = false }, { video = { copy = nil, encode = "rawvideo" } })
|
||||
@@ -194,12 +182,11 @@ function Y4m.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
return nil, err
|
||||
end
|
||||
local s = streams[1]
|
||||
local fn, fd
|
||||
local fn, fd, packets = Source.constantRate(s, opts and opts.maxFps)
|
||||
if opts and type(opts.frameRate) == "table" then
|
||||
fn, fd = opts.frameRate[1], opts.frameRate[2]
|
||||
else
|
||||
fn, fd = rational(s.frameRate or 25)
|
||||
fn, fd, packets = opts.frameRate[1], opts.frameRate[2], s.packets
|
||||
end
|
||||
s.packets = packets
|
||||
local w = Bytes.writer(64 + #s.packets * (buffer.len(s.packets[1].data) + 6))
|
||||
w:string(string.format("YUV4MPEG2 W%d H%d F%d:%d Ip A1:1 C420jpeg XYSCSS=420JPEG\n", s.width or 0, s.height or 0, fn, fd))
|
||||
for _, p in s.packets do
|
||||
|
||||
Reference in new issue
Block a user