ws-cleanup: merge master (ws/h264)
This commit is contained in:
commit
6b26845afe
28 files changed
+7345
-1
No files matched your search
@@ -0,0 +1,173 @@
|
||||
-- codecs/H264 tests: libx264 streams at many settings (Baseline / Main / High, CAVLC / CABAC, B frames, references,
|
||||
-- weighted prediction, 8x8 transforms, scaling matrices, slices, odd sizes with cropping, ...) decoded and compared
|
||||
-- per frame with ffmpeg's decoder (CRC-32 of the yuv420p planes, bit-exact); the AVCC (avcC + length-prefixed
|
||||
-- samples) and NAL-at-a-time paths; pts propagation; UNSUPPORTED streams (interlaced, 4:2:2, 4:4:4, 10-bit); error
|
||||
-- codes; and the fuzz no-throw pass. Fixtures: tools/fixtures/h264.py.
|
||||
local T = require("@tests/T")
|
||||
require("@formats/host") -- LuaJIT: the trace limits the api sets (host/init.luau)
|
||||
local H264 = require("@formats/codecs/H264")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Cases = require("@tests/gen/h264_cases")
|
||||
local Bad = require("@tests/gen/h264_bad")
|
||||
|
||||
local t = T.suite("h264")
|
||||
|
||||
-- 1. every stream, whole, against ffmpeg
|
||||
local nframes = 0
|
||||
for _, c in Cases do
|
||||
local frames, err = H264.decodeAll(c.data)
|
||||
t:check(frames, c.name .. ": " .. tostring(err))
|
||||
t:eq(#frames, #c.frames, c.name .. ": frame count")
|
||||
local good = 0
|
||||
for i, f in frames do
|
||||
if i == 1 then
|
||||
t:check(f.image.format == "yuv420p" and f.image.width == c.width and f.image.height == c.height, c.name .. ": size")
|
||||
end
|
||||
if Crc32.of(f.image.pixels) == c.frames[i] then
|
||||
good += 1
|
||||
end
|
||||
end
|
||||
t:eq(good, #c.frames, c.name .. ": frames bit-exact with ffmpeg")
|
||||
nframes += good
|
||||
end
|
||||
t:note("%d streams, %d frames bit-exact with ffmpeg", #Cases, nframes)
|
||||
|
||||
-- 2. AVCC samples, NAL-at-a-time input, pts
|
||||
local function splitAnnexB(s: string): { string }
|
||||
local out = {}
|
||||
local i = 1
|
||||
local starts = {}
|
||||
while true do
|
||||
local j = string.find(s, "\0\0\1", i, true)
|
||||
if not j then
|
||||
break
|
||||
end
|
||||
table.insert(starts, j + 3)
|
||||
i = j + 3
|
||||
end
|
||||
for k, st in starts do
|
||||
local e = if starts[k + 1] then starts[k + 1] - 4 else #s
|
||||
local nal = string.sub(s, st, e)
|
||||
while string.byte(nal, #nal) == 0 do
|
||||
nal = string.sub(nal, 1, #nal - 1)
|
||||
end
|
||||
table.insert(out, nal)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
for _, c in Cases do
|
||||
if c.avcc then
|
||||
local dec = H264.newDecoder({ avcC = buffer.fromstring(c.avcc) })
|
||||
t:check(dec, c.name .. ": avcC")
|
||||
local got = {}
|
||||
for i, s in c.samples do
|
||||
local frames, err = (dec :: any):decode(s, i * 1000)
|
||||
t:check(frames, c.name .. ": sample " .. i .. ": " .. tostring(err))
|
||||
for _, f in frames do
|
||||
table.insert(got, f)
|
||||
end
|
||||
end
|
||||
for _, f in (dec :: any):flush() do
|
||||
table.insert(got, f)
|
||||
end
|
||||
local good = 0
|
||||
local ptsOk = true
|
||||
local last = -1
|
||||
for i, f in got do
|
||||
if Crc32.of(f.image.pixels) == c.frames[i] then
|
||||
good += 1
|
||||
end
|
||||
if f.pts % 1000 ~= 0 then
|
||||
ptsOk = false
|
||||
end
|
||||
if f.poc <= last and not f.keyframe then
|
||||
ptsOk = false
|
||||
end
|
||||
last = f.poc
|
||||
end
|
||||
t:eq(good, #c.frames, c.name .. ": AVCC samples bit-exact")
|
||||
t:check(ptsOk, c.name .. ": pts carried per access unit, output in POC order")
|
||||
-- bare NAL units, one call each
|
||||
local dec2 = H264.newDecoder() :: any
|
||||
local got2 = 0
|
||||
for _, nal in splitAnnexB(c.data) do
|
||||
local frames = dec2:decode(nal)
|
||||
for _, f in frames do
|
||||
got2 += 1
|
||||
t:check(Crc32.of(f.image.pixels) == c.frames[got2], c.name .. ": NAL-at-a-time frame " .. got2)
|
||||
end
|
||||
end
|
||||
for _, f in dec2:flush() do
|
||||
got2 += 1
|
||||
t:check(Crc32.of(f.image.pixels) == c.frames[got2], c.name .. ": NAL-at-a-time frame " .. got2)
|
||||
end
|
||||
t:eq(got2, #c.frames, c.name .. ": NAL-at-a-time frame count")
|
||||
end
|
||||
end
|
||||
|
||||
-- 3. unsupported streams
|
||||
for _, c in Bad do
|
||||
local frames, err = H264.decodeAll(c.data)
|
||||
t:check(frames == nil and err and err.code == c.code, c.name .. ": " .. tostring(err))
|
||||
end
|
||||
|
||||
-- 4. error codes
|
||||
do
|
||||
local _, e1 = H264.decodeAll(123 :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "decode of a number: ARG")
|
||||
local _, e2 = H264.decodeAll(Cases[1].data, { maxPixels = 1000 })
|
||||
t:check(e2 and e2.code == Err.LIMIT, "maxPixels: LIMIT " .. tostring(e2))
|
||||
local _, e3 = H264.newDecoder({ avcC = buffer.fromstring("\1\2") })
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "short avcC: TRUNCATED")
|
||||
local _, e4 = H264.parseAvcC("\2\0\0\0\0\0\0")
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "avcC version 2: CORRUPT")
|
||||
local frames = H264.decodeAll("")
|
||||
t:check(frames and #frames == 0, "empty input: no frames")
|
||||
-- a stream cut in half still decodes the first frames (the rest concealed or missing)
|
||||
local c = Cases[3]
|
||||
local half = H264.decodeAll(string.sub(c.data, 1, #c.data // 2))
|
||||
t:check(half and #half > 0 and Crc32.of(half[1].image.pixels) == c.frames[1], "truncated stream: first frame intact")
|
||||
-- lowDelay outputs in decoding order (right for streams without reordering)
|
||||
local ld = H264.decodeAll(c.data, { lowDelay = true })
|
||||
t:check(ld and #ld == #c.frames and Crc32.of(ld[#ld].image.pixels) == c.frames[#c.frames], "lowDelay on a P-only stream")
|
||||
end
|
||||
|
||||
-- 5. fuzz: streams with damaged bytes / truncations must never throw
|
||||
local samples = {}
|
||||
for _, c in Cases do
|
||||
if #c.data < 20000 and #samples < 14 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decodeAll", function(b: buffer)
|
||||
return H264.decodeAll(b, { maxPixels = 2 ^ 18 })
|
||||
end, samples, { mutations = 10, keepHeader = 40 })
|
||||
t:fuzz("decodeAll (headers too)", function(b: buffer)
|
||||
return H264.decodeAll(b, { maxPixels = 2 ^ 18 })
|
||||
end, samples, { mutations = 6, garbage = 100 })
|
||||
-- heavier damage: many bytes changed in the slice data
|
||||
do
|
||||
local rand = T.rng(99)
|
||||
local tries, threw = 0, 0
|
||||
for _, s in samples do
|
||||
for _ = 1, 6 do
|
||||
local b = buffer.fromstring(s)
|
||||
local n = buffer.len(b)
|
||||
for _ = 1, 1 + rand(40) do
|
||||
buffer.writeu8(b, 60 + rand(n - 61), rand(256))
|
||||
end
|
||||
tries += 1
|
||||
local ok, err = pcall(H264.decodeAll, b, { maxPixels = 2 ^ 18 })
|
||||
if not ok then
|
||||
threw += 1
|
||||
print(" THREW (heavy mutation): " .. tostring(err))
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(threw == 0, "heavy mutations never throw (" .. threw .. " of " .. tries .. ")")
|
||||
t:note("heavy mutations: %d damaged streams, none threw", tries)
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,37 @@
|
||||
-- codecs/H264 conformance: a selection of the ITU-T H.264.1 (JVT) conformance bitstreams (Baseline / Main / Extended
|
||||
-- without SP / High: CAVLC, CABAC, B frames, weighted prediction, multiple slices, MMCO / long-term references,
|
||||
-- frame_num gaps, cropping, PCM, 8x8 transforms, scaling matrices, ...) decoded bit-exact against the reference
|
||||
-- decoder's yuv (JM, shipped in each zip; ffmpeg where there is none), and interlaced / SP / 4:2:2 streams refused
|
||||
-- with UNSUPPORTED. Fixtures: tools/fixtures/h264_jvt.py (downloaded from itu.int; skipped when unreachable).
|
||||
local T = require("@tests/T")
|
||||
require("@formats/host") -- LuaJIT: the trace limits the api sets (host/init.luau)
|
||||
local H264 = require("@formats/codecs/H264")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Cases = require("@tests/gen/h264_jvt")
|
||||
|
||||
local t = T.suite("h264 conformance")
|
||||
local streams, nframes, refused = 0, 0, 0
|
||||
for _, c in Cases do
|
||||
local data = Base64.decode(c.data64) :: buffer
|
||||
local frames, err = H264.decodeAll(data)
|
||||
if c.expect == "UNSUPPORTED" then
|
||||
t:check(frames == nil and err and err.code == Err.UNSUPPORTED, c.name .. ": UNSUPPORTED (" .. tostring(err) .. ")")
|
||||
refused += 1
|
||||
else
|
||||
t:check(frames, c.name .. ": " .. tostring(err))
|
||||
t:eq(#frames, #c.frames, c.name .. ": frame count")
|
||||
local good = 0
|
||||
for i, f in frames do
|
||||
if Crc32.of(f.image.pixels) == c.frames[i] then
|
||||
good += 1
|
||||
end
|
||||
end
|
||||
t:eq(good, #c.frames, c.name .. ": bit-exact with the " .. c.ref .. " reference")
|
||||
streams += 1
|
||||
nframes += good
|
||||
end
|
||||
end
|
||||
t:note("%d conformance streams (%d frames) bit-exact, %d refused as UNSUPPORTED%s", streams, nframes, refused, if #Cases == 0 then " (fixtures unavailable: no download)" else "")
|
||||
t:done()
|
||||
@@ -0,0 +1,100 @@
|
||||
-- video/h264 tests: H.264 through F.video.open end to end: raw Annex B (.h264, the video/h264 format), MP4 and MKV
|
||||
-- (avcC + length-prefixed samples) and MPEG-TS / AVI (Annex B packets) written by ffmpeg / libx264, frames bit-exact
|
||||
-- against ffmpeg's decode of each file; display-order timestamps; the raw writer (MP4 -> .h264) read back; sniffing;
|
||||
-- the fuzz no-throw pass on the raw demuxer. Fixtures: tools/fixtures/h264.py (h264_containers).
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Fmt = require("@formats/video/h264")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Cases = require("@tests/gen/h264_containers")
|
||||
|
||||
local t = T.suite("video/h264")
|
||||
t:eq(Fmt.name, "h264", "name")
|
||||
|
||||
local function frames(v: any): ({ any }, any)
|
||||
local out = {}
|
||||
while true do
|
||||
local f, err = v:nextFrame()
|
||||
if not f then
|
||||
return out, err
|
||||
end
|
||||
table.insert(out, f)
|
||||
end
|
||||
end
|
||||
|
||||
local byName = {}
|
||||
for _, c in Cases do
|
||||
byName[c.name] = c
|
||||
local v, err = F.video.open(c.data)
|
||||
t:check(v ~= nil, c.name .. ": F.video.open " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.codec, "h264", c.name .. ": codec")
|
||||
local got, ferr = frames(v)
|
||||
t:check(ferr == nil, c.name .. ": no decode error " .. tostring(ferr))
|
||||
t:eq(#got, #c.frames, c.name .. ": frame count")
|
||||
local good, mono = 0, true
|
||||
for i, f in got do
|
||||
if Crc32.of(f.image.pixels) == c.frames[i] then
|
||||
good += 1
|
||||
end
|
||||
if i > 1 and f.pts <= got[i - 1].pts then
|
||||
mono = false
|
||||
end
|
||||
end
|
||||
t:eq(good, #c.frames, c.name .. ": frames bit-exact with ffmpeg")
|
||||
t:check(mono, c.name .. ": display-order timestamps increase")
|
||||
t:check(got[1] and got[1].image.meta.yuv and got[1].image.meta.yuv.range == "limited", c.name .. ": meta.yuv")
|
||||
local rgba = got[1] and v:toRGBA(got[1])
|
||||
t:check(rgba and rgba.format == "rgba8" and rgba.width == c.width, c.name .. ": toRGBA")
|
||||
end
|
||||
end
|
||||
|
||||
-- raw format directly: sniff, demux, 25 fps output order
|
||||
do
|
||||
local c = byName["cabac_b.h264"]
|
||||
local r = Bytes.reader(buffer.fromstring(c.data))
|
||||
t:check(Fmt.sniff(r) >= 0.5, "sniff a raw stream")
|
||||
t:check(Fmt.sniff(Bytes.reader(buffer.fromstring(byName["cabac_b.mp4"].data))) == 0, "sniff rejects MP4")
|
||||
local dmx = Fmt.demux(r)
|
||||
t:check(dmx and dmx:count(1) == #c.frames, "one packet per access unit")
|
||||
local v = Fmt.open(Bytes.reader(buffer.fromstring(c.data)))
|
||||
local got = frames(v)
|
||||
t:check(#got == #c.frames and math.abs(got[2].pts - 1 / 25) < 1e-9, "raw: pts in output order at 25 fps")
|
||||
end
|
||||
|
||||
-- writer: the MP4's H.264 track as a raw Annex B stream, decoded again
|
||||
for _, src in { "cabac_b.mp4", "baseline.mkv", "baseline.ts" } do
|
||||
local c = byName[src]
|
||||
local v = F.video.open(c.data)
|
||||
local out, err = F.save(v, nil, { format = "h264" })
|
||||
t:check(out ~= nil, src .. " -> h264: " .. tostring(err))
|
||||
if out then
|
||||
local v2 = F.video.open(out)
|
||||
t:check(v2 ~= nil and v2.format == "h264", src .. " -> h264: reads back as raw H.264")
|
||||
local got = frames(v2)
|
||||
local good = 0
|
||||
for i, f in got do
|
||||
if Crc32.of(f.image.pixels) == c.frames[i] then
|
||||
good += 1
|
||||
end
|
||||
end
|
||||
t:eq(good, #c.frames, src .. " -> h264: frames bit-exact")
|
||||
end
|
||||
end
|
||||
|
||||
-- fuzz: the raw demuxer + decoder
|
||||
local samples = { byName["baseline.h264"].data, byName["cabac_b.h264"].data }
|
||||
t:fuzz("open + frames", function(b: buffer)
|
||||
local v = Fmt.open(Bytes.reader(b), { maxPixels = 2 ^ 18 })
|
||||
if v then
|
||||
for _ = 1, 40 do
|
||||
if not v:nextFrame() then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, samples, { mutations = 8, keepHeader = 30 })
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user