codecs: JPEG / MPEG-1/2 yield hooks (per MCU row, per slice), banded Mpeg2.toRgba with RGB clamp table; build.py: reserved child names pass (work from the userspace session)
This commit is contained in:
1 parent
551fd937b9
commit
05eedbc8cf
10 files changed
+246
-14
No files matched your search
@@ -270,4 +270,33 @@ do
|
||||
end, { Fx.fields.data }, { mutations = 3, garbage = 10 })
|
||||
end
|
||||
|
||||
do -- opts.yield: the cooperative hook runs (scans, IDCT, colour), output unchanged, inside a coroutine, errors ignored
|
||||
local s = Fx.streams[1]
|
||||
local f = s.frames[1]
|
||||
local b = buffer.fromstring(f.data)
|
||||
local calls = 0
|
||||
local img = Jpeg.decode(b, 0, nil, { yield = function()
|
||||
calls += 1
|
||||
end }) :: Types.Image
|
||||
t:check(img and calls > 0, "yield hook runs (" .. calls .. " calls)")
|
||||
t:eq(Crc32.of(img.pixels), f.rgba, "yield: rgba8 unchanged")
|
||||
local yuv = Jpeg.decode(b, 0, nil, { yuv = true, yield = function() end }) :: Types.Image
|
||||
t:eq(Crc32.of(yuv.pixels), f.yuv, "yield: yuv420p unchanged")
|
||||
local resumes = 0
|
||||
local co = coroutine.create(function()
|
||||
return Jpeg.decode(b, 0, nil, { yield = coroutine.yield })
|
||||
end)
|
||||
local ok, res = coroutine.resume(co)
|
||||
while ok and coroutine.status(co) == "suspended" do
|
||||
resumes += 1
|
||||
ok, res = coroutine.resume(co)
|
||||
end
|
||||
t:check(ok and resumes > 1 and res ~= nil, "a decode suspended at the hook resumes")
|
||||
t:eq(res and Crc32.of((res :: any).pixels), f.rgba, "suspended decode: same pixels")
|
||||
local e = Jpeg.decode(b, 0, nil, { yield = function()
|
||||
error("hook error")
|
||||
end }) :: Types.Image
|
||||
t:eq(e and Crc32.of(e.pixels), f.rgba, "an erroring hook is ignored")
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -291,6 +291,79 @@ do -- rgba = false: planes only, frameImage still gives both
|
||||
t:check(y.format == "yuv420p" and buffer.readu8(y.pixels, W) == buffer.readu8(f.y, f.codedWidth), "frameImage crops the coded width")
|
||||
end
|
||||
|
||||
do -- toRgba in bands (from bug tv's workers): whole, row bands into one buffer, bands of plane copies; odd sizes
|
||||
local function bands(f: any, step: number): buffer
|
||||
local w, h, cw = f.width, f.height, f.codedWidth
|
||||
local out = buffer.create(w * h * 4)
|
||||
for r0 = 0, h - 1, step do
|
||||
local r1 = math.min(h, r0 + step)
|
||||
-- copies holding only rows r0 .. r1 - 1 (planeRow0 = r0), converted into a band, then placed
|
||||
local chw, c0, c1 = cw // 2, r0 // 2, (r1 + 1) // 2
|
||||
local y = buffer.create((r1 - r0) * cw)
|
||||
buffer.copy(y, 0, f.y, r0 * cw, (r1 - r0) * cw)
|
||||
local cb, cr = buffer.create((c1 - c0) * chw), buffer.create((c1 - c0) * chw)
|
||||
buffer.copy(cb, 0, f.cb, c0 * chw, (c1 - c0) * chw)
|
||||
buffer.copy(cr, 0, f.cr, c0 * chw, (c1 - c0) * chw)
|
||||
local band = buffer.create((r1 - r0) * w * 4)
|
||||
assert(Mpeg1.toRgba(y, cb, cr, cw, w, h, band, r0, r1, 0, r0))
|
||||
buffer.copy(out, r0 * w * 4, band)
|
||||
end
|
||||
return out
|
||||
end
|
||||
local dec = Mpeg1.new({ rgba = false })
|
||||
local u1 = main.units[1]
|
||||
local f = dec:decode(mainFile, u1[1], u1[2]) :: any
|
||||
local whole = buffer.create(f.width * f.height * 4)
|
||||
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, whole), "toRgba whole frame")
|
||||
t:check(buffer.tostring(whole) == ref[1], "toRgba whole = the decoder's RGBA")
|
||||
local into = buffer.create(f.width * f.height * 4)
|
||||
for r0 = 0, f.height - 1, 32 do
|
||||
Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, into, r0, math.min(f.height, r0 + 32), r0 * f.width * 4)
|
||||
end
|
||||
t:check(buffer.tostring(into) == ref[1], "toRgba row bands into one buffer")
|
||||
t:check(buffer.tostring(bands(f, 16)) == ref[1], "toRgba bands of plane copies (planeRow0)")
|
||||
local fo = STREAMS[6] -- 173 x 95
|
||||
local bo = b64(fo.file)
|
||||
local odec = Mpeg1.new({ rgba = false })
|
||||
local uo = fo.units[1]
|
||||
local of = odec:decode(bo, uo[1], uo[2]) :: any
|
||||
local oimg = Mpeg1.frameImage(of, true) :: Types.Image
|
||||
t:check(of and buffer.tostring(bands(of, 10)) == buffer.tostring(oimg.pixels), "toRgba bands at an odd size")
|
||||
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, buffer.create(4)) == false, "toRgba: out too small")
|
||||
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, 7, f.width, f.height, whole) == false, "toRgba: odd coded width")
|
||||
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, whole, 4, 2) == false, "toRgba: empty band")
|
||||
t:check(Mpeg1.toRgba(f.y, f.cb, f.cr, f.codedWidth, f.width, f.height, whole, 0, nil, 2) == false, "toRgba: unaligned offset")
|
||||
end
|
||||
|
||||
do -- the cooperative yield hook: after every slice and every 16 rows of conversion; output unchanged; errors ignored
|
||||
local calls = 0
|
||||
local dec = Mpeg1.new({ yield = function()
|
||||
calls += 1
|
||||
end })
|
||||
local u1 = main.units[1]
|
||||
local f = dec:decode(mainFile, u1[1], u1[2]) :: any
|
||||
t:check(f ~= nil and buffer.tostring(f.pixels) == ref[1] and calls > 0, "yield hook runs, same frame")
|
||||
local inCo = 0
|
||||
local co = coroutine.create(function()
|
||||
local d2 = Mpeg1.new()
|
||||
d2:setYield(function()
|
||||
coroutine.yield()
|
||||
end)
|
||||
return d2:decode(mainFile, u1[1], u1[2])
|
||||
end)
|
||||
local ok, res = coroutine.resume(co)
|
||||
while ok and coroutine.status(co) == "suspended" do
|
||||
inCo += 1
|
||||
ok, res = coroutine.resume(co)
|
||||
end
|
||||
t:check(ok and inCo > 1 and res ~= nil and buffer.tostring((res :: any).pixels) == ref[1], "a decode suspended at the hook resumes to the same frame")
|
||||
local bad = Mpeg1.new({ yield = function()
|
||||
error("hook error")
|
||||
end })
|
||||
local g = bad:decode(mainFile, u1[1], u1[2]) :: any
|
||||
t:check(g ~= nil and buffer.tostring(g.pixels) == ref[1], "an erroring hook is ignored")
|
||||
end
|
||||
|
||||
do -- reset (seek): the next picture must be an I-picture again
|
||||
local dec = Mpeg1.new()
|
||||
for i = 1, 5 do
|
||||
|
||||
Reference in new issue
Block a user