236 lines
8.2 KiB
Luau
236 lines
8.2 KiB
Luau
-- codecs/video/H263 tests: H.263, H.263+ and Sorenson Spark streams bit-exact against `ffmpeg -idct int`.
|
|
-- Fixtures: tools/fixtures/h263.py.
|
|
local T = require("@tests/T")
|
|
local H263 = require("@rosetta/codecs/video/H263")
|
|
local Video = require("@rosetta/codecs/video/Video")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Base64 = require("@rosetta/core/Base64")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
|
|
|
local STREAMS = {
|
|
require("@tests/gen/h263_qcif"),
|
|
require("@tests/gen/h263_cif_mandel"),
|
|
require("@tests/gen/h263_sqcif_scroll"),
|
|
require("@tests/gen/h263_qcif_q1"),
|
|
require("@tests/gen/h263_qcif_aq"),
|
|
require("@tests/gen/h263_cif_gob"),
|
|
require("@tests/gen/h263_4cif_gob"),
|
|
require("@tests/gen/h263_plus_custom"),
|
|
require("@tests/gen/h263_plus_odd"),
|
|
require("@tests/gen/h263_plus_umv"),
|
|
require("@tests/gen/h263_plus_slices"),
|
|
require("@tests/gen/h263_plus_slices_umv"),
|
|
require("@tests/gen/h263_flv_320"),
|
|
require("@tests/gen/h263_flv_odd"),
|
|
require("@tests/gen/h263_flv_wide"),
|
|
require("@tests/gen/h263_flv_q1"),
|
|
require("@tests/gen/h263_flv_scroll"),
|
|
require("@tests/gen/h263_flv_disposable"),
|
|
}
|
|
local BAD = require("@tests/gen/h263_bad")
|
|
|
|
local t = T.suite("codecs/h263")
|
|
local rand = T.rng(263)
|
|
|
|
local function b64(s: string): buffer
|
|
return Base64.decode(s) :: buffer
|
|
end
|
|
|
|
local function planeCrc(b: buffer, stride: number, w: number, h: number): number
|
|
local row = buffer.create(w * h)
|
|
for y = 0, h - 1 do
|
|
buffer.copy(row, y * w, b, y * stride, w)
|
|
end
|
|
return Crc32.of(row)
|
|
end
|
|
|
|
local function crcs(f: any): (number, number, number)
|
|
local w, h = f.width, f.height
|
|
local cw, ch = (w + 1) // 2, (h + 1) // 2
|
|
return planeCrc(f.y, f.stride, w, h), planeCrc(f.cb, f.stride // 2, cw, ch), planeCrc(f.cr, f.stride // 2, cw, ch)
|
|
end
|
|
|
|
local function lumaDiff(f: any, ref: buffer): (number, number)
|
|
local n, first = 0, -1
|
|
for y = 0, f.height - 1 do
|
|
for x = 0, f.width - 1 do
|
|
if buffer.readu8(f.y, y * f.stride + x) ~= buffer.readu8(ref, y * f.width + x) then
|
|
n += 1
|
|
if first < 0 then
|
|
first = y * 65536 + x
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return n, first
|
|
end
|
|
|
|
local total = 0
|
|
local disposables = 0
|
|
for _, fx in STREAMS do
|
|
local name = fx.name
|
|
local file = b64(fx.file)
|
|
local dec = H263.new({ flv = fx.flv })
|
|
local t0 = os.clock()
|
|
local bad, n, msg = 0, 0, ""
|
|
for k, u in fx.units do
|
|
local f, err = dec:decode(file, u[1], u[2])
|
|
if not f then
|
|
bad += 1
|
|
if bad == 1 then
|
|
msg = string.format(" (first: picture %d: %s)", k, tostring(err))
|
|
end
|
|
else
|
|
n += 1
|
|
if f.disposable then
|
|
disposables += 1
|
|
end
|
|
local y, cb, cr = crcs(f)
|
|
local ref = fx.crcs[k]
|
|
if not ref or y ~= ref[1] or cb ~= ref[2] or cr ~= ref[3] then
|
|
bad += 1
|
|
if bad == 1 then
|
|
local detail = ""
|
|
if k == 1 then
|
|
local nd, first = lumaDiff(f, Deflate.zlib(b64(fx.first)) :: buffer)
|
|
detail = string.format(", %d luma samples differ, first at %d,%d", nd, first % 65536, first // 65536)
|
|
end
|
|
msg = string.format(" (first: frame %d, type %d: y %s u %s v %s%s)", k, f.pictureType,
|
|
tostring(ref and y == ref[1]), tostring(ref and cb == ref[2]), tostring(ref and cr == ref[3]), detail)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local dt = os.clock() - t0
|
|
t:eq(n, fx.frames, name .. ": frame count")
|
|
t:eq(bad, 0, name .. ": frames differing from ffmpeg" .. msg)
|
|
t:eq(dec.stats.errors, 0, name .. ": no damaged pictures")
|
|
total += n
|
|
t:note(" %-16s %4dx%-4d %3d frames %6.1f fps (with the checks)", name, fx.width, fx.height, n, n / dt)
|
|
end
|
|
t:check(disposables > 0, "disposable Sorenson pictures decoded (" .. disposables .. ")")
|
|
|
|
do
|
|
local fx = STREAMS[8] -- plus_custom
|
|
local file = b64(fx.file)
|
|
local h = assert(H263.header(file, fx.units[1][1], fx.units[1][2]))
|
|
t:check(h.plus and h.width == 320 and h.height == 240 and h.ptype == 1, "plus_custom: PLUSPTYPE custom size, I picture")
|
|
local h2 = assert(H263.header(file, fx.units[2][1], fx.units[2][2]))
|
|
t:check(h2.ptype == 2, "plus_custom: second picture P")
|
|
fx = STREAMS[14] -- flv_odd
|
|
file = b64(fx.file)
|
|
h = assert(H263.header(file, fx.units[1][1], fx.units[1][2], true))
|
|
t:check(h.flv and h.width == 100 and h.height == 75 and h.version == 1, "flv_odd: Sorenson 8-bit custom size, version 1")
|
|
fx = STREAMS[1]
|
|
h = assert(H263.header(b64(fx.file), 0, fx.units[1][2]))
|
|
t:check(not h.plus and h.width == 176 and h.height == 144, "qcif: PTYPE QCIF")
|
|
local _, e = H263.header(buffer.create(16))
|
|
t:check(e ~= nil and e.code == Err.CORRUPT, "no start code: CORRUPT")
|
|
end
|
|
|
|
for name, data in BAD do
|
|
local dec = H263.new()
|
|
local f, e = dec:decode(b64(data))
|
|
t:check(f == nil and e ~= nil and e.code == Err.UNSUPPORTED, name .. ": UNSUPPORTED (" .. tostring(e) .. ")")
|
|
end
|
|
|
|
do
|
|
local fx = STREAMS[14]
|
|
local dec = H263.new({ flv = true })
|
|
local file = b64(fx.file)
|
|
local f = assert(dec:decode(file, fx.units[1][1], fx.units[1][2]))
|
|
local img = assert(H263.frameImage(f))
|
|
t:check(img.format == "yuv420p" and img.width == 100 and img.height == 75, "frameImage: yuv420p 100x75")
|
|
t:eq(buffer.len(img.pixels), 100 * 75 + 2 * 50 * 38, "frameImage: plane sizes")
|
|
t:eq(Crc32.of(img.pixels, 0, 100 * 75), fx.crcs[1][1], "frameImage: luma equals ffmpeg's")
|
|
t:eq(img.meta.yuv.range, "limited", "frameImage: limited range")
|
|
t:eq(img.meta.source.format, "flv1", "frameImage: meta.source.format")
|
|
t:check(H263.frameImage({} :: any) == nil, "frameImage of a non-frame is nil")
|
|
t:check(dec:decode(42 :: any) == nil, "decode of a non-buffer is nil")
|
|
end
|
|
|
|
do
|
|
t:check(Video.fromFourcc("H263") == "h263" and Video.fromFourcc("s263") == "h263" and Video.fromFourcc("FLV1") == "flv1", "registry names")
|
|
t:check(Video.canDecode("h263") and Video.canDecode("flv1"), "registry decoders")
|
|
for _, idx in { 2, 15 } do
|
|
local fx = STREAMS[idx]
|
|
local file = b64(fx.file)
|
|
local dec = assert(Video.decoder({ id = 1, kind = "video", codec = if fx.flv then "flv1" else "h263", timescale = 25, width = fx.width, height = fx.height } :: any))
|
|
local frames, same = 0, true
|
|
for i, u in fx.units do
|
|
local d = buffer.create(u[2])
|
|
buffer.copy(d, 0, file, u[1], u[2])
|
|
local e = dec:decode({ track = 1, pts = i, keyframe = i == 1, data = d } :: any)
|
|
t:check(e == nil, fx.name .. ": registry decode " .. tostring(e))
|
|
while true do
|
|
local img, pts = dec:receive()
|
|
if not img then
|
|
break
|
|
end
|
|
frames += 1
|
|
same = same and pts == i and img.meta.yuv ~= nil and Crc32.of(img.pixels, 0, fx.width * fx.height) == fx.crcs[frames][1]
|
|
end
|
|
end
|
|
t:eq(frames, fx.frames, fx.name .. ": registry frame count")
|
|
t:check(same, fx.name .. ": registry frames equal ffmpeg's")
|
|
dec:reset()
|
|
local p = buffer.create(fx.units[2][2])
|
|
buffer.copy(p, 0, file, fx.units[2][1], fx.units[2][2])
|
|
t:check(dec:decode({ track = 1, pts = 2, keyframe = false, data = p } :: any) ~= nil, fx.name .. ": a P picture after reset is an error")
|
|
end
|
|
end
|
|
|
|
do
|
|
local samples = {}
|
|
for _, idx in { 1, 6, 8, 10, 11, 13, 16 } do
|
|
local fx = STREAMS[idx]
|
|
local file = b64(fx.file)
|
|
for k = 1, 2 do
|
|
local u = fx.units[k]
|
|
local b = buffer.create(u[2])
|
|
buffer.copy(b, 0, file, u[1], u[2])
|
|
table.insert(samples, { b, fx.flv })
|
|
end
|
|
end
|
|
local threw, damaged = 0, 0
|
|
for round = 1, 40 do
|
|
local s = samples[1 + round % #samples]
|
|
local src = s[1]
|
|
local n = if round % 3 == 0 then rand(buffer.len(src)) else buffer.len(src)
|
|
local b = buffer.create(n)
|
|
buffer.copy(b, 0, src, 0, n)
|
|
for _ = 1, 1 + round % 7 do
|
|
local i = 4 + rand(math.max(1, n - 4)) - 1
|
|
if i < n then
|
|
buffer.writeu8(b, i, bit32.bxor(buffer.readu8(b, i), bit32.lshift(1, rand(8) - 1)))
|
|
end
|
|
end
|
|
local ok = pcall(function()
|
|
local dec = H263.new({ flv = s[2] })
|
|
dec:decode(src)
|
|
dec:decode(b)
|
|
dec:decode(b)
|
|
damaged += dec.stats.errors
|
|
end)
|
|
if not ok then
|
|
threw += 1
|
|
end
|
|
end
|
|
t:eq(threw, 0, "corrupted pictures never throw")
|
|
t:check(damaged > 0, "corrupted pictures are counted (" .. damaged .. ")")
|
|
local fuzz = {}
|
|
for _, s in samples do
|
|
table.insert(fuzz, s[1])
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
local dec = H263.new({ flv = buffer.len(b) > 2 and bit32.band(buffer.readu8(b, 2), 0x04) ~= 0 })
|
|
local f = dec:decode(b)
|
|
dec:decode(b)
|
|
return f
|
|
end, fuzz, { mutations = 40, garbage = 20 })
|
|
end
|
|
|
|
t:note(" %d frames bit-exact with ffmpeg -idct int", total)
|
|
t:done()
|