codecs: H.264 / Yuv / video registry work, docs/API + guide/video, yuv + mp4 tests (uncommitted work found in the checkout, owner unknown; tests pass on both runtimes)

This commit is contained in:
4DBug committed 2026-11-01 01:16:04 -05:00
1 parent 05eedbc8cf
commit 2956576d7f
12 files changed
+252 -3

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
-- codecs/image/Yuv tests: scale (yuv420p planes resized bilinear: flat planes stay flat, a horizontal ramp stays a
-- monotonic ramp, output sizes with odd dimensions, identity size, short input), imageScale, and a toRgba round trip.
local T = require("@tests/T")
local Yuv = require("@formats/codecs/image/Yuv")
local Types = require("@formats/Types")
local t = T.suite("yuv")
local function planes(w: number, h: number): (number, number)
local cw, ch = (w + 1) // 2, (h + 1) // 2
return w * h, cw * ch
end
-- a w x h yuv420p picture: Y = horizontal ramp, U = 90, V = 200
local function picture(w: number, h: number): buffer
local ny, nc = planes(w, h)
local b = buffer.create(ny + 2 * nc)
for y = 0, h - 1 do
for x = 0, w - 1 do
buffer.writeu8(b, y * w + x, math.floor(x * 255 / math.max(w - 1, 1)))
end
end
buffer.fill(b, ny, 90, nc)
buffer.fill(b, ny + nc, 200, nc)
return b
end
for _, c in { { 64, 48, 32, 24 }, { 800, 600, 480, 360 }, { 33, 17, 13, 9 }, { 20, 20, 20, 20 } } do
local w, h, dw, dh = c[1], c[2], c[3], c[4]
local out = Yuv.scale(picture(w, h), w, h, dw, dh)
local ny, nc = planes(dw, dh)
local label = ("%dx%d -> %dx%d"):format(w, h, dw, dh)
t:eq(buffer.len(out), ny + 2 * nc, label .. ": size")
local mono, flat = true, true
for y = 0, dh - 1 do
local prev = -1
for x = 0, dw - 1 do
local v = buffer.readu8(out, y * dw + x)
mono = mono and v >= prev
prev = v
end
end
for i = 0, nc - 1 do
flat = flat and buffer.readu8(out, ny + i) == 90 and buffer.readu8(out, ny + nc + i) == 200
end
t:check(mono, label .. ": the ramp stays monotonic")
t:check(flat, label .. ": flat chroma stays flat")
t:check(buffer.readu8(out, 0) <= 16 and buffer.readu8(out, dw - 1) >= 239, label .. ": ramp ends")
end
-- the same size copies
local src = picture(20, 20)
t:eq(buffer.tostring(Yuv.scale(src, 20, 20, 20, 20)), buffer.tostring(src), "identity")
-- a short source gives a zero picture of the right size, no throw
local short = Yuv.scale(buffer.create(10), 64, 48, 32, 24)
t:eq(buffer.len(short), 32 * 24 + 2 * 16 * 12, "short source: size")
-- out reused when big enough
local reuse = buffer.create(32 * 24 + 2 * 16 * 12)
t:check(Yuv.scale(picture(64, 48), 64, 48, 32, 24, reuse) == reuse, "out reused")
local img = Types.Image.new(64, 48, "yuv420p")
img.pixels = picture(64, 48)
img.meta.yuv = { range = "limited", matrix = "bt601" }
local small = Yuv.imageScale(img, 32, 24)
t:check(small.format == "yuv420p" and small.width == 32 and small.height == 24, "imageScale")
t:check(small.meta.yuv and small.meta.yuv.range == "limited", "imageScale keeps meta")
local rgba = Yuv.imageToRgba(small)
t:check(rgba and rgba.width == 32 and rgba.format == "rgba8", "imageScale -> rgba")
t:done()
+46 -1
View File
@@ -1,7 +1,7 @@
-- video/mp4 tests: MP4 / MOV files from ffmpeg (MJPEG + AAC / PCM / FLAC / MP2, H.264 with B-frames and edit lists +
-- AAC, fragmented MP4, MPEG-1 + Opus): tracks and every packet against ffprobe (pts moved by the edit lists, as ffmpeg),
-- frames against ffmpeg (PSNR), audio through the api hook (AAC priming dropped), seeking, our MP4 / MOV writer read
-- back, fuzz. Fixtures: tools/fixtures/mp4.py.
-- back, the H.264 yield hook and maxSide, fuzz. Fixtures: tools/fixtures/mp4.py.
local T = require("@tests/T")
local C = require("@tests/video/_common")
local F = require("@formats")
@@ -76,6 +76,51 @@ for _, job in { { "mjpeg_aac.mp4", "mp4" }, { "mjpeg_pcm.mov", "mov" }, { "h264_
end
end
-- H.264 with the cooperative yield hook (decoding inside a coroutine that yields at every hook call: the same pictures)
-- and maxSide (frames shrunk to the longer side, aspect kept)
do
local Crc32 = require("@formats/core/Crc32")
local data = byName["h264_aac.mp4"].data
local function crcs(opts: any, inCoroutine: boolean): ({ number }, number, number, number)
local v = assert(F.video.open(data, opts))
local list, calls, w, h = {}, 0, 0, 0
local function body()
while true do
local f = v:nextFrame()
if not f then
break
end
w, h = f.image.width, f.image.height
table.insert(list, Crc32.of(f.image.pixels))
end
end
if inCoroutine then
local co = coroutine.create(body)
while coroutine.status(co) ~= "dead" do
local ok, err = coroutine.resume(co)
t:check(ok, "yield: resume " .. tostring(err))
calls += 1
end
else
body()
end
return list, calls, w, h
end
local plain, _, w0, h0 = crcs({ audio = false }, false)
local hooked, resumes = crcs({ audio = false, yield = function()
coroutine.yield()
end }, true)
t:check(#plain > 0, "h264: frames")
t:check(resumes > #plain, "h264 yield: the hook ran inside pictures (" .. resumes .. " resumes)")
local same = #plain == #hooked
for i, c in plain do
same = same and hooked[i] == c
end
t:check(same, "h264 yield: the same pictures as without the hook")
local _, _, w1, h1 = crcs({ audio = false, maxSide = 32 }, false)
t:check(math.max(w1, h1) == 32 and math.abs(w1 / h1 - w0 / h0) < 0.1, ("maxSide: %dx%d -> %dx%d"):format(w0, h0, w1, h1))
end
local samples = {}
for _, n in { "mjpeg_aac.mp4", "fragmented.mp4", "h264_aac.mp4" } do
table.insert(samples, string.sub(byName[n].data, 1, 30000))