ws-h264: merge ws/video; codecs/Video registers h264 (avc1/avc3/H264/... FourCCs, V_MPEG4/ISO/AVC) via H264.newVideoDecoder; video/h264 raw Annex B format (demux by access unit, copy-writer from MP4/MKV samples); MMCO 5 resets frame_num (JVT MR2/MR4/MR5); meta.yuv from the VUI

This commit is contained in:
4DBug committed 2026-10-31 10:29:56 -05:00
commit d4486dccce
211 files changed
+43039 -552

No files matched your search

+387 -7
View File
@@ -90,6 +90,16 @@ for _, c in { { "celt 128k", OpusCelt }, { "silk wb", OpusSilk }, { "hybrid", Op
Opus.decode(Bytes.reader(b))
end)
end
-- vorbis (ws-audio): 10 s of 44.1 kHz stereo music at oggenc -q 5 (tools/fixtures/vorbis.py)
do
local Vorbis = require("@formats/audio/vorbis")
local VB = require("@tests/gen/vorbis_bench")
local b = buffer.fromstring(VB.data)
local dt = bench("vorbis decode q5 (10 s st)", 3, VB.frames * VB.channels * 4, function()
Vorbis.decode(Bytes.reader(b))
end)
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", VB.frames / VB.rate / dt))
end
for _, name in { "s16_long", "s24_96k" } do
for _, c in FC do
if c.name == name then
@@ -116,6 +126,55 @@ do
Wav.decode(Bytes.reader(file))
end)
end
-- flac encode (ws-audio): 10 s of 44.1 kHz stereo 16-bit (a synthetic two-tone signal with noise)
do
local Fl = require("@formats/audio/flac")
local n = 441000
local a = Types.Audio.new(44100, 2, n)
local seed = 1
for i = 0, n - 1 do
seed = (seed * 1103515245 + 12345) % 2147483648
local noise = (seed / 2147483648 - 0.5) * 0.01
buffer.writef32(a.samples, i * 8, math.floor((math.sin(i * 0.031) * 0.4 + math.sin(i * 0.21) * 0.1 + noise) * 32768) / 32768)
buffer.writef32(a.samples, i * 8 + 4, math.floor((math.sin(i * 0.017) * 0.3 + noise) * 32768) / 32768)
end
for _, level in { 0, 5, 8 } do
local dt = bench("flac encode -" .. level .. " (10 s st 16)", 2, n * 4, function()
Fl.encode(a, { level = level })
end)
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", 10 / dt))
end
end
-- mp3 / mp2 encode (ws-audio): 10 s of 44.1 kHz stereo tones
do
local M3 = require("@formats/audio/mp3")
local n = 441000
local a = Types.Audio.new(44100, 2, n)
for i = 0, n - 1 do
buffer.writef32(a.samples, i * 8, math.sin(i * 0.031) * 0.4 + math.sin(i * 0.21) * 0.1)
buffer.writef32(a.samples, i * 8 + 4, math.sin(i * 0.017) * 0.3)
end
for _, cfg in { { layer = 3, bitrate = 128 }, { layer = 2, bitrate = 192 } } do
local dt = bench(string.format("mp%d encode %d kbps (10 s st)", cfg.layer, cfg.bitrate), 2, n * 8, function()
M3.encode(a, cfg)
end)
print(string.format("%-8s %-30s %8.1fx real time", runtime, "", 10 / dt))
end
end
-- vorbis encode (ws-audio): 10 s of 44.1 kHz stereo tones at quality 3
do
local Vb = require("@formats/audio/vorbis")
local n = 441000
local a = Types.Audio.new(44100, 2, n)
for i = 0, n - 1 do
buffer.writef32(a.samples, i * 8, math.sin(i * 0.031) * 0.4 + math.sin(i * 0.21) * 0.1)
buffer.writef32(a.samples, i * 8 + 4, math.sin(i * 0.017) * 0.3)
end
local dt = bench("vorbis encode q3 (10 s st)", 2, n * 8, function()
Vb.encode(a, { quality = 3 })
end)
print(string.format("%-8s %-30s %8.1fx real time", runtime, "", 10 / dt))
end
do
local Json = require("@formats/codecs/Json")
local JB = require("@tests/gen/json_bench")
@@ -536,14 +595,335 @@ do
end)
end
do
-- H.264: frames per second over whole streams (decode + output copies); MB/s of yuv420p output
local Xml = require("@formats/codecs/Xml")
local XB = require("@tests/gen/xml_bench")
local doc = assert(Xml.decode(XB.text))
bench("xml decode (DOM)", 5, #XB.text, function()
Xml.decode(XB.text)
end)
bench("xml events", 5, #XB.text, function()
for _ in Xml.events(XB.text) do
end
end)
bench("xml encode", 5, #XB.text, function()
Xml.encode(doc)
end)
end
-- ws-m4a: AAC-LC / ALAC decode and ALAC encode (MB/s of 16-bit PCM equivalent: frames * channels * 2), MDCT
do
local M4a = require("@formats/audio/m4a")
local Alac = require("@formats/codecs/Alac")
local Mdct = require("@formats/codecs/Mdct")
local MA = require("@tests/gen/m4a_aac")
local ML = require("@tests/gen/m4a_alac")
local aac = buffer.fromstring(MA[1].data)
local a = assert(M4a.decode(Bytes.reader(aac)))
bench("aac-lc decode (m4a, stereo)", 3, a.frames * a.channels * 2, function()
M4a.decode(Bytes.reader(aac))
end)
local alac = buffer.fromstring(ML[1].data)
local l = assert(M4a.decode(Bytes.reader(alac)))
bench("alac decode (m4a, stereo 16)", 3, l.frames * l.channels * 2, function()
M4a.decode(Bytes.reader(alac))
end)
local enc = assert(Alac.encoder({ sampleRate = 44100, channels = 2, bitDepth = 16 }))
local ints = table.create(a.frames * 2, 0)
for i = 1, a.frames * 2 do
ints[i] = math.floor(buffer.readf32(a.samples, (i - 1) * 4) * 32767)
end
bench("alac encode (stereo 16)", 2, a.frames * 2 * 2, function()
local f = 0
while f < a.frames do
local n = math.min(4096, a.frames - f)
enc:encode(ints, f * 2 + 1, n)
f += n
end
end)
local plan = assert(Mdct.new(2048))
local x, X = table.create(2048, 0.5), table.create(1024, 0)
bench("mdct 2048 x 1000 (forward)", 3, 1000 * 2048 * 4, function()
for _ = 1, 1000 do
Mdct.forward(plan, x, 1, X, 1)
end
end)
end
do
local Opus = require("@formats/audio/opus")
local Cases = require("@tests/audio/_opus_cases")
local c = Cases.list[2]
local a = Cases.signal(c)
local dt = bench("opus celt encode 128k (1.5 s st)", 2, a.frames * a.channels * 2, function()
Opus.encode(a, { bitrate = 128000 })
end)
local _ = dt
end
-- MIDI through the SoundFont synth (tools/fixtures/sf2.py's test song; tools/fixtures/midi.py's "song" through the
-- built-in waveform set): seconds of stereo 44.1 kHz audio rendered per second of CPU
do
local Midi = require("@formats/music/midi")
local Sf2 = require("@formats/music/sf2")
local SF = require("@tests/gen/sf2_cases")
local MC = require("@tests/gen/midi_cases")
local sf = assert(Sf2.decode(Bytes.reader(buffer.fromstring(SF.sf2))))
local function realtime(label: string, data: string, font: any)
local song = assert(Midi.decode(Bytes.reader(buffer.fromstring(data))))
local best = math.huge
local frames = 0
for _ = 1, 3 do
local dec = assert(Midi.render(song, { soundfont = font, tail = 0 }))
local out = buffer.create(4096 * 8)
local t0 = os.clock()
frames = 0
while true do
local _, n = dec:read(4096, out)
if n == 0 then
break
end
frames += n
end
best = math.min(best, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f x realtime", runtime, label, best * 1000, frames / 44100 / best))
end
realtime("midi render sf2 (test song)", SF.song, sf)
for _, c in MC do
if c.name == "song" then
realtime("midi render fallback", c.data, nil)
end
end
end
-- the synth's per-voice cost: 32 sustained, filtered, looped voices (sf2 saw preset with a mod env) for 2 s
do
local SoundFont = require("@formats/codecs/SoundFont")
local Sf2 = require("@formats/music/sf2")
local SF = require("@tests/gen/sf2_cases")
local sf = assert(Sf2.decode(Bytes.reader(buffer.fromstring(SF.sf2))))
local frames = 88200
local out = buffer.create(frames * 8)
local best = math.huge
for _ = 1, 3 do
local s = SoundFont.synth(sf, 44100)
SoundFont.Synth.midi(s, 0xC0, 1, 0)
for k = 0, 31 do
SoundFont.Synth.midi(s, 0x90, 40 + k, 100)
end
buffer.fill(out, 0, 0)
local t0 = os.clock()
for f = 0, frames - 1, 1024 do
SoundFont.Synth.render(s, out, f, 1024)
end
best = math.min(best, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f M voice-samples/s", runtime, "synth 32 voices x 2 s", best * 1000, 32 * frames / best / 1e6))
end
-- the tracker engine: 32 channels of looped samples (one note each, linear interpolation, volume ramps), and an
-- IT-style module with background voices (NNA continue) and resonant filters on every channel
do
local Tracker = require("@formats/codecs/Tracker")
local function song(format: string, filter: boolean): any
local m = Tracker.newModule(format, 32)
m.order = { 0 }
m.patterns[1] = Tracker.newPattern(64, 32)
local s = Tracker.newSample("saw")
s.length = 1000
s.data = buffer.create(4000)
for i = 0, 999 do
buffer.writef32(s.data, i * 4, (i % 100) / 50 - 1)
end
s.loopStart, s.loopEnd, s.loopType = 0, 1000, 1
m.samples[1] = s
if format == "it" then
local ins = Tracker.newInstrument("nna")
for n = 1, 120 do
ins.sampleMap[n] = 1
end
ins.nna = 1
ins.fadeout = 64
if filter then
ins.cutoff, ins.resonance = 60, 40
end
m.instruments[1] = ins
end
for ch = 1, 32 do
for row = 0, 63, 16 do
Tracker.setCell(m, 0, row, ch, 37 + ch + row // 16, 1, 0, 0, 0)
end
end
return m
end
local function realtime(label: string, m: any, voices: number)
local best, frames = math.huge, 0
local out = buffer.create(4096 * 8)
for _ = 1, 3 do
local dec = Tracker.render(m) :: any
frames = 0
local t0 = os.clock()
while true do
local _, n = dec:read(4096, out)
if n == 0 then
break
end
frames += n
end
best = math.min(best, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f x realtime %5.1f M voice-samples/s", runtime, label, best * 1000, frames / 44100 / best, voices * frames / best / 1e6))
end
realtime("tracker 32 ch (mod)", song("mod", false), 32)
realtime("tracker 32 ch + NNA, filters", song("it", true), 64)
end
-- ------------------------------------------------------------------------------------------- ws-video-codecs: fps
do
local Jpeg = require("@formats/codecs/Jpeg")
local Mpeg1 = require("@formats/codecs/Mpeg1")
local Base64 = require("@formats/core/Base64")
local Types = require("@formats/Types")
local MB = require("@tests/gen/mjpeg_bench")
local M1 = require("@tests/gen/mpeg1_bench")
-- best of n runs of f (which handles `frames` frames): ms per frame and frames per second
local function fps(label: string, n: number, frames: number, f: () -> ())
f()
local dt = math.huge
for _ = 1, n do
local t0 = os.clock()
f()
dt = math.min(dt, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, label, dt * 1000 / frames, frames / dt))
end
for _, c in MB.frames do
local b = Base64.decode(c.data) :: buffer
local img = assert(Jpeg.decode(b, 0, nil, { yuv = true }))
assert(Crc32.of(img.pixels) == c.yuv, "mjpeg bench: " .. c.name .. " does not match TurboJPEG")
fps("mjpeg decode yuv " .. c.name, 10, 1, function()
Jpeg.decode(b, 0, nil, { yuv = true })
end)
fps("mjpeg decode rgba " .. c.name, 10, 1, function()
Jpeg.decode(b)
end)
end
for _, s in MB.sources do
local img = Types.Image.new(s.width, s.height, "yuv420p")
img.pixels = assert(Deflate.zlib(Base64.decode(s.yuv) :: buffer))
fps(string.format("mjpeg encode yuv %dx%d q85", s.width, s.height), 5, 1, function()
Jpeg.encode(img, { quality = 85 })
end)
end
for _, s in M1 do
local file = Base64.decode(s.file) :: buffer
for _, rgba in { false, true } do
fps(string.format("mpeg1 decode %s %dx%d", if rgba then "rgba" else "yuv", s.width, s.height), 3, #s.units, function()
local dec = Mpeg1.new({ reorder = true, rgba = rgba })
for i, u in s.units do
dec:decode(file, u[1], u[2], i)
end
while dec:flush() do
end
end)
end
end
end
-- ------------------------------------------------------------------------------------------- ws-video: containers
do
local F = require("@formats")
local VB = require("@tests/gen/videobench")
local function clock(n: number, f: () -> number): (number, number)
local frames = f()
local dt = math.huge
for _ = 1, n do
local t0 = os.clock()
frames = f()
dt = math.min(dt, os.clock() - t0)
end
return dt, frames
end
for _, c in VB do
local data = buffer.fromstring(c.data)
local dtOpen = clock(3, function(): number
F.video.open(data, { audio = false })
return 1
end)
for _, rgba in { false, true } do
local ok, dt, frames = pcall(clock, 2, function(): number
local v = assert(F.video.open(data, { audio = false, rgba = rgba }))
local n = 0
while true do
local f, err = v:nextFrame()
if not f then
assert(err == nil, tostring(err))
break
end
n += 1
end
return n
end)
if ok then
print(string.format("%-8s %-30s %8.2f ms %7.1f fps (open %.1f ms)", runtime, string.format("video %s %s %s", c.size, c.name, if rgba then "rgba" else "yuv"), dt * 1000 / frames, frames / dt, dtOpen * 1000))
else
print(string.format("%-8s %-30s %s", runtime, "video " .. c.size .. " " .. c.name, tostring(dt)))
end
end
if c.name == "raw.y4m" then
local dt, frames = clock(2, function(): number
assert(F.convert(data, "avi", { quality = 85 }))
return 8
end)
print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, "video " .. c.size .. " y4m -> mjpeg avi", dt * 1000 / frames, frames / dt))
end
end
end
-- ------------------------------------------------------------------------------------------- ws-video-vp8: fps
do
-- VP8 video (codecs/Vp8, tools/fixtures/vp8video.py): whole IVF streams (libvpx, panning testsrc2) through one
-- decoder object; frames per second. On LuaJIT the library's host module raises the trace limits (maxtrace=8000,
-- see src/Formats/host/init.luau): the second pair of lines runs with them, as the api does.
local Vp8 = require("@formats/codecs/Vp8")
local VB = require("@tests/gen/vp8video_bench")
local function run(tag: string)
for _, c in VB do
local b = buffer.fromstring(c.ivf)
local frames = {}
local p = 32
while p + 12 <= buffer.len(b) do
local n = buffer.readu32(b, p)
table.insert(frames, { p + 12, n })
p += 12 + n
end
local dt = bench(string.format("vp8 decode %dx%d%s", c.width, c.height, tag), 3, c.width * c.height * 1.5 * #frames, function()
local dec = Vp8.newDecoder()
for _, f in frames do
dec:decodeFrame(b, f[1], f[2])
end
end)
print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. #frames .. " frames)", #frames / dt))
end
end
run("")
if runtime == "luajit" then
require("@formats/host")
run(" (jit tuned)")
end
end
do
-- H.264 (codecs/H264): whole Annex B streams, frames per second (decode + the cropped yuv420p output copies). The
-- testsrc2 clips are easy (mostly static); the mandelbrot zooms move everywhere. LuaJIT runs twice: with its default
-- trace limits and with the ones host/init.luau sets (maxtrace=8000, maxmcode=65536).
local H264 = require("@formats/codecs/H264")
local HB = require("@tests/gen/h264_bench")
for _, c in HB do
local n = c.width * c.height * 3 // 2 * #c.frames
local dt = bench("h264 " .. c.name .. " " .. #c.frames .. "f", 2, n, function()
H264.decodeAll(c.data)
end)
print(string.format("%-8s h264 %s: %.1f fps at %dx%d", runtime, c.name, #c.frames / dt, c.width, c.height))
local function run(tag: string)
for _, c in HB do
local n = c.width * c.height * 3 // 2 * #c.frames
local dt = bench(string.format("h264 %s %dx%d%s", c.name, c.width, c.height, tag), 3, n, function()
H264.decodeAll(c.data)
end)
print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. #c.frames .. " frames)", #c.frames / dt))
end
end
run("")
if runtime == "luajit" then
require("@formats/host")
run(" (jit tuned)")
end
end