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:
commit
2eaed46eea
96 files changed
+13398
-517
No files matched your search
@@ -0,0 +1,273 @@
|
||||
-- codecs/Jpeg tests (the Motion-JPEG side; tests/image/jpeg.luau covers the still-image decoder and encoder through
|
||||
-- image/jpeg): ffmpeg mjpeg frames (4:2:0, 4:2:2, 4:4:4, odd sizes, stripped DHT, AVI1 + junk after EOI) decoded
|
||||
-- to yuv420p bit-exact with libjpeg-turbo's IDCT planes (TurboJPEG) and to rgba8 bit-exact with djpeg, and within
|
||||
-- PSNR thresholds of ffmpeg's own decode; an interlaced AVI1 frame (two fields) woven; the yuv420p encoder
|
||||
-- byte-identical to TurboJPEG's tj3CompressFromYUV8, read back by djpeg (exact) and ffmpeg (PSNR); the API's
|
||||
-- arguments; the fuzz no-throw pass. Fixtures: tools/fixtures/mjpeg.py.
|
||||
local T = require("@tests/T")
|
||||
local Jpeg = require("@formats/codecs/Jpeg")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Deflate = require("@formats/codecs/Deflate")
|
||||
local Fx = require("@tests/gen/mjpeg_cases")
|
||||
local Enc = require("@tests/gen/mjpeg_enc")
|
||||
|
||||
local t = T.suite("codecs/jpeg")
|
||||
|
||||
-- the fixtures' binary fields: base64, and base64 of zlib data for raw planes / pixels
|
||||
local function b64(s: string): string
|
||||
return buffer.tostring(Base64.decode(s) :: buffer)
|
||||
end
|
||||
local function z64(s: string): buffer
|
||||
return Deflate.zlib(Base64.decode(s) :: buffer) :: buffer
|
||||
end
|
||||
for _, s in Fx.streams do
|
||||
for _, f in s.frames do
|
||||
f.data = b64(f.data)
|
||||
end
|
||||
s.ff.yuv = z64(s.ff.yuv)
|
||||
if s.ff.rgba then
|
||||
s.ff.rgba = z64(s.ff.rgba)
|
||||
end
|
||||
end
|
||||
Fx.fields.data = b64(Fx.fields.data)
|
||||
for _, c in Enc do
|
||||
c.want = b64(c.want)
|
||||
end
|
||||
|
||||
-- PSNR of n bytes of a at ao against b at bo
|
||||
local function psnr(a: buffer, ao: number, b: buffer, bo: number, n: number): number
|
||||
local se = 0
|
||||
for i = 0, n - 1 do
|
||||
local d = buffer.readu8(a, ao + i) - buffer.readu8(b, bo + i)
|
||||
se += d * d
|
||||
end
|
||||
if se == 0 then
|
||||
return 99
|
||||
end
|
||||
return 10 * math.log10(255 * 255 * n / se)
|
||||
end
|
||||
|
||||
-- PSNR of the RGB channels of two rgba8 buffers of n pixels
|
||||
local function psnrRgb(a: buffer, b: buffer, n: number): number
|
||||
local se = 0
|
||||
for i = 0, n - 1 do
|
||||
for k = 0, 2 do
|
||||
local d = buffer.readu8(a, i * 4 + k) - buffer.readu8(b, i * 4 + k)
|
||||
se += d * d
|
||||
end
|
||||
end
|
||||
if se == 0 then
|
||||
return 99
|
||||
end
|
||||
return 10 * math.log10(255 * 255 * n * 3 / se)
|
||||
end
|
||||
|
||||
local function hasDht(s: string): boolean
|
||||
local at = string.find(s, "\255\196", 1, true)
|
||||
local sos = string.find(s, "\255\218", 1, true)
|
||||
return at ~= nil and sos ~= nil and at < sos
|
||||
end
|
||||
|
||||
-- 1. ffmpeg's frames: yuv420p (TurboJPEG planes) and rgba8 (djpeg) bit-exact, PSNR against ffmpeg's decode
|
||||
do
|
||||
local frames, worstY, worstC, worstRgb = 0, 99, 99, 99
|
||||
for _, s in Fx.streams do
|
||||
local w, h = s.width, s.height
|
||||
local cw, ch = (w + 1) // 2, (h + 1) // 2
|
||||
for i, f in s.frames do
|
||||
local label = string.format("%s frame %d", s.name, i)
|
||||
local b = buffer.fromstring(f.data)
|
||||
local img, err = Jpeg.decode(b, 0, buffer.len(b), { yuv = true })
|
||||
t:check(img, label .. ": " .. tostring(err))
|
||||
t:check(img.format == "yuv420p" and img.width == w and img.height == h, label .. ": a yuv420p " .. w .. "x" .. h .. " Image")
|
||||
t:eq(buffer.len(img.pixels), w * h + 2 * cw * ch, label .. ": plane sizes")
|
||||
t:check(img.meta.source.yuv == s.sampling and img.meta.source.range == "full", label .. ": meta.source.yuv " .. tostring(img.meta.source.yuv))
|
||||
t:check(not img.meta.truncated, label .. ": not truncated")
|
||||
t:eq(Crc32.of(img.pixels), f.yuv, label .. ": yuv420p planes = TurboJPEG's")
|
||||
local rgba = Jpeg.decode(b) :: Types.Image
|
||||
t:check(rgba and rgba.format == "rgba8", label .. ": rgba8")
|
||||
t:eq(Crc32.of(rgba.pixels), f.rgba, label .. ": rgba8 = djpeg")
|
||||
if i == 1 then
|
||||
local ref = s.ff.yuv
|
||||
local py = psnr(img.pixels, 0, ref, 0, w * h)
|
||||
local pc = math.min(psnr(img.pixels, w * h, ref, w * h, cw * ch), psnr(img.pixels, w * h + cw * ch, ref, w * h + cw * ch, cw * ch))
|
||||
-- ffmpeg's IDCT differs from libjpeg's islow by rounding only; its 4:2:2 / 4:4:4 -> 4:2:0 is a filter
|
||||
t:check(py > 55, string.format("%s: luma PSNR %.1f dB against ffmpeg", s.name, py))
|
||||
t:check(pc > (if s.sampling == "420" then 55 else 20), string.format("%s: chroma PSNR %.1f dB against ffmpeg", s.name, pc))
|
||||
worstY, worstC = math.min(worstY, py), math.min(worstC, pc)
|
||||
if s.ff.rgba then
|
||||
local pr = psnrRgb(rgba.pixels, s.ff.rgba, w * h)
|
||||
-- swscale upsamples chroma differently from libjpeg (fancy): testsrc2's sharp colour edges cost ~10 dB
|
||||
t:check(pr > 25, string.format("%s: rgba PSNR %.1f dB against ffmpeg", s.name, pr))
|
||||
worstRgb = math.min(worstRgb, pr)
|
||||
end
|
||||
end
|
||||
if string.find(s.name, "nodht") then
|
||||
t:check(not hasDht(f.data), label .. ": no DHT segment")
|
||||
end
|
||||
if s.name == "avi1_junk" then
|
||||
t:eq(img.meta.source.avi1, 0, label .. ": AVI1 polarity")
|
||||
t:eq(Jpeg.info(b).avi1, 0, label .. ": info.avi1")
|
||||
end
|
||||
frames += 1
|
||||
end
|
||||
end
|
||||
t:note("%d ffmpeg mjpeg frames bit-exact (yuv420p: TurboJPEG, rgba8: djpeg); worst PSNR against ffmpeg: luma %.1f, chroma %.1f, rgba %.1f dB", frames, worstY, worstC, worstRgb)
|
||||
end
|
||||
|
||||
-- 2. an interlaced frame: two fields (AVI1 polarity 1) woven, field 1 on the even rows
|
||||
do
|
||||
local F = Fx.fields
|
||||
local b = buffer.fromstring(F.data)
|
||||
local img = Jpeg.decode(b, 0, nil, { yuv = true }) :: Types.Image
|
||||
t:check(img and img.width == F.width and img.height == F.height and img.format == "yuv420p", "fields: one 320x240 yuv420p frame")
|
||||
t:eq(img.meta.source.fields, 2, "fields: meta.source.fields")
|
||||
t:eq(Crc32.of(img.pixels), F.yuv, "fields: woven yuv420p planes = TurboJPEG's fields woven")
|
||||
local src = z64(F.src)
|
||||
local p = psnr(img.pixels, 0, src, 0, F.width * F.height)
|
||||
t:check(p > 35, string.format("fields: luma PSNR %.1f dB against the source frame", p))
|
||||
local rgba = Jpeg.decode(b) :: Types.Image
|
||||
t:check(rgba.height == F.height and Crc32.of(rgba.pixels) == F.rgba, "fields: woven rgba8 = djpeg's fields woven")
|
||||
local one = Jpeg.decode(b, 0, nil, { yuv = true, fields = false }) :: Types.Image
|
||||
t:check(one.height == F.height // 2 and one.meta.source.fields == 1, "fields = false: the first field only")
|
||||
-- the first field alone (no second JPEG): a half-height frame, not an error
|
||||
local first = string.find(F.data, "\255\217", 1, true) :: number
|
||||
local half = Jpeg.decode(buffer.fromstring(string.sub(F.data, 1, first + 1)), 0, nil, { yuv = true }) :: Types.Image
|
||||
t:check(half and half.height == F.height // 2 and half.meta.source.fields == 1, "a missing second field leaves the first")
|
||||
t:check(Jpeg.info(b).avi1 == 1, "fields: info.avi1")
|
||||
end
|
||||
|
||||
-- 3. the yuv420p encoder: byte-identical with TurboJPEG, read back by djpeg (exact) and ffmpeg (PSNR)
|
||||
do
|
||||
local exact = 0
|
||||
for _, c in Enc do
|
||||
local img = Types.Image.new(c.width, c.height, "yuv420p")
|
||||
img.pixels = z64(c.yuv)
|
||||
local out, err = Jpeg.encode(img, { quality = c.quality })
|
||||
t:check(out, c.name .. ": " .. tostring(err))
|
||||
local s = buffer.tostring(out :: buffer)
|
||||
if s ~= c.want then
|
||||
local at = 0
|
||||
for i = 1, math.min(#s, #c.want) do
|
||||
if string.byte(s, i) ~= string.byte(c.want, i) then
|
||||
at = i
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(false, string.format("%s: differs from tj3CompressFromYUV8 at byte %d (ours %d bytes, TurboJPEG %d)", c.name, at, #s, #c.want))
|
||||
end
|
||||
exact += 1
|
||||
local back = Jpeg.decode(out :: buffer) :: Types.Image
|
||||
t:eq(Crc32.of(back.pixels), c.rgba, c.name .. ": our decode of it = djpeg's")
|
||||
local floor = if c.quality >= 75 then 30 else 26
|
||||
if c.width * c.height >= 1000 then
|
||||
t:check(c.ffpsnr > floor, string.format("%s: ffmpeg reads it at %.1f dB luma PSNR", c.name, c.ffpsnr))
|
||||
local y = Jpeg.decode(out :: buffer, 0, nil, { yuv = true }) :: Types.Image
|
||||
local p = psnr(y.pixels, 0, img.pixels, 0, c.width * c.height)
|
||||
t:check(p > floor, string.format("%s: yuv round trip %.1f dB", c.name, p))
|
||||
end
|
||||
end
|
||||
t:note("%d yuv420p encodes byte-identical with tj3CompressFromYUV8", exact)
|
||||
-- grey from yuv420p: the luma plane only
|
||||
local c = Enc[1]
|
||||
local img = Types.Image.new(c.width, c.height, "yuv420p")
|
||||
img.pixels = z64(c.yuv)
|
||||
local g = Jpeg.encode(img, { quality = 80, gray = true }) :: buffer
|
||||
local gi = Jpeg.info(g)
|
||||
t:check(gi and gi.components == 1, "yuv420p with gray = true: one component")
|
||||
local gy = Jpeg.decode(g, 0, nil, { yuv = true }) :: Types.Image
|
||||
t:check(gy.format == "yuv420p" and gy.meta.source.yuv == "gray" and buffer.readu8(gy.pixels, c.width * c.height) == 128, "grey decodes to yuv420p with chroma 128")
|
||||
local p = Jpeg.encode(img, { quality = 80, progressive = true, restart = 2 }) :: buffer
|
||||
local pi = Jpeg.info(p)
|
||||
t:check(pi and pi.progressive and pi.sampling[1][1] == 2 and pi.sampling[1][2] == 2, "yuv420p progressive")
|
||||
local pd = Jpeg.decode(p, 0, nil, { yuv = true }) :: Types.Image
|
||||
t:check(psnr(pd.pixels, 0, img.pixels, 0, c.width * c.height) > 30, "yuv420p progressive round trip")
|
||||
local s4 = Jpeg.encode(img, { subsampling = "444" }) :: buffer
|
||||
t:eq(Jpeg.info(s4).sampling[1][1], 2, "yuv420p input is always 4:2:0")
|
||||
-- extra segments and density
|
||||
local seg = Jpeg.encode(img, { segments = { { marker = 0xFE, data = buffer.fromstring("hello") } }, density = { unit = 1, x = 72, y = 72 } }) :: buffer
|
||||
local sd = Jpeg.decode(seg) :: Types.Image
|
||||
t:check(sd.meta.text and sd.meta.text.comment == "hello" and sd.meta.dpi and sd.meta.dpi.x == 72, "segments and density")
|
||||
end
|
||||
|
||||
-- 4. the API: ranges, options, fallbacks, errors
|
||||
do
|
||||
local s = Fx.streams[1]
|
||||
local data = s.frames[1].data
|
||||
local big = buffer.create(#data + 30)
|
||||
buffer.writestring(big, 17, data)
|
||||
local a = Jpeg.decode(big, 17, #data, { yuv = true }) :: Types.Image
|
||||
t:eq(Crc32.of(a.pixels), s.frames[1].yuv, "decode from a sub-range")
|
||||
local i = Jpeg.info(big, 17, #data)
|
||||
t:check(i and i.width == s.width and i.colorSpace == "ycbcr" and i.sampling[1][2] == 2, "info from a sub-range")
|
||||
local sc = Jpeg.decode(buffer.fromstring(data), 0, nil, { yuv = true, scale = 2 }) :: Types.Image
|
||||
t:check(sc.format == "rgba8" and sc.width == s.width // 2, "yuv with scale 2 falls back to rgba8")
|
||||
local _, e1 = Jpeg.decode("nope" :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "decode(string)")
|
||||
local _, e2 = Jpeg.decode(buffer.fromstring("nope"))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "junk")
|
||||
local _, e3 = Jpeg.decode(buffer.fromstring(data), 0, nil, { maxPixels = 100, yuv = true })
|
||||
t:check(e3 and e3.code == Err.LIMIT, "maxPixels")
|
||||
local _, e4 = Jpeg.decode(buffer.fromstring(data), 0, nil, 5 :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "non-table options")
|
||||
local _, e5 = Jpeg.encode(Types.Image.new(4, 4, "rgba16"))
|
||||
t:check(e5 and e5.code == Err.ARG, "rgba16 is refused")
|
||||
local short = Types.Image.new(8, 8, "yuv420p")
|
||||
short.pixels = buffer.create(70)
|
||||
local _, e6 = Jpeg.encode(short)
|
||||
t:check(e6 and e6.code == Err.ARG, "a short yuv420p buffer")
|
||||
local _, e7 = Jpeg.encode(Types.Image.new(8, 8), { segments = { { marker = 0xC4, data = buffer.create(1) } } })
|
||||
t:check(e7 and e7.code == Err.ARG, "a bad segment marker")
|
||||
local _, e8 = Jpeg.info(buffer.create(0))
|
||||
t:check(e8 and Err.is(e8), "info of nothing")
|
||||
-- truncated frames decode as far as they go or fail with an Err
|
||||
local cuts, partial = 0, 0
|
||||
for cut = 0, #data - 1, #data // 37 do
|
||||
local img, err = Jpeg.decode(buffer.fromstring(string.sub(data, 1, cut)), 0, nil, { yuv = true })
|
||||
t:check(img or Err.is(err), "cut at " .. cut .. ": no image and no Err")
|
||||
if img then
|
||||
partial += 1
|
||||
t:check(img.meta.truncated, "cut at " .. cut .. ": flagged truncated")
|
||||
end
|
||||
cuts += 1
|
||||
end
|
||||
t:note("%d truncated frames, %d decoded partially", cuts, partial)
|
||||
end
|
||||
|
||||
-- 5. speed (a note; tools/bench.luau has the real numbers)
|
||||
do
|
||||
local s = Fx.streams[1]
|
||||
local frames = {}
|
||||
for _, f in s.frames do
|
||||
table.insert(frames, buffer.fromstring(f.data))
|
||||
end
|
||||
local t0, n = os.clock(), 0
|
||||
while os.clock() - t0 < 0.3 do
|
||||
for _, b in frames do
|
||||
Jpeg.decode(b, 0, nil, { yuv = true })
|
||||
n += 1
|
||||
end
|
||||
end
|
||||
t:note("%dx%d yuv420p decode: %.0f fps", s.width, s.height, n / (os.clock() - t0))
|
||||
end
|
||||
|
||||
-- 6. fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for k, s in Fx.streams do
|
||||
if k % 2 == 1 then
|
||||
table.insert(samples, s.frames[1].data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode yuv", function(b: buffer)
|
||||
return Jpeg.decode(b, 0, nil, { yuv = true })
|
||||
end, samples, { mutations = 4, keepHeader = 2 })
|
||||
t:fuzz("decode fields", function(b: buffer)
|
||||
return Jpeg.decode(b, 0, nil, { fields = true })
|
||||
end, { Fx.fields.data }, { mutations = 3, garbage = 10 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,413 @@
|
||||
-- codecs/Mpeg1 tests, ported from mpeg1-luau tests/decode.luau and tests/robust.luau. Against ffmpeg
|
||||
-- (tools/fixtures/mpeg1.py): every stream decodes, one access unit at a time, to as many frames as ffmpeg's decode,
|
||||
-- in display order; each frame's RGBA channel means within 1.5 of ffmpeg's; whole sampled frames within 2 MAE per
|
||||
-- channel (IDCT and colour rounding) and, as yuv420p (frameImage), within a PSNR floor of ffmpeg -pix_fmt yuv420p;
|
||||
-- the same stream written in random pieces through write() / decodeFrame() gives identical frames; a size change.
|
||||
-- Robustness / API: bad arguments, pictures before the first sequence header, reset(), rgba = false, tags through
|
||||
-- B-frame reordering, string input, truncated units, bit flips and garbage (never throws, recovers at an
|
||||
-- I-picture), an oversized sequence header; frameImage; the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local Mpeg1 = require("@formats/codecs/Mpeg1")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Deflate = require("@formats/codecs/Deflate")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local STREAMS = {
|
||||
require("@tests/gen/mpeg1_qcif_200k"),
|
||||
require("@tests/gen/mpeg1_main_450k"),
|
||||
require("@tests/gen/mpeg1_zoom_150k"),
|
||||
require("@tests/gen/mpeg1_zoom_1500k"),
|
||||
require("@tests/gen/mpeg1_big_1000k"),
|
||||
require("@tests/gen/mpeg1_odd_173x95"),
|
||||
require("@tests/gen/mpeg1_matrix"),
|
||||
require("@tests/gen/mpeg1_q1"),
|
||||
require("@tests/gen/mpeg1_q31"),
|
||||
require("@tests/gen/mpeg1_zeromv"),
|
||||
require("@tests/gen/mpeg1_bframes"),
|
||||
require("@tests/gen/mpeg1_bframes_zoom"),
|
||||
}
|
||||
local SizeChange = require("@tests/gen/mpeg1_sizechange")
|
||||
|
||||
local t = T.suite("codecs/mpeg1")
|
||||
local rand = T.rng(7)
|
||||
local function between(a: number, b: number): number
|
||||
return a + rand(b - a + 1)
|
||||
end
|
||||
|
||||
local function b64(s: string): buffer
|
||||
return Base64.decode(s) :: buffer
|
||||
end
|
||||
local function z64(s: string): buffer
|
||||
return Deflate.zlib(b64(s)) :: buffer
|
||||
end
|
||||
|
||||
-- mean absolute error per channel (r, g, b) between two RGBA8 buffers of n pixels, and the largest difference
|
||||
local function mae(a: buffer, b: buffer, n: number): (number, number, number, number)
|
||||
local sr, sg, sb, mx = 0, 0, 0, 0
|
||||
for i = 0, n - 1 do
|
||||
local o = i * 4
|
||||
local dr = math.abs(buffer.readu8(a, o) - buffer.readu8(b, o))
|
||||
local dg = math.abs(buffer.readu8(a, o + 1) - buffer.readu8(b, o + 1))
|
||||
local db = math.abs(buffer.readu8(a, o + 2) - buffer.readu8(b, o + 2))
|
||||
sr += dr
|
||||
sg += dg
|
||||
sb += db
|
||||
mx = math.max(mx, dr, dg, db)
|
||||
end
|
||||
return sr / n, sg / n, sb / n, mx
|
||||
end
|
||||
|
||||
local function means(a: buffer, n: number): (number, number, number)
|
||||
local r, g, b = 0, 0, 0
|
||||
for i = 0, n - 1 do
|
||||
local o = i * 4
|
||||
r += buffer.readu8(a, o)
|
||||
g += buffer.readu8(a, o + 1)
|
||||
b += buffer.readu8(a, o + 2)
|
||||
end
|
||||
return r / n, g / n, b / n
|
||||
end
|
||||
|
||||
local function psnr(a: buffer, ao: number, b: buffer, bo: number, n: number): number
|
||||
local se = 0
|
||||
for i = 0, n - 1 do
|
||||
local d = buffer.readu8(a, ao + i) - buffer.readu8(b, bo + i)
|
||||
se += d * d
|
||||
end
|
||||
if se == 0 then
|
||||
return 99
|
||||
end
|
||||
return 10 * math.log10(255 * 255 * n / se)
|
||||
end
|
||||
|
||||
local MAE_LIMIT = 2
|
||||
local MEAN_LIMIT = 1.5
|
||||
|
||||
-- 1. every stream against ffmpeg
|
||||
local worst, worstY, worstC = 0, 99, 99
|
||||
for _, fx in STREAMS do
|
||||
local name = fx.name
|
||||
local file = b64(fx.file)
|
||||
local w, h = fx.width, fx.height
|
||||
local n = w * h
|
||||
local cw, ch = (w + 1) // 2, (h + 1) // 2
|
||||
local rgbaRef, yuvRef = {}, {}
|
||||
for _, s in fx.rgba do
|
||||
rgbaRef[s.index] = z64(s.pixels)
|
||||
end
|
||||
for _, s in fx.yuv do
|
||||
yuvRef[s.index] = z64(s.planes)
|
||||
end
|
||||
local hasB = string.find(name, "bframes") ~= nil
|
||||
local dec = Mpeg1.new({ reorder = hasB })
|
||||
local frames = 0
|
||||
local maxMae, maxMeanErr = 0, 0
|
||||
local sums = {}
|
||||
local function onFrame(f: any)
|
||||
frames += 1
|
||||
t:check(f.width == w and f.height == h, string.format("%s: size %dx%d", name, f.width, f.height))
|
||||
t:eq(f.index, frames, name .. ": frame index")
|
||||
local px = f.pixels :: buffer
|
||||
t:eq(buffer.len(px), n * 4, name .. ": pixels size")
|
||||
local ref = fx.means[frames]
|
||||
t:check(ref ~= nil, string.format("%s: more frames than ffmpeg (%d)", name, frames))
|
||||
local r, g, b = means(px, n)
|
||||
local me = math.max(math.abs(r - ref[1]), math.abs(g - ref[2]), math.abs(b - ref[3]))
|
||||
maxMeanErr = math.max(maxMeanErr, me)
|
||||
t:check(me <= MEAN_LIMIT, string.format("%s frame %d: channel means off by %.2f", name, frames, me))
|
||||
local s = rgbaRef[frames]
|
||||
if s then
|
||||
local mr, mg, mb = mae(px, s, n)
|
||||
local m = math.max(mr, mg, mb)
|
||||
maxMae = math.max(maxMae, m)
|
||||
t:check(m <= MAE_LIMIT, string.format("%s frame %d: MAE %.3f %.3f %.3f", name, frames, mr, mg, mb))
|
||||
local again = Mpeg1.frameImage(f, true) :: Types.Image
|
||||
t:check(again.format == "rgba8" and buffer.tostring(again.pixels) == buffer.tostring(px), name .. ": frameImage(f, true) = frame.pixels")
|
||||
end
|
||||
local y = yuvRef[frames]
|
||||
if y then
|
||||
local img = Mpeg1.frameImage(f) :: Types.Image
|
||||
t:check(img.format == "yuv420p" and img.width == w and img.height == h and buffer.len(img.pixels) == n + 2 * cw * ch, name .. ": frameImage yuv420p")
|
||||
t:check(img.meta.source.range == "limited" and img.meta.source.pictureType == f.pictureType, name .. ": frameImage meta")
|
||||
local py = psnr(img.pixels, 0, y, 0, n)
|
||||
local pc = psnr(img.pixels, n, y, n, 2 * cw * ch)
|
||||
worstY, worstC = math.min(worstY, py), math.min(worstC, pc)
|
||||
t:check(py > 50 and pc > 50, string.format("%s frame %d: yuv420p PSNR %.1f / %.1f dB against ffmpeg", name, frames, py, pc))
|
||||
end
|
||||
t:check(buffer.readu8(px, 3) == 255 and buffer.readu8(px, n * 4 - 1) == 255, "alpha 255")
|
||||
sums[frames] = buffer.tostring(px)
|
||||
end
|
||||
local t0 = os.clock()
|
||||
for _, u in fx.units do
|
||||
local f = dec:decode(file, u[1], u[2])
|
||||
if f then
|
||||
onFrame(f)
|
||||
end
|
||||
end
|
||||
while true do
|
||||
local f = dec:flush()
|
||||
if f == nil then
|
||||
break
|
||||
end
|
||||
onFrame(f)
|
||||
end
|
||||
local dt = os.clock() - t0
|
||||
t:eq(frames, fx.frames, name .. ": frames (ffmpeg's count)")
|
||||
t:eq(dec.stats.errors, 0, name .. ": no bitstream errors")
|
||||
worst = math.max(worst, maxMae)
|
||||
t:note("%-13s %4dx%-4d %3d frames worst MAE %.3f means err %.3f %6.1f fps (with the checks)", name, w, h, frames, maxMae, maxMeanErr, frames / dt)
|
||||
|
||||
-- streamed: random pieces through write / decodeFrame, then flush
|
||||
local d2 = Mpeg1.new({ reorder = hasB })
|
||||
local got = 0
|
||||
local p, total = 0, buffer.len(file)
|
||||
local function take(f: any)
|
||||
got += 1
|
||||
t:check(sums[got] == buffer.tostring(f.pixels :: buffer), string.format("%s streamed frame %d identical", name, got))
|
||||
end
|
||||
while p < total do
|
||||
local k = math.min(total - p, between(1, 6000))
|
||||
t:check(d2:write(file, p, k), "write")
|
||||
p += k
|
||||
while true do
|
||||
local f = d2:decodeFrame()
|
||||
if f == nil then
|
||||
break
|
||||
end
|
||||
take(f)
|
||||
end
|
||||
end
|
||||
while true do
|
||||
local f = d2:flush()
|
||||
if f == nil then
|
||||
break
|
||||
end
|
||||
take(f)
|
||||
end
|
||||
t:eq(got, frames, name .. ": streamed frame count")
|
||||
end
|
||||
t:note("worst sampled-frame MAE %.3f (limit %d); worst yuv420p PSNR luma %.1f, chroma %.1f dB", worst, MAE_LIMIT, worstY, worstC)
|
||||
|
||||
-- 2. a size change mid-stream
|
||||
do
|
||||
local file = b64(SizeChange.file)
|
||||
local dec = Mpeg1.new()
|
||||
dec:write(file)
|
||||
local list = {}
|
||||
while true do
|
||||
local f = dec:decodeFrame() or dec:flush()
|
||||
if f == nil then
|
||||
break
|
||||
end
|
||||
table.insert(list, { w = f.width, h = f.height, px = buffer.tostring(f.pixels :: buffer) })
|
||||
end
|
||||
local a, b = SizeChange.parts[1], SizeChange.parts[2]
|
||||
t:eq(#list, a.frames + b.frames, "sizechange: frame count")
|
||||
t:check(list[1].w == a.width and list[1].h == a.height, "sizechange: first size")
|
||||
t:check(list[a.frames].w == a.width and list[a.frames + 1].w == b.width and list[#list].h == b.height, "sizechange: second size")
|
||||
for k, part in { a, b } do
|
||||
local last = list[if k == 1 then a.frames else #list]
|
||||
local mr, mg, mb = mae(buffer.fromstring(last.px), z64(part.last), part.width * part.height)
|
||||
t:check(math.max(mr, mg, mb) <= MAE_LIMIT, string.format("sizechange part %d MAE %.3f %.3f %.3f", k, mr, mg, mb))
|
||||
end
|
||||
end
|
||||
|
||||
-- 3. robustness and the API
|
||||
local function slice(b: buffer, from: number, n: number): buffer
|
||||
local out = buffer.create(n)
|
||||
buffer.copy(out, 0, b, from, n)
|
||||
return out
|
||||
end
|
||||
local main = STREAMS[2]
|
||||
local mainFile = b64(main.file)
|
||||
local W, H = main.width, main.height
|
||||
local ref = {}
|
||||
do
|
||||
local dec = Mpeg1.new()
|
||||
for _, u in main.units do
|
||||
local f = dec:decode(mainFile, u[1], u[2])
|
||||
table.insert(ref, buffer.tostring((f :: any).pixels))
|
||||
end
|
||||
t:eq(#ref, #main.units, "reference decode")
|
||||
end
|
||||
|
||||
do -- bad arguments
|
||||
local dec = Mpeg1.new()
|
||||
t:check(dec:decode((nil :: any)) == nil, "decode(nil)")
|
||||
t:check(dec:decode((42 :: any)) == nil, "decode(number)")
|
||||
t:check(dec:write(buffer.create(4), 10) == false, "write offset past the end")
|
||||
t:check(dec:write(buffer.create(4), 0, 9) == false, "write length past the end")
|
||||
t:check(dec:write(buffer.create(4), -1) == false, "write negative offset")
|
||||
t:check(dec:decodeFrame() == nil, "decodeFrame with no data")
|
||||
t:check(dec:flush() == nil, "flush with no data")
|
||||
t:check(Mpeg1.new((5 :: any)) ~= nil, "new(non-table)")
|
||||
t:check(dec:decode(buffer.create(0)) == nil, "empty unit")
|
||||
t:check(Mpeg1.frameImage(nil :: any) == nil and Mpeg1.frameImage({} :: any) == nil, "frameImage of a non-frame")
|
||||
end
|
||||
|
||||
do -- string input; pictures before the first sequence header are skipped
|
||||
local dec = Mpeg1.new()
|
||||
local u2 = main.units[2]
|
||||
t:check(dec:decode(buffer.tostring(slice(mainFile, u2[1], u2[2]))) == nil, "P picture before any sequence header")
|
||||
local u1 = main.units[1]
|
||||
local f = dec:decode(buffer.tostring(slice(mainFile, u1[1], u1[2])))
|
||||
t:check(f ~= nil and buffer.tostring((f :: any).pixels) == ref[1], "string unit decodes")
|
||||
t:check(dec.width == W and dec.height == H and math.abs(dec.frameRate - 24) < 0.01, "sequence fields")
|
||||
t:check(f.pictureType == Mpeg1.PICTURE_I and f.codedWidth == 320 and buffer.len(f.y) == 320 * 192, "frame fields")
|
||||
t:eq(dec.stats.skipped, 1, "stats.skipped")
|
||||
end
|
||||
|
||||
do -- rgba = false: planes only, frameImage still gives both
|
||||
local dec = Mpeg1.new({ rgba = false })
|
||||
local u1 = main.units[1]
|
||||
local f = dec:decode(mainFile, u1[1], u1[2]) :: any
|
||||
t:check(f ~= nil and f.pixels == nil and buffer.len(f.y) > 0 and buffer.len(f.cb) == buffer.len(f.y) // 4, "rgba = false")
|
||||
local img = Mpeg1.frameImage(f, true) :: Types.Image
|
||||
t:check(img and buffer.tostring(img.pixels) == ref[1], "frameImage(f, true) after rgba = false")
|
||||
local y = Mpeg1.frameImage(f) :: Types.Image
|
||||
t:check(y.format == "yuv420p" and buffer.readu8(y.pixels, W) == buffer.readu8(f.y, f.codedWidth), "frameImage crops the coded width")
|
||||
end
|
||||
|
||||
do -- reset (seek): the next picture must be an I-picture again
|
||||
local dec = Mpeg1.new()
|
||||
for i = 1, 5 do
|
||||
local u = main.units[i]
|
||||
dec:decode(mainFile, u[1], u[2])
|
||||
end
|
||||
dec:reset()
|
||||
local u = main.units[10]
|
||||
t:check(dec:decode(mainFile, u[1], u[2]) == nil, "P after reset has no reference")
|
||||
u = main.units[1]
|
||||
local f = dec:decode(mainFile, u[1], u[2])
|
||||
t:check(f ~= nil and buffer.tostring((f :: any).pixels) == ref[1], "I after reset")
|
||||
end
|
||||
|
||||
do -- tags (pts) follow pictures through reordering
|
||||
local fx = STREAMS[11]
|
||||
local b = b64(fx.file)
|
||||
local dec = Mpeg1.new({ reorder = true, rgba = false })
|
||||
local order = {}
|
||||
for i, u in fx.units do
|
||||
local f = dec:decode(b, u[1], u[2], i)
|
||||
if f then
|
||||
table.insert(order, f)
|
||||
end
|
||||
end
|
||||
while true do
|
||||
local f = dec:flush()
|
||||
if f == nil then
|
||||
break
|
||||
end
|
||||
table.insert(order, f)
|
||||
end
|
||||
t:eq(#order, #fx.units, "bframes: every picture out")
|
||||
local sawB = false
|
||||
for i = 2, #order do
|
||||
local a, c = order[i - 1], order[i]
|
||||
if c.temporalReference ~= 0 then
|
||||
t:check(c.temporalReference == a.temporalReference + 1, "display order at " .. i)
|
||||
end
|
||||
if c.pictureType == Mpeg1.PICTURE_B then
|
||||
sawB = true
|
||||
t:check(c.tag == a.tag + 1 or a.pictureType ~= Mpeg1.PICTURE_B, "B tag order")
|
||||
end
|
||||
end
|
||||
t:check(sawB, "the stream has B-frames")
|
||||
local d2 = Mpeg1.new()
|
||||
for i, u in fx.units do
|
||||
local f = d2:decode(b, u[1], u[2], i)
|
||||
t:check(f ~= nil and f.tag == i, "decode order tag " .. i)
|
||||
end
|
||||
end
|
||||
|
||||
do -- truncated units: every prefix length of a picture, then recovery
|
||||
local dec = Mpeg1.new()
|
||||
local u1 = main.units[1]
|
||||
for k = 0, u1[2], 97 do
|
||||
Mpeg1.new():decode(mainFile, u1[1], k)
|
||||
end
|
||||
for i = 1, 6 do
|
||||
local u = main.units[i]
|
||||
dec:decode(mainFile, u[1], u[2] // 2)
|
||||
end
|
||||
local f = dec:decode(mainFile, main.units[1][1], main.units[1][2])
|
||||
t:check(f ~= nil and buffer.tostring((f :: any).pixels) == ref[1], "recovers at an I-picture after truncation")
|
||||
end
|
||||
|
||||
-- bit flips and byte garbage in every unit: no throw, sane frames
|
||||
local rounds = 0
|
||||
for _, k in { 2, 11, 8, 6 } do
|
||||
local fx = STREAMS[k]
|
||||
local b = b64(fx.file)
|
||||
for round = 1, 6 do
|
||||
local dec = Mpeg1.new({ reorder = round % 2 == 0 })
|
||||
for _, u in fx.units do
|
||||
local unit = slice(b, u[1], u[2])
|
||||
local nflip = if round <= 3 then round * 3 else 40
|
||||
for _ = 1, nflip do
|
||||
local at = between(0, u[2] - 1)
|
||||
if round == 6 then
|
||||
buffer.writeu8(unit, at, between(0, 255))
|
||||
else
|
||||
buffer.writeu8(unit, at, bit32.bxor(buffer.readu8(unit, at), bit32.lshift(1, between(0, 7))))
|
||||
end
|
||||
end
|
||||
local ok, f = pcall(dec.decode, dec, unit)
|
||||
t:check(ok, fx.name .. ": corrupt unit threw " .. tostring(f))
|
||||
if f then
|
||||
t:check(buffer.len((f :: any).pixels) == f.width * f.height * 4, "frame size")
|
||||
end
|
||||
end
|
||||
while dec:flush() do
|
||||
end
|
||||
rounds += 1
|
||||
end
|
||||
end
|
||||
t:note("%d corrupted-stream rounds", rounds)
|
||||
|
||||
do -- streamed garbage and random units
|
||||
local dec = Mpeg1.new()
|
||||
for _ = 1, 300 do
|
||||
local n = between(0, 3000)
|
||||
local g = buffer.create(n)
|
||||
for i = 0, n - 1 do
|
||||
local r = between(0, 9)
|
||||
buffer.writeu8(g, i, if r < 3 then 0 elseif r == 3 then 1 elseif r == 4 then 0xB3 else between(0, 255))
|
||||
end
|
||||
dec:write(g)
|
||||
while dec:decodeFrame() do
|
||||
end
|
||||
dec:decode(g)
|
||||
end
|
||||
dec:reset()
|
||||
local f = dec:decode(mainFile, main.units[1][1], main.units[1][2])
|
||||
t:check(f ~= nil and buffer.tostring((f :: any).pixels) == ref[1], "clean after garbage")
|
||||
end
|
||||
|
||||
do -- a hostile sequence header: a huge size is rejected
|
||||
local b = buffer.create(16)
|
||||
buffer.writestring(b, 0, "\0\0\1\xB3\xFF\xFF\xFF\x13\xFF\xFF\xE0\x18\0\0\1\xB8")
|
||||
local dec = Mpeg1.new({ maxPixels = 1000 * 1000 })
|
||||
dec:decode(b)
|
||||
t:eq(dec.width, 0, "oversized sequence header rejected")
|
||||
end
|
||||
|
||||
-- 4. the fuzz pass: whole elementary streams (cut, mutated, garbage) through one decoder each
|
||||
do
|
||||
local samples = {}
|
||||
for _, k in { 1, 9 } do
|
||||
local fx = STREAMS[k]
|
||||
local b = b64(fx.file)
|
||||
local u = fx.units
|
||||
table.insert(samples, buffer.tostring(slice(b, 0, u[4][1] + u[4][2])))
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local dec = Mpeg1.new({ reorder = true })
|
||||
local out = { dec:decode(b) }
|
||||
while dec:flush() do
|
||||
end
|
||||
return table.unpack(out)
|
||||
end, samples, { mutations = 12, keepHeader = 4 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -56,7 +56,7 @@ do
|
||||
local inter = buffer.fromstring(frames[1])
|
||||
buffer.writeu8(inter, 0, bit32.bor(buffer.readu8(inter, 0), 1))
|
||||
local _, e1 = Vp8.decodeFrame(inter)
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "inter frame: " .. tostring(e1))
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "inter frame without a key frame: " .. tostring(e1))
|
||||
local bad = buffer.fromstring(frames[1])
|
||||
buffer.writeu8(bad, 3, 0)
|
||||
local _, e2 = Vp8.decodeFrame(bad)
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
-- codecs/Vp8 video tests: whole IVF streams through one decoder object, every shown frame bit-exact against ffmpeg's
|
||||
-- native vp8 decoder (yuv420p CRC32): the official VP8 test vectors (vp80-00-comprehensive-001..018 and others) and
|
||||
-- libvpx clips at many settings (alt-ref / hidden frames, golden, token partitions, error resilience, profiles 1-3,
|
||||
-- sharpness, segmentation, q extremes, short key frame intervals, motion off the frame, odd sizes). Also hidden
|
||||
-- frames, reset() and inter frames without a key frame, and the fuzz no-throw pass over damaged streams.
|
||||
-- Fixtures: tools/fixtures/vp8video.py.
|
||||
local T = require("@tests/T")
|
||||
local Vp8 = require("@formats/codecs/Vp8")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Cases = require("@tests/gen/vp8video_cases")
|
||||
|
||||
local t = T.suite("vp8video")
|
||||
|
||||
-- the frames of an IVF file: { offset, length }
|
||||
local function ivfFrames(b: buffer): { { number } }
|
||||
local out = {}
|
||||
local p = buffer.readu16(b, 6) -- header length (32)
|
||||
while p + 12 <= buffer.len(b) do
|
||||
local n = buffer.readu32(b, p)
|
||||
if p + 12 + n > buffer.len(b) then
|
||||
break
|
||||
end
|
||||
table.insert(out, { p + 12, n })
|
||||
p += 12 + n
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local total, hidden, vectors = 0, 0, 0
|
||||
local samples: { buffer } = {}
|
||||
for _, c in Cases do
|
||||
local b = buffer.fromstring(c.ivf)
|
||||
local fr = ivfFrames(b)
|
||||
local dec = Vp8.newDecoder()
|
||||
local shown, bad, firstBad = 0, 0, ""
|
||||
local ok = true
|
||||
for i, f in fr do
|
||||
local img, err = dec:decodeFrame(b, f[1], f[2])
|
||||
if not img then
|
||||
ok = false
|
||||
firstBad = "frame " .. i .. ": " .. tostring(err)
|
||||
break
|
||||
end
|
||||
local src = img.meta.source
|
||||
if src.show then
|
||||
shown += 1
|
||||
if buffer.len(img.pixels) ~= c.sizes[shown] or Crc32.of(img.pixels) ~= c.crcs[shown] then
|
||||
bad += 1
|
||||
if bad == 1 then
|
||||
firstBad = string.format("frame %d (shown %d, %s)", i, shown, if src.keyFrame then "key" else "inter")
|
||||
end
|
||||
end
|
||||
else
|
||||
hidden += 1
|
||||
end
|
||||
end
|
||||
t:check(ok and bad == 0 and shown == #c.crcs, string.format("%s: %d / %d frames bit-exact, %d shown of %d; first bad: %s", c.name, shown - bad, #c.crcs, shown, #fr, firstBad))
|
||||
total += shown
|
||||
if string.sub(c.name, 1, 5) == "vp80-" then
|
||||
vectors += 1
|
||||
end
|
||||
if #samples < 6 and #c.ivf < 20000 then
|
||||
table.insert(samples, b)
|
||||
end
|
||||
end
|
||||
t:note("%d streams (%d official test vectors), %d shown frames bit-exact with ffmpeg, %d hidden frames", #Cases, vectors, total, hidden)
|
||||
|
||||
-- hidden frames, reset(), inter frames without a key frame
|
||||
do
|
||||
local c
|
||||
for _, x in Cases do
|
||||
if x.name == "altref_352x288" then
|
||||
c = x
|
||||
end
|
||||
end
|
||||
local b = buffer.fromstring(c.ivf)
|
||||
local fr = ivfFrames(b)
|
||||
local dec = Vp8.newDecoder()
|
||||
local _, e0 = dec:decodeFrame(b, fr[2][1], fr[2][2])
|
||||
t:check(e0 and e0.code == Err.CORRUPT, "inter frame first: " .. tostring(e0))
|
||||
local key = dec:decodeFrame(b, fr[1][1], fr[1][2]) :: any
|
||||
t:check(key and key.meta.source.keyFrame and key.meta.source.show and key.meta.source.format == "vp8", "key frame meta")
|
||||
local sawHidden = false
|
||||
for i = 2, #fr do
|
||||
local img = dec:decodeFrame(b, fr[i][1], fr[i][2]) :: any
|
||||
if img and not img.meta.source.show then
|
||||
sawHidden = true
|
||||
t:check(not img.meta.source.keyFrame and img.width == c.width, "hidden frame is an Image")
|
||||
end
|
||||
end
|
||||
t:check(sawHidden, "the alt-ref stream has a hidden frame")
|
||||
-- the returned planes are a copy: decoding more frames leaves an earlier image alone
|
||||
local d2 = Vp8.newDecoder()
|
||||
local first = d2:decodeFrame(b, fr[1][1], fr[1][2]) :: any
|
||||
local crc = Crc32.of(first.pixels)
|
||||
d2:decodeFrame(b, fr[2][1], fr[2][2])
|
||||
d2:decodeFrame(b, fr[3][1], fr[3][2])
|
||||
t:eq(Crc32.of(first.pixels), crc, "images are fresh buffers")
|
||||
dec:reset()
|
||||
local _, e1 = dec:decodeFrame(b, fr[3][1], fr[3][2])
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "inter frame after reset: " .. tostring(e1))
|
||||
local again = dec:decodeFrame(b, fr[1][1], fr[1][2]) :: any
|
||||
t:eq(again and Crc32.of(again.pixels), c.crcs[1], "key frame after reset")
|
||||
local nxt = dec:decodeFrame(b, fr[2][1], fr[2][2]) :: any
|
||||
t:check(nxt ~= nil, "inter frame after the key frame")
|
||||
local _, e2 = Vp8.decodeFrame(b, fr[2][1], fr[2][2])
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "one-shot decodeFrame of an inter frame")
|
||||
local _, e3 = dec:decodeFrame(b, fr[2][1], 2)
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "truncated frame tag: " .. tostring(e3))
|
||||
end
|
||||
|
||||
-- fuzz: whole damaged streams through one decoder (truncated / mutated frames after valid ones)
|
||||
local function feed(b: buffer)
|
||||
local dec = Vp8.newDecoder()
|
||||
local any = false
|
||||
local p, n = 32, buffer.len(b)
|
||||
while p + 12 <= n do
|
||||
local len = math.min(buffer.readu32(b, p), n - p - 12)
|
||||
local img = dec:decodeFrame(b, p + 12, len, { maxPixels = 2 ^ 20 })
|
||||
any = any or img ~= nil
|
||||
p += 12 + len
|
||||
end
|
||||
return any, nil
|
||||
end
|
||||
-- damage aimed at inter frames: one frame truncated, or bytes changed in its headers / modes (first 48 bytes)
|
||||
local damaged: { buffer } = {}
|
||||
local rand = T.rng(8)
|
||||
for _, b in samples do
|
||||
local fr = ivfFrames(b)
|
||||
for _ = 1, 12 do
|
||||
local k = 2 + rand(#fr - 1)
|
||||
local parts = {}
|
||||
for i, f in fr do
|
||||
local data = buffer.readstring(b, f[1], f[2])
|
||||
if i == k then
|
||||
if rand(2) == 0 then
|
||||
data = string.sub(data, 1, rand(#data))
|
||||
else
|
||||
for _ = 1, 1 + rand(3) do
|
||||
local at = 1 + rand(math.min(48, #data))
|
||||
data = string.sub(data, 1, at - 1) .. string.char(rand(256)) .. string.sub(data, at + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local hdr = buffer.create(12)
|
||||
buffer.writeu32(hdr, 0, #data)
|
||||
table.insert(parts, buffer.tostring(hdr) .. data)
|
||||
end
|
||||
table.insert(damaged, buffer.fromstring(buffer.readstring(b, 0, 32) .. table.concat(parts)))
|
||||
end
|
||||
end
|
||||
local throws = 0
|
||||
for _, b in damaged do
|
||||
if not pcall(feed, b) then
|
||||
throws += 1
|
||||
end
|
||||
end
|
||||
t:eq(throws, 0, #damaged .. " streams with one damaged inter frame: no throw")
|
||||
t:fuzz("stream", feed, samples, { mutations = 5, keepHeader = 32 })
|
||||
t:fuzz("stream (more damage)", feed, samples, { mutations = 15, keepHeader = 32, garbage = 20 })
|
||||
|
||||
t:done()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,221 @@
|
||||
-- tests/video/_common: shared checks for the video container tests (mkv, avi, y4m, mpegts, mpegps, mp4): a demuxer's
|
||||
-- tracks and packets against ffprobe's, decoded frames against ffmpeg's (CRC-32 for bit-exact codecs, PSNR for the
|
||||
-- others), audio through the api's hook against ffmpeg's f32 decode, seeking. Not a test by itself.
|
||||
local F = require("@formats")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Video = require("@formats/codecs/Video")
|
||||
|
||||
local C = {}
|
||||
|
||||
-- the codecs whose decode must equal ffmpeg's bit for bit
|
||||
C.EXACT = { vp8 = true, rawvideo = true }
|
||||
|
||||
-- psnr(a, b, off?, n?) -> dB between two byte strings / buffers (math.huge when equal)
|
||||
function C.psnr(a: buffer, b: buffer, n: number?): number
|
||||
local len = n or math.min(buffer.len(a), buffer.len(b))
|
||||
local se = 0
|
||||
for i = 0, len - 1 do
|
||||
local d = buffer.readu8(a, i) - buffer.readu8(b, i)
|
||||
se += d * d
|
||||
end
|
||||
if se == 0 then
|
||||
return math.huge
|
||||
end
|
||||
return 10 * math.log10(255 * 255 * len / se)
|
||||
end
|
||||
|
||||
-- snr(ours f32 buffer, reference f32 string, n samples) -> dB
|
||||
function C.snr(ours: buffer, ref: string, n: number): number
|
||||
local rb = buffer.fromstring(ref)
|
||||
local sig, err = 0, 0
|
||||
for i = 0, n - 1 do
|
||||
local r = buffer.readf32(rb, i * 4)
|
||||
local d = buffer.readf32(ours, i * 4) - r
|
||||
sig += r * r
|
||||
err += d * d
|
||||
end
|
||||
if err == 0 then
|
||||
return math.huge
|
||||
end
|
||||
return 10 * math.log10(sig / err)
|
||||
end
|
||||
|
||||
-- packets(t, dmx, c, label, opts?): every track's packets against ffprobe's (in order): size, CRC, time, keyframe.
|
||||
-- opts.skipStream(ffprobePacket, track) -> true skips a stream (ffmpeg's parsers re-split some, e.g. MPEG audio in TS)
|
||||
function C.packets(t: any, dmx: any, c: any, label: string, opts: any?)
|
||||
local o = opts or {}
|
||||
local trackOf = {} -- ffprobe stream index -> our track
|
||||
for i, s in c.streams do
|
||||
trackOf[s.index] = o.trackFor and o.trackFor(dmx, s) or dmx.tracks[i]
|
||||
end
|
||||
local k: { [number]: number } = {}
|
||||
local total, bad = 0, 0
|
||||
local first = nil
|
||||
for _, want in c.packets do
|
||||
local tr = trackOf[want.stream]
|
||||
if not (o.skipStream and o.skipStream(want, tr)) then
|
||||
total += 1
|
||||
local id = if tr then tr.id else -1
|
||||
k[id] = (k[id] or 0) + 1
|
||||
local p = if tr then dmx:get(id, k[id]) else nil
|
||||
local ok = p ~= nil and buffer.len(p.data) == want.size and Crc32.of(p.data) == want.crc
|
||||
local s = c.streams[want.stream + 1]
|
||||
if ok and p and want.pts and s and not o.noPts then
|
||||
local ours = p.pts / tr.timescale - (tr.codecDelay or 0) -- ffmpeg shifts by Matroska's CodecDelay
|
||||
local theirs = want.pts * s.tbn / s.tbd
|
||||
ok = math.abs(ours - theirs) < 0.0015
|
||||
end
|
||||
if ok and p and not o.noKey and tr and tr.kind == "video" then
|
||||
ok = p.keyframe == want.key
|
||||
end
|
||||
if not ok then
|
||||
bad += 1
|
||||
if not first then
|
||||
first = string.format("track %s packet %d: size %s/%d pts %s/%s key %s/%s crc %s", tostring(id), k[id], tostring(p and buffer.len(p.data)), want.size, tostring(p and p.pts), tostring(want.pts), tostring(p and p.keyframe), tostring(want.key), tostring(p and Crc32.of(p.data) == want.crc))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local extra = 0
|
||||
for id, n in k do
|
||||
extra += math.max(0, dmx:count(id) - n)
|
||||
end
|
||||
t:check(bad == 0, label .. ": " .. total .. " packets equal ffprobe's (" .. bad .. " differ" .. (if first then "; first: " .. first else "") .. ")")
|
||||
t:check(extra == 0, label .. ": no extra packets (" .. extra .. ")")
|
||||
end
|
||||
|
||||
-- frames(t, v, c, label): every decoded frame against ffmpeg's (CRC for EXACT codecs, else PSNR of the raw ones)
|
||||
function C.frames(t: any, v: any, c: any, label: string, minPsnr: number?)
|
||||
if not c.frames then
|
||||
return
|
||||
end
|
||||
if not Video.canDecode(v.codec) then
|
||||
t:note("%s: no %s decoder, frames not checked", label, v.codec)
|
||||
return
|
||||
end
|
||||
v:reset()
|
||||
local n, bad, worst = 0, 0, math.huge
|
||||
local firstBad = nil
|
||||
while true do
|
||||
local f, err = v:nextFrame()
|
||||
if not f then
|
||||
t:check(err == nil, label .. ": decode error " .. tostring(err))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
local img = f.image
|
||||
if img.format ~= "yuv420p" then
|
||||
t:check(false, label .. ": frame " .. n .. " is " .. img.format)
|
||||
break
|
||||
end
|
||||
if C.EXACT[v.codec] then
|
||||
if Crc32.of(img.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
firstBad = firstBad or n
|
||||
end
|
||||
elseif c.yuv and c.yuv[n] then
|
||||
local p = C.psnr(img.pixels, buffer.fromstring(c.yuv[n]))
|
||||
worst = math.min(worst, p)
|
||||
end
|
||||
end
|
||||
t:eq(n, #c.frames, label .. ": frame count")
|
||||
if C.EXACT[v.codec] then
|
||||
t:check(bad == 0, label .. ": every frame bit-exact with ffmpeg (" .. bad .. " differ, first " .. tostring(firstBad) .. ")")
|
||||
elseif c.yuv then
|
||||
t:check(worst >= (minPsnr or 38), string.format("%s: PSNR vs ffmpeg %.1f dB", label, worst))
|
||||
end
|
||||
end
|
||||
|
||||
-- audio(t, v, c, label, exact?): the audio decoder (through the api hook) against ffmpeg's f32
|
||||
function C.audio(t: any, v: any, c: any, label: string, exact: boolean?, minSnr: number?)
|
||||
if not c.pcm then
|
||||
return
|
||||
end
|
||||
if not v.audio then
|
||||
local code = v.audioError and v.audioError.code
|
||||
t:check(code == F.Err.UNKNOWN_FORMAT or code == F.Err.UNSUPPORTED, label .. ": audio missing only for a missing audio format (" .. tostring(v.audioError) .. ")")
|
||||
return
|
||||
end
|
||||
local a = v.audio
|
||||
a:seek(0)
|
||||
local want = #c.pcm // 4
|
||||
local out = buffer.create(want * 4)
|
||||
local got = 0
|
||||
while got * a.channels < want do
|
||||
local chunk, k = a:read(want // a.channels - got)
|
||||
if not chunk or k <= 0 then
|
||||
break
|
||||
end
|
||||
buffer.copy(out, got * a.channels * 4, chunk, 0, k * a.channels * 4)
|
||||
got += k
|
||||
end
|
||||
local n = got * a.channels
|
||||
t:check(n == want or n * 4 == c.pcmBytes, label .. ": " .. n .. " samples read of " .. want)
|
||||
n = math.min(n, want)
|
||||
if exact then
|
||||
local same = true
|
||||
local rb = buffer.fromstring(c.pcm)
|
||||
for i = 0, n - 1 do
|
||||
if math.abs(buffer.readf32(out, i * 4) - buffer.readf32(rb, i * 4)) > 1e-6 then
|
||||
same = false
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(same, label .. ": PCM equal to ffmpeg's")
|
||||
else
|
||||
local s = C.snr(out, c.pcm, n)
|
||||
t:check(s >= (minSnr or 30), string.format("%s: audio SNR vs ffmpeg %.1f dB", label, s))
|
||||
end
|
||||
end
|
||||
|
||||
-- seek(t, v, c, label, at): after seek(at) the next frame is the first at or after `at`
|
||||
function C.seek(t: any, v: any, c: any, label: string, at: number)
|
||||
if not (c.frames or c.rgba) or not Video.canDecode(v.codec) then
|
||||
return
|
||||
end
|
||||
if v.duration > 0 and at > v.duration * 0.6 then
|
||||
at = math.floor(v.duration * 50) / 100
|
||||
end
|
||||
v:seek(at)
|
||||
local f = v:nextFrame()
|
||||
t:check(f ~= nil and f.pts >= at - 1e-6 and f.pts < at + 0.2, label .. ": seek to " .. at .. " lands at " .. tostring(f and f.pts))
|
||||
if f and C.EXACT[v.codec] and c.frames then
|
||||
local idx = math.floor(f.pts * v.frameRate + 0.5) + 1
|
||||
t:eq(Crc32.of(f.image.pixels), c.frames[idx], label .. ": the frame after seeking equals ffmpeg's frame " .. idx)
|
||||
end
|
||||
v:reset()
|
||||
end
|
||||
|
||||
-- fuzzOpen(Fmt) -> a fuzz function: open, read every packet, decode up to 8 frames, read some audio
|
||||
function C.fuzzOpen(Fmt: any): (buffer) -> any
|
||||
return function(b: buffer): any
|
||||
local v = Fmt.open(Bytes.reader(b), { openAudio = function(d: any, tr: any): any
|
||||
local AudioWrap = require("@formats/containers/AudioWrap")
|
||||
AudioWrap.wrap(d, tr)
|
||||
return nil
|
||||
end })
|
||||
if not v then
|
||||
return nil
|
||||
end
|
||||
for _ = 1, 8 do
|
||||
local f = v:nextFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
end
|
||||
if v.demuxer then
|
||||
v.demuxer:rewind()
|
||||
while v.demuxer:read() do
|
||||
end
|
||||
end
|
||||
return v
|
||||
end
|
||||
end
|
||||
|
||||
-- open(data, opts?) -> VideoDecoder through the api (category video)
|
||||
function C.open(data: string, opts: any?): (any, any)
|
||||
return F.video.open(buffer.fromstring(data), opts)
|
||||
end
|
||||
|
||||
return C
|
||||
@@ -0,0 +1,97 @@
|
||||
-- video/avi tests: AVI files from ffmpeg (MJPEG + PCM s16 / u8 / IMA ADPCM, MPEG-1 + MP2, uncompressed BGR24 / BGRA /
|
||||
-- I420) read through the OpenDML indexes, idx1 alone, and a movi scan: tracks and every packet against ffprobe,
|
||||
-- frames against ffmpeg, audio through the api hook, seeking; our muxer (MJPEG + PCM) read back; a GIF converted to
|
||||
-- AVI. Fixtures: tools/fixtures/avi.py.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local F = require("@formats")
|
||||
local AviFormat = require("@formats/video/avi")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Fx = require("@tests/gen/avi_cases")
|
||||
|
||||
local t = T.suite("avi")
|
||||
|
||||
t:eq(AviFormat.name, "avi", "name")
|
||||
t:eq(AviFormat.category, "video", "category")
|
||||
|
||||
local EXACT_AUDIO = { pcm_s16le = true, pcm_u8 = true, adpcm_ima_wav = true }
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(AviFormat.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, label .. ": sniff")
|
||||
local v, err = C.open(c.data)
|
||||
if c.truncated then
|
||||
t:check(v ~= nil or err ~= nil, label .. ": opens or fails cleanly")
|
||||
else
|
||||
t:check(v ~= nil, label .. ": open: " .. tostring(err))
|
||||
if v then
|
||||
local dmx = v.demuxer
|
||||
t:eq(#dmx.tracks, #c.streams, label .. ": track count")
|
||||
C.packets(t, dmx, c, label)
|
||||
if c.rgba then
|
||||
v:reset()
|
||||
local n, bad = 0, 0
|
||||
while true do
|
||||
local f = v:nextFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
if f.image.format ~= "rgba8" or Crc32.of(f.image.pixels) ~= c.rgba[n] then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:check(n == #c.rgba and bad == 0, label .. ": " .. n .. " rgba frames equal ffmpeg's (" .. bad .. " differ)")
|
||||
else
|
||||
C.frames(t, v, c, label)
|
||||
end
|
||||
local at = dmx:track("audio")
|
||||
if at then
|
||||
C.audio(t, v, c, label, EXACT_AUDIO[at.codec], 30)
|
||||
end
|
||||
C.seek(t, v, c, label, 0.5)
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(C.open(byName["mjpeg_pcm.avi"].data).demuxer.meta.index, "idx1", "ffmpeg's AVI: idx1 used")
|
||||
t:eq(C.open(byName["odml.avi"].data).demuxer.meta.index, "odml", "OpenDML indexes used")
|
||||
t:eq(C.open(byName["idx1_only.avi"].data).demuxer.meta.index, "idx1", "idx1 used without OpenDML indexes")
|
||||
t:eq(C.open(byName["no_index.avi"].data).demuxer.meta.index, "scan", "movi scanned without indexes")
|
||||
|
||||
-- our muxer: MJPEG copied + PCM, read back packet for packet
|
||||
do
|
||||
local v = C.open(byName["mjpeg_pcm.avi"].data)
|
||||
local out, err = F.save(v, nil, { format = "avi" })
|
||||
t:check(out ~= nil, "mux avi: " .. tostring(err))
|
||||
if out then
|
||||
local v2 = F.video.open(out)
|
||||
t:check(v2 ~= nil, "mux avi: reads back")
|
||||
if v2 then
|
||||
local a, b = v.demuxer, v2.demuxer
|
||||
local same = a:count(1) == b:count(1)
|
||||
for k = 1, a:count(1) do
|
||||
local p, q = a:get(1, k), b:get(1, k)
|
||||
if not q or buffer.tostring(p.data) ~= buffer.tostring(q.data) then
|
||||
same = false
|
||||
end
|
||||
end
|
||||
t:check(same, "mux avi: identical MJPEG packets")
|
||||
t:eq(v2.frameRate, 10, "mux avi: frame rate")
|
||||
C.audio(t, v2, byName["mjpeg_pcm.avi"], "mux avi", true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
local samples = {}
|
||||
for _, n in { "mjpeg_pcm.avi", "mjpeg_ima.avi", "idx1_only.avi", "i420.avi" } do
|
||||
local d = byName[n].data
|
||||
table.insert(samples, string.sub(d, 1, math.min(#d, 40000)))
|
||||
end
|
||||
t:fuzz("open", C.fuzzOpen(AviFormat), samples, { mutations = 6, garbage = 100 })
|
||||
t:done()
|
||||
@@ -0,0 +1,116 @@
|
||||
-- video writer tests: files our muxers wrote (tools/fixtures/_video_encode.luau: WebM remux, MJPEG Matroska / AVI
|
||||
-- copied and re-encoded from VP8 / MPEG-1, an animated GIF with uneven delays to AVI / Matroska / Y4M, WebM to Y4M)
|
||||
-- were probed by ffprobe and decoded by ffmpeg at fixture time; here our demuxers must see the same packets, our
|
||||
-- decoders the same frames (CRC for VP8 / raw, PSNR for MJPEG) and audio; the writers are rerun and must give the same
|
||||
-- bytes. Fixtures: tools/fixtures/videoenc.py.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local F = require("@formats")
|
||||
local Fx = require("@tests/gen/videoenc_cases")
|
||||
local Src = require("@tests/gen/videoenc_src")
|
||||
|
||||
local t = T.suite("video encode")
|
||||
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
if c.err then
|
||||
t:note("%s: not written at fixture time: %s", label, c.err)
|
||||
else
|
||||
local v, err = C.open(c.data)
|
||||
t:check(v ~= nil, label .. ": opens: " .. tostring(err))
|
||||
if v then
|
||||
t:eq(#v.demuxer.tracks, #c.streams, label .. ": track count (ffprobe)")
|
||||
-- (ffmpeg's AVI demuxer cuts PCM chunks into 1024-sample packets: AVI audio is compared decoded)
|
||||
local avi = string.sub(label, -4) == ".avi"
|
||||
C.packets(t, v.demuxer, c, label, { skipStream = function(_: any, tr: any): boolean
|
||||
return avi and tr ~= nil and tr.kind == "audio"
|
||||
end })
|
||||
C.frames(t, v, c, label, 40)
|
||||
local at = v.demuxer:track("audio")
|
||||
if at then
|
||||
C.audio(t, v, c, label, string.sub(at.codec, 1, 4) == "pcm_", 30)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the GIF (100 / 50 / 200 / 100 ms) at a constant 20 fps: 2 + 1 + 4 + 2 frames
|
||||
do
|
||||
local c = byName["gif.avi"]
|
||||
if c and c.frames then
|
||||
t:eq(#c.frames, 9, "gif.avi: 9 frames at 20 fps (ffmpeg)")
|
||||
local v = C.open(c.data)
|
||||
t:eq(v.frameRate, 20, "gif.avi: 20 fps")
|
||||
end
|
||||
local y = byName["gif.y4m"]
|
||||
if y and y.frames then
|
||||
t:eq(#y.frames, 9, "gif.y4m: 9 frames (ffmpeg)")
|
||||
end
|
||||
-- F.video.open on the GIF itself: a frames-backed video
|
||||
local g = F.video.open(buffer.fromstring(Src.gif))
|
||||
t:check(g ~= nil and g.codec == "frames" and g.frames == 4, "F.video.open(gif): 4 frames")
|
||||
if g then
|
||||
local f1 = g:nextFrame()
|
||||
local f2 = g:nextFrame()
|
||||
t:check(f1 and f1.image.format == "rgba8" and f1.duration == 0.1 and f2 and f2.pts == 0.1 and f2.duration == 0.05, "gif frames: rgba8 with the GIF's times")
|
||||
g:seek(0.2)
|
||||
local f3 = g:nextFrame()
|
||||
t:check(f3 ~= nil and f3.index == 3, "gif seek: frame 3 at 0.15 s holds 0.2 s")
|
||||
end
|
||||
end
|
||||
|
||||
-- animated WebP through F.video.open: frames-backed, the canvases equal Pillow's (libwebp's WebPAnimDecoder)
|
||||
do
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Anims = require("@tests/gen/webp_anim")
|
||||
for _, a in Anims do
|
||||
local v = F.video.open(buffer.fromstring(a.data))
|
||||
t:check(v ~= nil and v.codec == "frames" and v.frames == #a.frames, a.name .. ": opens as a " .. #a.frames .. "-frame video")
|
||||
if v then
|
||||
local bad, n, t0 = 0, 0, 0
|
||||
while true do
|
||||
local f = v:nextFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
local px = f.image.pixels
|
||||
if a.rgba == false then
|
||||
px = buffer.create(buffer.len(f.image.pixels))
|
||||
buffer.copy(px, 0, f.image.pixels)
|
||||
for i = 3, buffer.len(px) - 1, 4 do
|
||||
buffer.writeu8(px, i, 255)
|
||||
end
|
||||
end
|
||||
if Crc32.of(px) ~= a.frames[n].crc or math.abs(f.pts - t0) > 1e-6 then
|
||||
bad += 1
|
||||
end
|
||||
t0 += a.frames[n].ms / 1000
|
||||
end
|
||||
t:check(n == #a.frames and bad == 0, a.name .. ": video frames equal Pillow's canvases and times (" .. bad .. " differ)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the writers are deterministic: the same bytes again
|
||||
do
|
||||
local Mkv = require("@tests/gen/mkv_cases")
|
||||
local remux = byName["remux.webm"]
|
||||
for _, m in Mkv do
|
||||
if m.name == "vp8_opus.webm" and remux and remux.data then
|
||||
local out = F.convert(buffer.fromstring(m.data), "webm")
|
||||
t:check(out ~= nil and buffer.tostring(out) == remux.data, "remux.webm: written again byte for byte")
|
||||
end
|
||||
end
|
||||
local gifAvi = byName["gif.avi"]
|
||||
if gifAvi and gifAvi.data then
|
||||
local out = F.convert(buffer.fromstring(Src.gif), "avi", { quality = 95 })
|
||||
t:check(out ~= nil and buffer.tostring(out) == gifAvi.data, "gif.avi: F.convert(gif, 'avi') again byte for byte")
|
||||
end
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,128 @@
|
||||
-- video/mkv tests: Matroska / WebM files from ffmpeg and mkvmerge (VP8 + Opus / Vorbis, MJPEG + PCM, MPEG-1 + MP2,
|
||||
-- VP8 + FLAC, srt subtitles, Xiph / EBML / fixed lacing, zlib compression, unknown-size clusters, a truncated file):
|
||||
-- tracks and every packet against ffprobe, frames against ffmpeg (bit-exact VP8, PSNR for MJPEG / MPEG-1), audio
|
||||
-- through the api hook against ffmpeg, seeking, subtitles; our muxer's output (remux and MJPEG) read back. Fixtures:
|
||||
-- tools/fixtures/mkv.py.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local F = require("@formats")
|
||||
local Mkv = require("@formats/video/mkv")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/mkv_cases")
|
||||
|
||||
local t = T.suite("mkv")
|
||||
|
||||
-- 1. contract
|
||||
t:eq(Mkv.name, "mkv", "name")
|
||||
t:eq(Mkv.category, "video", "category")
|
||||
t:check(table.find(Mkv.extensions, "webm") ~= nil, "webm extension")
|
||||
|
||||
local EXACT_AUDIO = { pcm_s16le = true, flac = true }
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
|
||||
-- 2. every fixture
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Mkv.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, label .. ": sniff")
|
||||
if c.truncated then
|
||||
local v, err = C.open(c.data)
|
||||
t:check(v ~= nil or err ~= nil, label .. ": opens or fails cleanly")
|
||||
if v then
|
||||
local n = 0
|
||||
while v:nextFrame() do
|
||||
n += 1
|
||||
end
|
||||
t:note("%s: %d frames before the cut", label, n)
|
||||
end
|
||||
else
|
||||
local v, err = C.open(c.data)
|
||||
t:check(v ~= nil, label .. ": open: " .. tostring(err))
|
||||
if v then
|
||||
local dmx = v.demuxer
|
||||
t:eq(#dmx.tracks, #c.streams, label .. ": track count")
|
||||
for i, s in c.streams do
|
||||
local tr = dmx.tracks[i]
|
||||
if tr then
|
||||
t:eq(tr.kind, s.kind, label .. ": track " .. i .. " kind")
|
||||
if s.width then
|
||||
t:check(tr.width == s.width and tr.height == s.height, label .. ": track " .. i .. " size")
|
||||
end
|
||||
if s.rate then
|
||||
t:check(tr.sampleRate == s.rate and tr.channels == s.channels, label .. ": track " .. i .. " rate / channels")
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Vorbis frames inside a lace carry no time of their own (ffmpeg derives them from the Vorbis block sizes)
|
||||
C.packets(t, dmx, c, label, { noPts = c.name == "laced_vorbis.mkv" or c.name == "zlib_vorbis.mka" })
|
||||
C.frames(t, v, c, label)
|
||||
local at = dmx:track("audio")
|
||||
if at then
|
||||
C.audio(t, v, c, label, EXACT_AUDIO[at.codec], if at.codec == "vorbis" then 20 else 30)
|
||||
end
|
||||
C.seek(t, v, c, label, 0.6)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 3. specific files
|
||||
do
|
||||
local v = C.open(byName["vp8_opus.webm"].data)
|
||||
t:eq(v.format, "webm", "webm: format")
|
||||
t:eq(v.codec, "vp8", "webm: codec")
|
||||
t:eq(v.width, 160, "webm: width")
|
||||
t:check(math.abs(v.duration - 1) < 0.1, "webm: duration " .. v.duration)
|
||||
t:check(#v.demuxer.meta.cues > 0, "webm: cues read")
|
||||
local subs = Mkv.subtitles(C.open(byName["subs.mkv"].data).demuxer)
|
||||
t:check(subs ~= nil and #subs.cues == 2, "subtitles: two cues")
|
||||
if subs then
|
||||
t:eq(subs.cues[1].text, "Hello", "subtitles: text")
|
||||
t:check(math.abs(subs.cues[1].start - 0.1) < 0.002 and math.abs(subs.cues[1].stop - 0.5) < 0.002, "subtitles: times")
|
||||
end
|
||||
local lv = C.open(byName["lacing.mka"].data)
|
||||
t:eq(lv.demuxer:count(1), 10, "lacing: 10 frames from three laced blocks")
|
||||
local _, _, _, _, len = lv.demuxer:info(1, 4)
|
||||
t:eq(len, 160, "lacing: EBML-laced frame size")
|
||||
end
|
||||
|
||||
-- 4. our muxer: remux WebM (VP8 + Opus copied) and read it back packet for packet
|
||||
do
|
||||
local src = byName["vp8_opus.webm"]
|
||||
local v = C.open(src.data)
|
||||
local out, err = F.save(v, nil, { format = "webm" })
|
||||
t:check(out ~= nil, "remux webm: " .. tostring(err))
|
||||
if out then
|
||||
local v2 = F.video.open(out)
|
||||
t:check(v2 ~= nil and v2.format == "webm", "remux webm: reads back as webm")
|
||||
if v2 then
|
||||
local a, b = v.demuxer, v2.demuxer
|
||||
t:eq(#b.tracks, 2, "remux webm: two tracks")
|
||||
local same = a:count(1) == b:count(1) and a:count(2) == b:count(2)
|
||||
for k = 1, a:count(1) do
|
||||
local p, q = a:get(1, k), b:get(1, k)
|
||||
if not q or p.pts ~= q.pts or buffer.tostring(p.data) ~= buffer.tostring(q.data) or p.keyframe ~= q.keyframe then
|
||||
same = false
|
||||
end
|
||||
end
|
||||
t:check(same, "remux webm: identical video packets")
|
||||
C.frames(t, v2, src, "remux webm")
|
||||
C.audio(t, v2, src, "remux webm", false, 30)
|
||||
end
|
||||
end
|
||||
local bad, berr = F.save(C.open(byName["mjpeg_pcm.mkv"].data), nil, { format = "webm" })
|
||||
t:check(bad == nil and berr ~= nil and berr.code == Err.UNSUPPORTED, "webm refuses MJPEG: " .. tostring(berr))
|
||||
end
|
||||
|
||||
-- 5. errors and the fuzz pass
|
||||
local e1 = select(2, Mkv.open(Bytes.reader(buffer.fromstring("\26\69\223\163\128"))))
|
||||
t:check(e1 ~= nil, "empty EBML header fails")
|
||||
local samples = {}
|
||||
for _, n in { "vp8_opus.webm", "mjpeg_pcm.mkv", "lacing.mka", "zlib_vorbis.mka", "subs.mkv" } do
|
||||
local d = byName[n].data
|
||||
table.insert(samples, string.sub(d, 1, math.min(#d, 60000)))
|
||||
end
|
||||
t:fuzz("open", C.fuzzOpen(Mkv), samples, { mutations = 6, garbage = 100 })
|
||||
t:done()
|
||||
@@ -0,0 +1,74 @@
|
||||
-- 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.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local F = require("@formats")
|
||||
local Mp4 = require("@formats/video/mp4")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Fx = require("@tests/gen/mp4_cases")
|
||||
|
||||
local t = T.suite("mp4")
|
||||
t:eq(Mp4.name, "mp4", "name")
|
||||
|
||||
local EXACT_AUDIO = { pcm_s16le = true, flac = true }
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Mp4.sniff(Bytes.reader(buffer.fromstring(c.data))) >= 0.6, label .. ": sniff")
|
||||
local v, err = C.open(c.data)
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
local dmx = v.demuxer
|
||||
t:eq(#dmx.tracks, #c.streams, label .. ": track count")
|
||||
C.packets(t, dmx, c, label)
|
||||
C.frames(t, v, c, label, 38)
|
||||
local at = dmx:track("audio")
|
||||
if at then
|
||||
C.audio(t, v, c, label, EXACT_AUDIO[at.codec], 30)
|
||||
end
|
||||
C.seek(t, v, c, label, 0.5)
|
||||
end
|
||||
end
|
||||
|
||||
-- our writer: MP4 (MJPEG + AAC copied) and MOV (MJPEG + PCM) read back packet for packet
|
||||
for _, job in { { "mjpeg_aac.mp4", "mp4" }, { "mjpeg_pcm.mov", "mov" }, { "h264_aac.mp4", "mp4" } } do
|
||||
local src = byName[job[1]]
|
||||
local v = C.open(src.data)
|
||||
local out, err = F.save(v, nil, { format = job[2] })
|
||||
t:check(out ~= nil, "write " .. job[2] .. " from " .. job[1] .. ": " .. tostring(err))
|
||||
if out then
|
||||
local v2 = F.video.open(out)
|
||||
t:check(v2 ~= nil, "write " .. job[1] .. ": reads back")
|
||||
if v2 then
|
||||
local a, b = v.demuxer, v2.demuxer
|
||||
local same = #a.tracks == #b.tracks
|
||||
for i, tr in a.tracks do
|
||||
local tr2 = b.tracks[i]
|
||||
if not tr2 or a:count(tr.id) ~= b:count(tr2.id) then
|
||||
same = false
|
||||
else
|
||||
for k = 1, a:count(tr.id) do
|
||||
local p, q = a:get(tr.id, k), b:get(tr2.id, k)
|
||||
if not q or buffer.tostring(p.data) ~= buffer.tostring(q.data) or math.abs(p.pts / tr.timescale - q.pts / tr2.timescale) > 0.002 then
|
||||
same = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(same, "write " .. job[1] .. ": same packets and times")
|
||||
end
|
||||
end
|
||||
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))
|
||||
end
|
||||
t:fuzz("open", C.fuzzOpen(Mp4), samples, { mutations = 6, garbage = 100 })
|
||||
t:done()
|
||||
@@ -0,0 +1,48 @@
|
||||
-- video/mpegps tests: program streams from ffmpeg (MPEG-1 system stream, B-frames, MPEG-2 PS / VOB packs, a raw .m1v
|
||||
-- elementary stream): tracks, picture packets (count and timestamps against ffmpeg's frames), frames against ffmpeg
|
||||
-- (PSNR) once an MPEG-1 decoder is registered, audio through the api hook (SNR), seeking, fuzz. Fixtures:
|
||||
-- tools/fixtures/mpegts.py.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local Ps = require("@formats/video/mpegps")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Fx = require("@tests/gen/mpegps_cases")
|
||||
|
||||
local t = T.suite("mpegps")
|
||||
t:eq(Ps.name, "mpegps", "name")
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Ps.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, label .. ": sniff")
|
||||
local v, err = C.open(c.data)
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
local dmx = v.demuxer
|
||||
local vt = dmx:track("video")
|
||||
t:check(vt ~= nil and vt.codec == "mpeg1", label .. ": an MPEG-1 video track")
|
||||
t:eq(dmx:count(vt.id), #c.frames, label .. ": one packet per picture")
|
||||
-- presentation times: one per frame slot
|
||||
local seen, ok = {}, true
|
||||
for k = 1, dmx:count(vt.id) do
|
||||
local pts = dmx:info(vt.id, k)
|
||||
local slot = math.floor((pts - dmx:info(vt.id, 1)) / 3600 + 0.5)
|
||||
if seen[slot] then
|
||||
ok = false
|
||||
end
|
||||
seen[slot] = true
|
||||
end
|
||||
t:check(ok, label .. ": distinct presentation times")
|
||||
C.frames(t, v, c, label, 36)
|
||||
if dmx:track("audio") then
|
||||
C.audio(t, v, c, label, false, 30)
|
||||
end
|
||||
C.seek(t, v, c, label, 0.5)
|
||||
end
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, string.sub(c.data, 1, 30000))
|
||||
end
|
||||
t:fuzz("open", C.fuzzOpen(Ps), samples, { mutations = 6, garbage = 100 })
|
||||
t:done()
|
||||
@@ -0,0 +1,64 @@
|
||||
-- video/mpegts tests: transport streams from ffmpeg (MPEG-1 + MP2, B-frames, H.264 + AAC, M2TS, garbage before the
|
||||
-- first packet, a truncated file): tracks and the video packets against ffprobe (time, size, CRC, keyframe), frames
|
||||
-- against ffmpeg (PSNR) once an MPEG-1 decoder is registered, audio through the api hook (SNR), seeking, fuzz.
|
||||
-- Fixtures: tools/fixtures/mpegts.py.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local Ts = require("@formats/video/mpegts")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Fx = require("@tests/gen/mpegts_cases")
|
||||
|
||||
local t = T.suite("mpegts")
|
||||
t:eq(Ts.name, "mpegts", "name")
|
||||
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
|
||||
local function audioStream(want: any, tr: any): boolean
|
||||
return tr == nil or tr.kind ~= "video" -- ffmpeg's parser re-splits PES audio into frames
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
local v, err = C.open(c.data)
|
||||
if c.truncated then
|
||||
t:check(v ~= nil or err ~= nil, label .. ": opens or fails cleanly")
|
||||
else
|
||||
t:check(Ts.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0 or label == "garbage.ts", label .. ": sniff")
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
local dmx = v.demuxer
|
||||
t:eq(#dmx.tracks, #c.streams, label .. ": track count")
|
||||
for i, s in c.streams do
|
||||
local tr = dmx.tracks[i]
|
||||
t:check(tr ~= nil and tr.kind == s.kind, label .. ": track " .. i .. " kind")
|
||||
if tr and s.width and tr.width then -- (H.264 sizes come from its SPS, read by the codec)
|
||||
t:check(tr.width == s.width and tr.height == s.height, label .. ": track " .. i .. " size")
|
||||
end
|
||||
end
|
||||
if c.packets then
|
||||
C.packets(t, dmx, c, label, { skipStream = audioStream })
|
||||
end
|
||||
C.frames(t, v, c, label, 36)
|
||||
if dmx:track("audio") then
|
||||
C.audio(t, v, c, label, false, 30)
|
||||
end
|
||||
C.seek(t, v, c, label, 0.5)
|
||||
end
|
||||
end
|
||||
end
|
||||
do
|
||||
local v = C.open(byName["mpeg1_mp2.ts"].data)
|
||||
t:eq(v.codec, "mpeg1", "stream type 0x02 without a sequence extension is MPEG-1")
|
||||
t:eq(v.demuxer.tracks[2].codec, "mp2", "MPEG audio layer II")
|
||||
t:eq(C.open(byName["h264_aac.ts"].data).codec, "h264", "H.264 track")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, n in { "mpeg1_mp2.ts", "bframes.ts", "h264_aac.ts" } do
|
||||
table.insert(samples, string.sub(byName[n].data, 1, 188 * 120))
|
||||
end
|
||||
t:fuzz("open", C.fuzzOpen(Ts), samples, { mutations = 6, garbage = 100 })
|
||||
t:done()
|
||||
@@ -0,0 +1,103 @@
|
||||
-- video/y4m tests: YUV4MPEG2 streams from ffmpeg (C420jpeg at an odd size, C420mpeg2, C422, C444, Cmono, C420p10)
|
||||
-- against ffmpeg's yuv420p decode (bit-exact for 4:2:0 8-bit, PSNR for the converted layouts), the header fields,
|
||||
-- our writer round trip (and from an rgba frames-backed video), errors and the fuzz pass. Fixtures:
|
||||
-- tools/fixtures/y4m.py.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local F = require("@formats")
|
||||
local Y4m = require("@formats/video/y4m")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/y4m_cases")
|
||||
|
||||
local t = T.suite("y4m")
|
||||
t:eq(Y4m.name, "y4m", "name")
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Y4m.sniff(Bytes.reader(buffer.fromstring(c.data))) == 1, label .. ": sniff")
|
||||
local v, err = Y4m.open(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
t:check(v.width == c.width and v.height == c.height, label .. ": size")
|
||||
t:check(math.abs(v.frameRate - c.rate[1] / c.rate[2]) < 1e-9, label .. ": frame rate")
|
||||
local n, bad, worst = 0, 0, math.huge
|
||||
while true do
|
||||
local f = v:nextFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
if c.exact then
|
||||
if Crc32.of(f.image.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
end
|
||||
elseif c.yuv[n] then
|
||||
worst = math.min(worst, C.psnr(f.image.pixels, buffer.fromstring(c.yuv[n])))
|
||||
end
|
||||
end
|
||||
t:eq(n, #c.frames, label .. ": frame count")
|
||||
if c.exact then
|
||||
t:check(bad == 0, label .. ": frames bit-exact with ffmpeg")
|
||||
else
|
||||
t:check(worst > 30, string.format("%s: PSNR vs ffmpeg %.1f dB", label, worst))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- writer: a y4m stream back to y4m is byte-identical in its frames; header as ffmpeg writes it
|
||||
do
|
||||
local c = Fx[1]
|
||||
local v = F.video.open(buffer.fromstring(c.data))
|
||||
local out, err = F.save(v, nil, { format = "y4m" })
|
||||
t:check(out ~= nil, "write: " .. tostring(err))
|
||||
if out then
|
||||
local s = buffer.tostring(out)
|
||||
t:check(string.find(s, "YUV4MPEG2 W173 H95 F30000:1001 Ip A1:1 C420jpeg XYSCSS=420JPEG\n", 1, true) == 1, "write: header " .. string.sub(s, 1, 60))
|
||||
local v2 = Y4m.open(Bytes.reader(out))
|
||||
local n, same = 0, true
|
||||
while true do
|
||||
local f = v2:nextFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
same = same and Crc32.of(f.image.pixels) == c.frames[n]
|
||||
end
|
||||
t:check(n == #c.frames and same, "write: frames round trip")
|
||||
end
|
||||
-- an rgba frames-backed video (a 2-frame animation) to y4m
|
||||
local a = Types.Image.new(4, 2)
|
||||
buffer.fill(a.pixels, 0, 255)
|
||||
local b = Types.Image.new(4, 2)
|
||||
a.frames = { { image = a, delay = 0.5, x = 0, y = 0, dispose = "none", blend = "source" }, { image = b, delay = 0.5, x = 0, y = 0, dispose = "none", blend = "source" } }
|
||||
local y, yerr = F.save(a, nil, { format = "y4m" })
|
||||
t:check(y ~= nil, "write from frames: " .. tostring(yerr))
|
||||
if y then
|
||||
local v3 = Y4m.open(Bytes.reader(y))
|
||||
local f1 = v3:nextFrame()
|
||||
local f2 = v3:nextFrame()
|
||||
t:check(f1 ~= nil and f2 ~= nil and v3:nextFrame() == nil, "write from frames: 2 frames")
|
||||
t:eq(v3.frameRate, 2, "write from frames: frame rate from the delays")
|
||||
t:eq(buffer.readu8(f1.image.pixels, 0), 235, "write from frames: white -> Y 235 (limited range)")
|
||||
t:eq(buffer.readu8(f2.image.pixels, 0), 16, "write from frames: black -> Y 16")
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
local function code(s: string): string?
|
||||
local _, e = Y4m.open(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("YUV4MPEG2 W0 H0\n"), Err.CORRUPT, "zero size")
|
||||
t:eq(code("YUV4MPEG2 W16 H16 Cfoo\n"), Err.UNSUPPORTED, "unknown colour space")
|
||||
t:eq(code("YUV4MPEG2 W99999999 H99999 C420\n"), Err.CORRUPT, "huge size")
|
||||
t:eq(code("YUV4MPEG2 W16 H16"), Err.TRUNCATED, "no header end")
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, string.sub(c.data, 1, 30000))
|
||||
end
|
||||
t:fuzz("open", C.fuzzOpen(Y4m), samples, { mutations = 6, garbage = 100 })
|
||||
t:done()
|
||||
Reference in new issue
Block a user