merge master (ws/video2: MPEG-2, Theora, legacy codecs) into ws/video2-flv
This commit is contained in:
commit
da743e2fb8
120 files changed
+22683
-93
No files matched your search
@@ -0,0 +1,105 @@
|
||||
-- video/fli tests: FLI (COLOR_64, BRUN, LC, BLACK, COPY, PSTAMP, a ring frame), FLC (prefix chunk, COLOR_256, SS2 with
|
||||
-- line skips and the last-byte opcode, an empty frame), an odd-size FLC, and 15 / 16 / 24-bit FLCs (BRUN, DTA_BRUN,
|
||||
-- DELTA, DTA_LC, COPY, DTA_COPY, BLACK), all encoded by the fixture script: every frame equals ffmpeg's decode
|
||||
-- (CRC-32 of rgba), frame rates, the ring frame option, seeking, errors, the fuzz pass. Fixtures: tools/fixtures/fli.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Fli = require("@formats/video/fli")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/fli_cases")
|
||||
|
||||
local t = T.suite("fli")
|
||||
t:eq(Fli.name, "fli", "name")
|
||||
t:eq(Fli.category, "video", "category")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Fli.sniff(reader(c.data)) > 0, label .. ": sniff")
|
||||
t:eq(F.detect(buffer.fromstring(c.data)), "fli", label .. ": detect")
|
||||
local v, err = F.video.open(buffer.fromstring(c.data))
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.codec, "flic", label .. ": codec")
|
||||
t:check(v.width == c.width and v.height == c.height, label .. ": size")
|
||||
t:check(math.abs(v.frameRate - c.rate) < 1e-9, label .. ": frame rate " .. v.frameRate)
|
||||
t:eq(v.track.bitsPerPixel, c.depth, label .. ": depth")
|
||||
local ring = v.demuxer.meta.ringFrame
|
||||
local want = if ring then #c.frames - 1 else #c.frames
|
||||
local n, bad = 0, 0
|
||||
while true do
|
||||
local f, ferr = v:nextFrame()
|
||||
if not f then
|
||||
t:check(ferr == nil, label .. ": " .. tostring(ferr))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
if f.image.format ~= "rgba8" or Crc32.of(f.image.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:check(n == want and bad == 0, string.format("%s: %d of %d frames equal ffmpeg's (%d differ)", label, n - bad, want, bad))
|
||||
v:seek(0.25)
|
||||
local f = v:nextFrame()
|
||||
local k = math.floor(0.25 * c.rate + 1 - 1e-9) + 1
|
||||
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[k], label .. ": seek to frame " .. k)
|
||||
if ring then
|
||||
local v2 = Fli.open(reader(c.data), { ringFrame = true })
|
||||
local m = 0
|
||||
while v2 and v2:nextFrame() do
|
||||
m += 1
|
||||
end
|
||||
t:eq(m, #c.frames, label .. ": with the ring frame")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = Fli.open(reader(string.sub(Fx[1].data, 1, 100)))
|
||||
t:check(e ~= nil and e.code == Err.TRUNCATED, "short header: TRUNCATED")
|
||||
local bad = Fx[1].data
|
||||
bad = string.sub(bad, 1, 4) .. "\0\0" .. string.sub(bad, 7)
|
||||
t:eq(Fli.sniff(reader(bad)), 0, "bad magic: no sniff")
|
||||
local _, e2 = Fli.open(reader(bad))
|
||||
t:check(e2 ~= nil and e2.code == Err.CORRUPT, "bad magic: CORRUPT")
|
||||
local big = string.sub(Fx[1].data, 1, 8) .. "\255\15\255\15" .. string.sub(Fx[1].data, 13)
|
||||
local _, e3 = Fli.open(reader(big), { maxPixels = 2 ^ 20 })
|
||||
t:check(e3 ~= nil and e3.code == Err.LIMIT, "maxPixels: LIMIT")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local v = Fli.open(Bytes.reader(b))
|
||||
if v then
|
||||
while v:nextFrame() do
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, samples, { mutations = 12, garbage = 40, keepHeader = 16 })
|
||||
-- the frame decoder itself (codecs/video/Legacy/Flic, native: no pcall in between)
|
||||
local Flic = require("@formats/codecs/video/Legacy").Flic
|
||||
for _, c in { Fx[1], Fx[2], Fx[4], Fx[6] } do
|
||||
local d = Fli.demux(reader(c.data))
|
||||
local packets = {}
|
||||
for k = 1, math.min(4, d:count(1)) do
|
||||
table.insert(packets, d:get(1, k).data)
|
||||
end
|
||||
t:fuzz(c.name .. " frames", function(b: buffer)
|
||||
local st = Flic.state(c.width, c.height, c.depth)
|
||||
if st then
|
||||
Flic.decode(st, b)
|
||||
return Flic.image(st)
|
||||
end
|
||||
return nil
|
||||
end, packets, { mutations = 20, garbage = 30 })
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,80 @@
|
||||
-- MPEG-2 video in the containers (tools/fixtures/mpeg2.py, "mpeg2_containers"): the same elementary stream copied by
|
||||
-- ffmpeg into MPEG-TS (stream type 0x02, and the PMT patched to 0x80, private / DigiCipher II video), MPEG-PS (VOB),
|
||||
-- Matroska (V_MPEG2) and AVI: each opens through F.video.open with codec "mpeg2", its frames equal ffmpeg's
|
||||
-- `-idct int` decode (luma CRC per frame, display order), seeking lands on a frame; the raw .m2v opens too.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Fx = require("@tests/gen/mpeg2_containers")
|
||||
local Prog = require("@tests/gen/mpeg2_prog")
|
||||
|
||||
local t = T.suite("video/mpeg2")
|
||||
|
||||
local function lumaCrc(img: any): number
|
||||
return Crc32.of(img.pixels, 0, img.width * img.height)
|
||||
end
|
||||
|
||||
local CASES = {
|
||||
{ "ts", "mpegts" },
|
||||
{ "ts80", "mpegts" },
|
||||
{ "mpg", "mpegps" },
|
||||
{ "mkv", "mkv" },
|
||||
{ "avi", "avi" },
|
||||
}
|
||||
for _, c in CASES do
|
||||
local key, format = c[1], c[2]
|
||||
local data = Base64.decode(Fx[key]) :: buffer
|
||||
local v, err = F.video.open(data)
|
||||
t:check(v ~= nil, key .. ": opens: " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.format, format, key .. ": format")
|
||||
t:eq(v.codec, "mpeg2", key .. ": codec")
|
||||
t:check(v.width == Prog.width and v.height == Prog.height, key .. ": size")
|
||||
local frames, exact = 0, 0
|
||||
while true do
|
||||
local f, ferr = v:nextFrame()
|
||||
t:check(ferr == nil, key .. ": frame error " .. tostring(ferr))
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
frames += 1
|
||||
local ref = Fx.crcs[frames]
|
||||
if ref and lumaCrc(f.image) == ref[1] then
|
||||
exact += 1
|
||||
end
|
||||
end
|
||||
t:eq(frames, #Fx.crcs, key .. ": frame count")
|
||||
-- the frames ffmpeg's encoder left with an uncoded macroblock are concealed differently (tests/codecs/mpeg2)
|
||||
t:check(exact >= frames - 2, string.format("%s: %d of %d frames equal ffmpeg's", key, exact, frames))
|
||||
v:seek(0.4)
|
||||
local f = v:nextFrame()
|
||||
t:check(f ~= nil and math.abs(f.pts - 0.4) < 0.05, key .. ": seek")
|
||||
t:note(" %-5s %-7s %d frames, %d bit-exact", key, format, frames, exact)
|
||||
end
|
||||
end
|
||||
|
||||
-- the elementary stream itself
|
||||
do
|
||||
local v, err = F.video.open(Base64.decode(Prog.file) :: buffer)
|
||||
t:check(v ~= nil and v.codec == "mpeg2", "m2v: opens as mpeg2: " .. tostring(err))
|
||||
if v then
|
||||
local n = 0
|
||||
while v:nextFrame() do
|
||||
n += 1
|
||||
end
|
||||
t:eq(n, Prog.frames, "m2v: frame count")
|
||||
end
|
||||
end
|
||||
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local v = F.video.open(b)
|
||||
if v then
|
||||
for _ = 1, 3 do
|
||||
v:nextFrame()
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, { Base64.decode(Fx.ts) :: buffer, Base64.decode(Fx.mpg) :: buffer }, { mutations = 30, garbage = 5 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,211 @@
|
||||
-- video/ogv tests: Ogg Theora files from ffmpeg (libtheora + libvorbis; tools/fixtures/theora.py): sniffing (Theora
|
||||
-- BOS pages, plain Ogg Vorbis left to the audio formats), tracks, granule times, every frame through F.video.open
|
||||
-- bit-exact with ffmpeg's (Adler-32 of yuv420p, its framecrc), an empty packet (a repeated frame), the Vorbis track
|
||||
-- through the api's audio hook against ffmpeg's decode, tags, seeking, remuxing (ogv -> ogv, mkv -> ogv, ogv -> mkv)
|
||||
-- read back, errors and the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local F = require("@formats")
|
||||
local Ogv = require("@formats/video/ogv")
|
||||
local Theora = require("@formats/codecs/video/Theora")
|
||||
local AudioWrap = require("@formats/containers/AudioWrap")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Adler32 = require("@formats/core/Adler32")
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local t = T.suite("ogv")
|
||||
|
||||
local AV = require("@tests/gen/theora_av") :: any
|
||||
local Q0 = require("@tests/gen/theora_q0") :: any
|
||||
local DUPE = require("@tests/gen/theora_dupe") :: any
|
||||
local MKV = require("@tests/gen/theora_mkv") :: any
|
||||
local OPUS = require("@tests/gen/theora_opus") :: any
|
||||
local FLAC = require("@tests/gen/theora_flac") :: any
|
||||
|
||||
local function file(fx: any): buffer
|
||||
return Base64.decode(fx.file) :: buffer
|
||||
end
|
||||
|
||||
-- every frame of a VideoDecoder against ffmpeg's framecrc (yuv420p, keyed by frame number); frames ffmpeg does not
|
||||
-- show (empty packets) must repeat the previous one
|
||||
local function frames(v: any, fx: any, label: string, fps: number)
|
||||
local want: { [number]: number } = {}
|
||||
for _, f in fx.frames do
|
||||
want[f[1]] = f[2]
|
||||
end
|
||||
local n, bad, shown, prev = 0, 0, 0, nil :: number?
|
||||
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 c = Adler32.of(f.image.pixels, nil, nil, 0)
|
||||
local w = want[math.floor(f.pts * fps + 0.5)]
|
||||
if w then
|
||||
shown += 1
|
||||
if c ~= w then
|
||||
bad += 1
|
||||
end
|
||||
elseif c ~= prev then
|
||||
bad += 1
|
||||
end
|
||||
prev = c
|
||||
end
|
||||
t:eq(bad, 0, label .. ": frames equal to ffmpeg's (" .. n .. " frames)")
|
||||
t:eq(shown, #fx.frames, label .. ": every frame ffmpeg shows")
|
||||
return n
|
||||
end
|
||||
|
||||
-- 1. contract and sniffing
|
||||
t:eq(Ogv.name, "ogv", "name")
|
||||
t:eq(Ogv.category, "video", "category")
|
||||
t:check(table.find(Ogv.extensions, "ogv") ~= nil, "ogv extension")
|
||||
t:eq(Ogv.sniff(Bytes.reader(file(AV))), 1, "sniff: Theora + Vorbis")
|
||||
t:eq(Ogv.sniff(Bytes.reader(file(Q0))), 1, "sniff: Theora")
|
||||
t:eq(Ogv.sniff(Bytes.reader(buffer.fromstring("OggS\0\2" .. string.rep("\0", 40)))), 0, "sniff: an Ogg page without Theora")
|
||||
t:eq(Ogv.sniff(Bytes.reader(buffer.fromstring("RIFF"))), 0, "sniff: not Ogg")
|
||||
|
||||
-- 2. Theora + Vorbis through the api
|
||||
local v, err = F.video.open(file(AV))
|
||||
t:check(v ~= nil, "open: " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.format, "ogv", "format")
|
||||
t:eq(v.codec, "theora", "codec")
|
||||
t:eq(v.width * 1000 + v.height, 160120, "size")
|
||||
t:eq(v.frameRate, 25, "frame rate")
|
||||
local dmx = v.demuxer :: any
|
||||
t:eq(#dmx.tracks, 2, "two tracks")
|
||||
local vt, at = dmx:track("video"), dmx:track("audio")
|
||||
t:check(vt and vt.codec == "theora" and vt.timescale == 25, "video track")
|
||||
t:check(at and at.codec == "vorbis" and at.sampleRate == 44100 and at.channels == 2, "audio track")
|
||||
t:check(vt and vt.config and #(Theora.splitConfig(vt.config) or {}) == 3, "video config: three Xiph-laced headers")
|
||||
t:check(math.abs(v.duration - 1) < 0.05, "duration " .. v.duration)
|
||||
t:check(v.meta.tags and v.meta.tags.encoder ~= nil, "tags from the comment header")
|
||||
local first = dmx:get(vt.id, 1)
|
||||
t:check(first and first.keyframe and first.pts == 0, "first packet: a keyframe at 0")
|
||||
t:check(not dmx:get(vt.id, 2).keyframe, "second packet: an inter frame")
|
||||
frames(v, AV, "av", 25)
|
||||
local pcm = Base64.decode(AV.pcm) :: buffer
|
||||
C.audio(t, v, { pcm = buffer.tostring(pcm) }, "av", false, 20)
|
||||
-- seeking
|
||||
v:seek(0.5)
|
||||
local f = v:nextFrame()
|
||||
t:check(f ~= nil and f.pts >= 0.5 - 1e-6 and f.pts < 0.6, "seek 0.5 -> " .. tostring(f and f.pts))
|
||||
if f then
|
||||
local want = nil
|
||||
for _, w in AV.frames do
|
||||
if w[1] == math.floor(f.pts * 25 + 0.5) then
|
||||
want = w[2]
|
||||
end
|
||||
end
|
||||
t:eq(Adler32.of(f.image.pixels, nil, nil, 0), want, "the frame after seeking equals ffmpeg's")
|
||||
end
|
||||
-- remux ogv -> ogv
|
||||
v:reset()
|
||||
local out, rerr = F.save(v, nil, { format = "ogv" })
|
||||
t:check(out ~= nil, "remux: " .. tostring(rerr))
|
||||
if out then
|
||||
t:eq(Ogv.sniff(Bytes.reader(out)), 1, "remux: sniff")
|
||||
local v2 = F.video.open(out) :: any
|
||||
t:check(v2 ~= nil and #v2.demuxer.tracks == 2, "remux: two tracks")
|
||||
if v2 then
|
||||
frames(v2, AV, "remux", 25)
|
||||
C.audio(t, v2, { pcm = buffer.tostring(pcm) }, "remux", false, 20)
|
||||
end
|
||||
end
|
||||
-- remux ogv -> mkv (V_THEORA)
|
||||
v:reset()
|
||||
local mk, merr = F.save(v, nil, { format = "mkv" })
|
||||
t:check(mk ~= nil, "to mkv: " .. tostring(merr))
|
||||
if mk then
|
||||
local v3 = F.video.open(mk) :: any
|
||||
t:check(v3 ~= nil and v3.codec == "theora", "to mkv: Theora track")
|
||||
if v3 then
|
||||
frames(v3, AV, "to mkv", 25)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 3. Opus and FLAC audio tracks (FLAC: exact)
|
||||
for _, c in { { OPUS, "opus", 48000 }, { FLAC, "flac", 22050 } } do
|
||||
local fx, codec, rate = c[1], c[2], c[3]
|
||||
local vd, e = F.video.open(file(fx))
|
||||
t:check(vd ~= nil, codec .. ": open: " .. tostring(e))
|
||||
if vd then
|
||||
local at = (vd.demuxer :: any):track("audio")
|
||||
t:check(at and at.codec == codec and at.sampleRate == rate and at.channels == 1, codec .. ": audio track")
|
||||
frames(vd, fx, codec, 25)
|
||||
C.audio(t, vd, { pcm = buffer.tostring(Base64.decode(fx.pcm) :: buffer) }, codec, codec == "flac", 20)
|
||||
if codec == "opus" then
|
||||
vd:reset()
|
||||
local out = F.save(vd, nil, { format = "ogv" })
|
||||
local v2 = out and F.video.open(out) :: any
|
||||
t:check(v2 ~= nil and v2.audio ~= nil, "opus: remux keeps the audio")
|
||||
if v2 then
|
||||
C.audio(t, v2, { pcm = buffer.tostring(Base64.decode(fx.pcm) :: buffer) }, "opus remux", false, 20)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 4. an empty packet: a repeated frame with its own time
|
||||
do
|
||||
local vd = Ogv.open(Bytes.reader(file(DUPE))) :: any
|
||||
local n = 0
|
||||
local prev, dupAt = nil, nil
|
||||
while true do
|
||||
local f = vd:nextFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
local c = Adler32.of(f.image.pixels, nil, nil, 0)
|
||||
if n == 6 then
|
||||
dupAt = f.pts
|
||||
t:eq(c, prev, "dupe: frame 6 repeats frame 5")
|
||||
end
|
||||
prev = c
|
||||
end
|
||||
t:eq(n, #DUPE.frames + 1, "dupe: one frame per packet")
|
||||
t:check(dupAt ~= nil and math.abs((dupAt :: number) - 0.2) < 1e-6, "dupe: its time slot (0.2 s)")
|
||||
t:check(vd.audio == nil and vd.audioError == nil, "no audio track, no audio error")
|
||||
end
|
||||
|
||||
-- 5. Theora in Matroska -> ogv
|
||||
do
|
||||
local mv = F.video.open(file(MKV)) :: any
|
||||
local out, e = F.save(mv, nil, { format = "ogv" })
|
||||
t:check(out ~= nil, "mkv -> ogv: " .. tostring(e))
|
||||
if out then
|
||||
local v2 = Ogv.open(Bytes.reader(out)) :: any
|
||||
frames(v2, MKV, "mkv -> ogv", 25)
|
||||
end
|
||||
end
|
||||
|
||||
-- 6. not Theora: plain Ogg Vorbis goes to the audio format, writing other codecs fails
|
||||
do
|
||||
local vd = Ogv.open(Bytes.reader(file(AV))) :: any
|
||||
local at = vd.demuxer:track("audio")
|
||||
local ogg = AudioWrap.wrap(vd.demuxer, at) :: buffer
|
||||
t:eq(Ogv.sniff(Bytes.reader(ogg)), 0, "sniff: Ogg Vorbis is not ogv")
|
||||
local a = F.load(ogg, { name = "x.ogg" })
|
||||
t:check(a ~= nil and a.kind == "audio", "F.load of Ogg Vorbis named .ogg: audio")
|
||||
local v4, e4 = Ogv.open(Bytes.reader(ogg))
|
||||
t:check(v4 ~= nil and v4.decodeError ~= nil, "an Ogg without video: opens, frames UNSUPPORTED (" .. tostring(e4) .. ")")
|
||||
local _, ee = Ogv.encode(Ogv.open(Bytes.reader(ogg)) :: any)
|
||||
t:check(ee ~= nil and ee.code == Err.UNSUPPORTED, "encode without Theora: UNSUPPORTED")
|
||||
local _, eg = Ogv.open(Bytes.reader(buffer.fromstring("OggS garbage")))
|
||||
t:check(eg ~= nil, "garbage: an error")
|
||||
local cut = buffer.create(5000)
|
||||
buffer.copy(cut, 0, file(Q0), 0, 5000)
|
||||
local vc = Ogv.open(Bytes.reader(cut))
|
||||
t:check(vc ~= nil, "a truncated file opens")
|
||||
end
|
||||
|
||||
-- 7. fuzz
|
||||
t:fuzz("open", C.fuzzOpen(Ogv), { buffer.tostring(file(Q0)) }, { mutations = 16, garbage = 50 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,180 @@
|
||||
-- video/smk tests: Smacker files encoded by the fixture script (SMK2 and SMK4, every block type and run, the trees'
|
||||
-- escape cache, palette updates, a ring frame, ms and 10 us frame times; packed 16-bit stereo / 8-bit mono / 16-bit
|
||||
-- mono / 8-bit stereo audio and raw s16le / u8 tracks): every video frame and every audio track equal ffmpeg's
|
||||
-- decode (CRC-32 of rgba / f32le), audio seeking, errors, and the fuzz pass on the container, the video codec and the
|
||||
-- audio codec. Fixtures: tools/fixtures/smk.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Smk = require("@formats/video/smk")
|
||||
local SmkAudio = require("@formats/video/smk/Audio")
|
||||
local Legacy = require("@formats/codecs/video/Legacy")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/smk_cases")
|
||||
|
||||
local t = T.suite("smk")
|
||||
t:eq(Smk.name, "smk", "name")
|
||||
t:eq(Smk.category, "video", "category")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
-- every frame of an audio Decoder -> f32 buffer, frames
|
||||
local function drain(dec: any): (buffer, number)
|
||||
local chunks, total = {}, 0
|
||||
while true do
|
||||
local b, n = dec:read(1000)
|
||||
if n == 0 then
|
||||
break
|
||||
end
|
||||
table.insert(chunks, { b, n })
|
||||
total += n
|
||||
end
|
||||
local out = buffer.create(total * dec.channels * 4)
|
||||
local at = 0
|
||||
for _, c in chunks do
|
||||
buffer.copy(out, at, c[1], 0, c[2] * dec.channels * 4)
|
||||
at += c[2] * dec.channels * 4
|
||||
end
|
||||
return out, total
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Smk.sniff(reader(c.data)) == 1, label .. ": sniff")
|
||||
t:eq(F.detect(buffer.fromstring(c.data)), "smk", label .. ": detect")
|
||||
local v, err = F.video.open(buffer.fromstring(c.data))
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.codec, "smackvid", label .. ": codec")
|
||||
t:check(v.width == c.width and v.height == c.height, label .. ": size")
|
||||
t:check(math.abs(v.frameRate - c.rate) < 1e-6, label .. ": frame rate " .. v.frameRate)
|
||||
local want = if c.ring then #c.frames - 1 else #c.frames
|
||||
local n, bad = 0, 0
|
||||
while true do
|
||||
local f, ferr = v:nextFrame()
|
||||
if not f then
|
||||
t:check(ferr == nil, label .. ": " .. tostring(ferr))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
if f.image.format ~= "rgba8" or Crc32.of(f.image.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:check(n == want and bad == 0, string.format("%s: %d of %d frames equal ffmpeg's (%d differ)", label, n - bad, want, bad))
|
||||
v:seek(0.2)
|
||||
local f = v:nextFrame()
|
||||
local k = math.floor(0.2 * c.rate - 1e-9) + 2
|
||||
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[k], label .. ": seek to frame " .. k)
|
||||
-- audio: the first track through the VideoDecoder, every track through Smk.openAudio
|
||||
if #c.audio > 0 then
|
||||
t:check(v.audio ~= nil, label .. ": audio " .. tostring(v.audioError))
|
||||
end
|
||||
local tracks = {}
|
||||
for _, tr in v.demuxer.tracks do
|
||||
if tr.kind == "audio" then
|
||||
table.insert(tracks, tr)
|
||||
end
|
||||
end
|
||||
t:eq(#tracks, #c.audio, label .. ": audio tracks")
|
||||
for i, a in c.audio do
|
||||
local tr = tracks[i]
|
||||
t:eq(tr.codec, a.codec, label .. ": audio " .. i .. " codec")
|
||||
local dec, aerr = Smk.openAudio(v.demuxer, tr)
|
||||
t:check(dec ~= nil, label .. ": audio " .. i .. ": " .. tostring(aerr))
|
||||
if dec then
|
||||
t:check(dec.sampleRate == a.rate and dec.channels == a.channels, label .. ": audio " .. i .. " format")
|
||||
local pcm, frames = drain(dec)
|
||||
t:eq(frames, a.frames, label .. ": audio " .. i .. " frames")
|
||||
t:eq(Crc32.of(pcm), a.crc, label .. ": audio " .. i .. " equals ffmpeg's")
|
||||
dec:seek(0)
|
||||
local again = drain(dec)
|
||||
t:eq(Crc32.of(again), a.crc, label .. ": audio " .. i .. " after seek(0)")
|
||||
dec:seek(0.3)
|
||||
local tail, rest = drain(dec)
|
||||
local skip = a.frames - rest
|
||||
t:check(math.abs(skip - 0.3 * a.rate) <= 1, label .. ": audio " .. i .. " seek(0.3) at frame " .. skip)
|
||||
local same = buffer.len(tail) > 0
|
||||
for q = 0, math.min(buffer.len(tail), 4000) - 4, 4 do
|
||||
if buffer.readu32(tail, q) ~= buffer.readu32(pcm, skip * a.channels * 4 + q) then
|
||||
same = false
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(same, label .. ": audio " .. i .. " seek(0.3) samples")
|
||||
end
|
||||
end
|
||||
if c.ring then
|
||||
local v2 = Smk.open(reader(c.data), { ringFrame = true })
|
||||
local m = 0
|
||||
while v2 and v2:nextFrame() do
|
||||
m += 1
|
||||
end
|
||||
t:eq(m, #c.frames, label .. ": with the ring frame")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = Smk.open(reader(string.sub(Fx[1].data, 1, 60)))
|
||||
t:check(e ~= nil and e.code == Err.TRUNCATED, "short header: TRUNCATED")
|
||||
local _, e2 = Smk.open(reader("SMK3" .. string.sub(Fx[1].data, 5)))
|
||||
t:check(e2 ~= nil and e2.code == Err.CORRUPT, "bad magic: CORRUPT")
|
||||
t:eq(Smk.sniff(reader("SMK3" .. string.sub(Fx[1].data, 5))), 0, "bad magic: no sniff")
|
||||
local _, e3 = Smk.open(reader(Fx[1].data), { maxPixels = 100 })
|
||||
t:check(e3 ~= nil and e3.code == Err.LIMIT, "maxPixels: LIMIT")
|
||||
local _, e4 = Smk.open(reader(string.sub(Fx[1].data, 1, 200)))
|
||||
t:check(e4 ~= nil and e4.code == Err.TRUNCATED, "trees past the end: TRUNCATED")
|
||||
end
|
||||
|
||||
-- fuzz: the container, the video codec (native, no pcall in between) and the audio codec
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local v = Smk.open(Bytes.reader(b))
|
||||
if v then
|
||||
for _ = 1, 4 do
|
||||
if not v:nextFrame() then
|
||||
break
|
||||
end
|
||||
end
|
||||
if v.audio then
|
||||
v.audio:read(500)
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, samples, { mutations = 16, garbage = 40, keepHeader = 4 })
|
||||
local S, P = Legacy.Smacker, Legacy.Picture
|
||||
for _, c in { Fx[1], Fx[2] } do
|
||||
local d = Smk.demux(reader(c.data))
|
||||
local tr = d.tracks[1]
|
||||
local packets = {}
|
||||
for k = 1, math.min(3, d:count(1)) do
|
||||
table.insert(packets, d:get(1, k).data)
|
||||
end
|
||||
t:fuzz(c.name .. " trees", function(b: buffer)
|
||||
return S.trees(b)
|
||||
end, { tr.config }, { mutations = 30, garbage = 30, keepHeader = 16 })
|
||||
local trees = assert(S.trees(tr.config))
|
||||
t:fuzz(c.name .. " frames", function(b: buffer)
|
||||
return S.decode(P.new(c.width, c.height, true, 4, 4), b, trees, tr.codecTag == "SMK4")
|
||||
end, packets, { mutations = 30, garbage = 30 })
|
||||
for _, at in d.tracks do
|
||||
if at.kind == "audio" then
|
||||
local ap = {}
|
||||
for k = 1, math.min(2, d:count(at.id)) do
|
||||
table.insert(ap, d:get(at.id, k).data)
|
||||
end
|
||||
t:fuzz(c.name .. " audio " .. at.codec, function(b: buffer)
|
||||
return SmkAudio.decode(b, at.channels, at.bitsPerSample, at.codec == "smackaudio")
|
||||
end, ap, { mutations = 20, garbage = 20 })
|
||||
end
|
||||
end
|
||||
end
|
||||
t:done()
|
||||
Reference in new issue
Block a user