-- video/flv tests: FLV files from ffmpeg against ffprobe / ffmpeg (Nellymoser, Speex, VP6: known but UNSUPPORTED). -- Fixtures: tools/fixtures/flv.py. local T = require("@tests/T") local C = require("@tests/video/_common") local F = require("@rosetta") local Flv = require("@rosetta/video/flv") local Video = require("@rosetta/codecs/video/Video") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Fx = require("@tests/gen/flv_cases") local Mx = require("@tests/gen/flv_mux") local Src = require("@tests/gen/flv_src") local t = T.suite("flv") t:eq(Flv.name, "flv", "name") t:eq(Flv.category, "video", "category") local EXACT_AUDIO = { pcm_s16le = true, pcm_u8 = true, pcm_alaw = true, adpcm_swf = true } local NO_AUDIO_FORMAT = { nellymoser = true, speex = true } local byName = {} for _, c in Fx do byName[c.name] = c end local function frames(v: any, c: any, label: string) if v.codec == "flv1" then v:reset() local n, bad = 0, 0 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 if Crc32.of(f.image.pixels) ~= c.frames[n] then bad += 1 end end t:eq(n, #c.frames, label .. ": frame count") t:eq(bad, 0, label .. ": Sorenson frames bit-exact with ffmpeg") else C.frames(t, v, c, label, 38) end end for _, c in Fx do local label = c.name t:check(Flv.sniff(Bytes.reader(buffer.fromstring(c.data))) == 1, 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) for i, s in c.streams do local tr = dmx.tracks[i] if s.kind == "video" then t:check(tr.width == s.width and tr.height == s.height, label .. ": video size " .. tostring(tr.width) .. "x" .. tostring(tr.height)) t:check(tr.codec == s.codec, label .. ": video codec " .. tr.codec .. " (" .. s.codec .. ")") elseif s.rate then t:check(tr.sampleRate == s.rate and tr.channels == s.channels, label .. ": audio " .. tostring(tr.sampleRate) .. " Hz x " .. tostring(tr.channels)) end end local md = dmx.meta.metadata t:check(md ~= nil and type(md.duration) == "number" and md.duration > 0, label .. ": onMetaData duration") if c.frames then frames(v, c, label) C.seek(t, v, c, label, 0.5) elseif dmx:track("video") then local _, e = v:nextFrame() t:check(e ~= nil and e.code == F.Err.UNSUPPORTED and not Video.canDecode(v.codec), label .. ": " .. v.codec .. " is known, decoding UNSUPPORTED") end local at = dmx:track("audio") if at then if NO_AUDIO_FORMAT[at.codec] then t:check(v.audio == nil and v.audioError and v.audioError.code == F.Err.UNSUPPORTED, label .. ": " .. at.codec .. " audio UNSUPPORTED (" .. tostring(v.audioError) .. ")") else C.audio(t, v, c, label, EXACT_AUDIO[at.codec], 30) end end end end do local v = assert(C.open(byName["h264_aac.flv"].data)) local vt, at = v.demuxer:track("video"), v.demuxer:track("audio") t:check(vt.config ~= nil and buffer.readu8(vt.config, 0) == 1, "h264: avcC in track.config") t:check(at.config ~= nil and buffer.len(at.config) >= 2, "h264: AudioSpecificConfig in track.config") local reordered = false for k = 1, v.demuxer:count(vt.id) do local p = v.demuxer:get(vt.id, k) if p and p.pts ~= p.dts then reordered = true end end t:check(reordered, "h264: B-frame composition times in the pts") t:eq(v.frameRate, 12, "h264: frame rate from onMetaData") local md = v.demuxer.meta.metadata t:check(md.width == 160 and md.height == 120 and md.videocodecid == 7 and md.audiocodecid == 10, "onMetaData: width, height, codec ids") t:check(type(md.encoder) == "string", "onMetaData: encoder string") local s = assert(C.open(byName["screen.flv"].data)) t:check(s.codec == "flashsv" and s.width == 64 and s.height == 48, "screen video: known codec, size from its first packet") end -- crafted files: VP6 (known, UNSUPPORTED, size from its key frame header), an enhanced FourCC tag, encrypted tags local function flv(tags: { { any } }): buffer local w = Bytes.writer(256) w:string("FLV") w:u8(1) w:u8(1) w:u32be(9) w:u32be(0) for _, tg in tags do local body: string = tg[3] w:u8(tg[1]) w:u24be(#body) w:u24be(tg[2]) w:u8(0) w:u24be(0) w:string(body) w:u32be(11 + #body) end return w:finish() end do -- VP6: frame type 1 / codec 4, adjustment 0x00, key frame header (marker 0, profile 3: no offset), 2 x 3 MBs local key = "\20\0" .. "\0\6" .. "\2\3\2\3" .. string.rep("\0", 8) local inter = "\36\0" .. "\128" .. string.rep("\0", 8) local b = flv({ { 9, 0, key }, { 9, 40, inter }, { 9, 80, key } }) local v = assert(F.video.open(b)) t:check(v.codec == "vp6f" and v.width == 48 and v.height == 32, "vp6: codec vp6f, " .. v.width .. "x" .. v.height .. " from its header") t:check(v.track.config ~= nil and buffer.len(v.track.config) == 1, "vp6: the adjustment byte as config") t:eq(v.demuxer:count(1), 3, "vp6: packets") local p = assert(v.demuxer:get(1, 1)) t:check(buffer.len(p.data) == #key - 2 and p.keyframe, "vp6: packet after the adjustment byte, keyframe") t:check(not assert(v.demuxer:get(1, 2)).keyframe, "vp6: inter frame") local _, e = v:nextFrame() t:check(e ~= nil and e.code == F.Err.UNSUPPORTED, "vp6: decoding UNSUPPORTED") v.demuxer:seek(0.06) t:eq(assert(v.demuxer:read(1)).pts, 0, "vp6: seek to the keyframe before") -- remux of a copied VP6 track keeps the adjustment byte local out = assert(F.save(v.demuxer, nil, { format = "flv" })) local v2 = assert(F.video.open(out)) t:check(v2.codec == "vp6f" and v2.demuxer:count(1) == 3 and buffer.tostring(assert(v2.demuxer:get(1, 1)).data) == buffer.tostring(p.data), "vp6: remuxed") -- enhanced FLV: an hvc1 sequence start and coded frames (with composition time) local hv = flv({ { 9, 0, "\144hvc1" .. "\1\2\3" }, { 9, 0, "\145hvc1" .. "\0\0\40" .. "\9\9" }, { 9, 40, "\163hvc1" .. "\7\7\7" } }) local h = assert(Flv.demux(Bytes.reader(hv))) local vt = assert(h:track("video")) t:check(vt.codec == "hevc" and vt.config ~= nil and buffer.len(vt.config) == 3, "enhanced: hvc1 -> hevc, sequence start as config") local p1, p2 = assert(h:get(1, 1)), assert(h:get(1, 2)) t:check(p1.pts == 40 and p1.dts == 0 and buffer.len(p1.data) == 2 and p1.keyframe, "enhanced: coded frames with composition time") t:check(p2.pts == 40 and buffer.len(p2.data) == 3 and not p2.keyframe, "enhanced: coded frames X") -- encrypted audio tags: the track says why local enc = flv({ { 8 + 32, 0, "\47\1\2\3" } }) local d = assert(Flv.demux(Bytes.reader(enc))) t:check(d:track("audio").unsupported ~= nil and d:count(2) == 0, "encrypted tags: dropped, track.unsupported") -- truncated: the tags before the cut local full = buffer.fromstring(byName["sorenson_mp3.flv"].data) local cut = buffer.create(buffer.len(full) // 2) buffer.copy(cut, 0, full, 0, buffer.len(cut)) local tr = assert(Flv.demux(Bytes.reader(cut))) t:check(tr.meta.truncated == true and tr:count(1) > 0, "truncated file: meta.truncated, packets before the cut") local _, e2 = Flv.demux(Bytes.reader(buffer.fromstring("RIFF0000AVI LIST"))) t:check(e2 ~= nil and e2.code == F.Err.CORRUPT, "not FLV: CORRUPT") end local SOURCES = { mp4 = Src.mp4, mkv = Src.mkv, ts = Src.ts } for _, c in Mx do local label = "write " .. c.name if c.err then t:check(c.source == "ts" and string.find(c.err, "UNSUPPORTED", 1, true) ~= nil, label .. ": " .. c.err) else local v = assert(C.open(c.data)) t:eq(#v.demuxer.tracks, #c.streams, label .. ": ffprobe sees the same tracks") C.packets(t, v.demuxer, c, label) if c.frames then frames(v, c, label) end local at = v.demuxer:track("audio") if at then C.audio(t, v, c, label, EXACT_AUDIO[at.codec], 30) end local src = assert(C.open(SOURCES[c.source] or byName[c.source].data)) local a, b = src.demuxer, v.demuxer local same = true for _, kind in { "video", "audio" } do local ta, tb = a:track(kind), b:track(kind) if (ta == nil) ~= (tb == nil) then same = false elseif ta and tb then if a:count(ta.id) ~= b:count(tb.id) then same = false else for k = 1, a:count(ta.id) do local p, q = assert(a:get(ta.id, k)), assert(b:get(tb.id, k)) if buffer.tostring(p.data) ~= buffer.tostring(q.data) or p.keyframe ~= q.keyframe then same = false end end end end end if c.name == "ts_aac.flv" then -- MPEG-TS AAC: ADTS frames re-framed as raw frames with a made AudioSpecificConfig local bt = assert(b:track("audio")) t:check(bt.codec == "aac" and bt.config ~= nil and b:count(bt.id) > a:count(assert(a:track("audio")).id), label .. ": ADTS split into raw AAC frames") else t:check(same, label .. ": the source's packets (" .. c.source .. ")") end local md = b.meta.metadata t:check(md and md.encoder == "rosetta" and md.filesize == #c.data, label .. ": onMetaData encoder and file size") end end local samples = {} for _, n in { "sorenson_mp3.flv", "h264_aac.flv", "sorenson_pcm.flv", "screen.flv" } do local d = byName[n].data table.insert(samples, string.sub(d, 1, math.min(#d, 30000))) end t:fuzz("open", C.fuzzOpen(Flv), samples, { mutations = 6, garbage = 100 }) t:done()