diff --git a/docs/guide/video.md b/docs/guide/video.md index cbabe27..debbbec 100644 --- a/docs/guide/video.md +++ b/docs/guide/video.md @@ -185,6 +185,7 @@ Images and animated images save as videos too. | webm | VP8 / VP9 / AV1 copied (VP8 is decode only, so the source must be WebM-compatible) | Opus / Vorbis copied | | mkv | any codec copied, else MJPEG | copied, else PCM s16 | | mp4, mov | H.264 / HEVC / AV1 / MJPEG / MPEG-4 copied, else MJPEG | AAC / ALAC / Opus / FLAC / MP3 copied; PCM in MOV only | +| flv | H.264 (with its avcC) / Sorenson Spark / VP6 / screen video copied; nothing else (no FLV encoder) | AAC / MP3 / PCM / G.711 copied, else PCM s16 at 5512 / 11025 / 22050 / 44100 Hz | | avi | MJPEG copied, else MJPEG encoded (`quality`) | PCM s16 | | y4m | raw yuv420p | none | | h264 | an H.264 track copied as an Annex B stream | none | @@ -263,6 +264,36 @@ assert(C.register({ assert(C.fromFourcc("SLID") == "solid" and C.canDecode("solid")) ``` +## FLV and H.263 + +FLV (Flash Video) opens like any other container. Its onMetaData script tag is `v.meta.metadata` (duration, width, +height, framerate, codec ids, encoder, ...). Sorenson Spark (FLV1) and H.263 (in 3GP / MP4 `s263`, AVI `H263`) +decode bit-exactly as ffmpeg decodes them; H.264 tracks carry their avcC in `track.config` and their B-frame +composition times in the pts. VP6, screen video, Flash ADPCM, Nellymoser and Speex tracks are known (and remuxable) +but not decoded: `nextFrame` / `v.audioError` say UNSUPPORTED. Writing FLV copies H.264 + AAC / MP3 from MP4, +Matroska or FLV sources (FLV has no codec here that encodes). + +```luau +local v = assert(F.video.open(flvBytes)) -- Sorenson Spark + MP3 +assert(v.format == "flv" and v.codec == "flv1" and v.width == 64 and v.height == 48) +local md = v.meta.metadata -- onMetaData (AMF0) +assert(md.duration > 0 and md.framerate == 10 and md.videocodecid == 2) +local f = assert(v:nextFrame()) -- yuv420p (BT.601, limited range) +assert(f.keyframe and f.image.format == "yuv420p") +local sound = assert(F.ops.audio.toAudio(assert(v.audio, tostring(v.audioError)))) +assert(sound.sampleRate == 44100) +local h = assert(F.video.open(h263Bytes)) -- H.263 (QCIF) + AAC in 3GP +assert(h.codec == "h263" and h.width == 176 and h:nextFrame() ~= nil) +local flv = assert(F.convert(mp4Bytes, "flv")) -- remux: H.264 + AAC copied, onMetaData written +local back = assert(F.video.open(flv)) +assert(F.detect(flv) == "flv" and back.codec == "h264" and back.demuxer:track("audio").codec == "aac") +``` + +The H.263 decoder covers baseline H.263 (sub-QCIF .. 16CIF, GOB headers, unrestricted vectors) and the H.263+ +picture header ffmpeg's `h263p` encoder writes (custom sizes, the rounding type, unrestricted vectors, slice +structure); advanced prediction, PB-frames, advanced intra coding, the deblocking filter and the other H.263+ +options are UNSUPPORTED. + ## Playing in a game loop Decode only the frames that are due, and stop when this frame's time budget is used up; a slow frame then makes @@ -306,7 +337,8 @@ editable:WritePixelsBuffer(Vector2.zero, editable.Size, v:toRGBA(frame).pixels) | Format | Read | Write | Video codecs (decode) | |---|---|---|---| -| mp4 / mov / m4v / 3gp | yes | yes | H.264, MJPEG, MPEG-1 | +| mp4 / mov / m4v / 3gp | yes | yes | H.264, H.263, MJPEG, MPEG-1 | +| flv | yes | yes (copy) | Sorenson Spark, H.264 (VP6, screen video: known, not decoded) | | mkv / webm | yes | yes | VP8, MJPEG, MPEG-1, raw (+ H.264) | | avi | yes | yes | MJPEG, MPEG-1, raw (+ H.264) | | mpegts, mpegps | yes | - | MPEG-1, H.264 | diff --git a/tests/video/flv.luau b/tests/video/flv.luau index d1aeeff..70d9e01 100644 --- a/tests/video/flv.luau +++ b/tests/video/flv.luau @@ -213,7 +213,13 @@ for _, c in Mx do end end end - t:check(same, label .. ": the source's packets (" .. c.source .. ")") + 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 == "formats-luau" and md.filesize == #c.data, label .. ": onMetaData encoder and file size") end diff --git a/tools/bench.luau b/tools/bench.luau index b32ee01..df151e0 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -1078,3 +1078,37 @@ do jitTune(false) end end +-- ------------------------------------------------------------------------------------------- ws-video2-flv: fps +do + -- H.263 / Sorenson Spark (codecs/video/H263, tools/fixtures/h263.py): CIF mandelbrot zooms (1 Mbit/s, an I picture + -- every 12), one picture per decode, planes only (frameImage copies excluded) and with the yuv420p Image copies + local H263 = require("@formats/codecs/video/H263") + local Base64 = require("@formats/core/Base64") + local HB = require("@tests/gen/h263_bench") + local function run(tag: string) + for _, c in HB do + local file = Base64.decode(c.file) :: buffer + for _, images in { false, true } do + local n = #c.units + local dt = bench(string.format("%s %dx%d%s%s", c.name, c.width, c.height, if images then " +img" else "", tag), 3, c.width * c.height * 3 // 2 * n, function() + local dec = H263.new({ flv = c.flv }) + for _, u in c.units do + if images then + dec:image(file, u[1], u[2]) + else + dec:decode(file, u[1], u[2]) + end + end + end) + print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. n .. " frames)", n / dt)) + end + end + end + jitTune(false) + run("") + if runtime == "luajit" then + jitTune(true) + run(" (jit tuned)") + jitTune(false) + end +end diff --git a/tools/fixtures/_flv_encode.luau b/tools/fixtures/_flv_encode.luau index ec92a28..70ac28b 100644 --- a/tools/fixtures/_flv_encode.luau +++ b/tools/fixtures/_flv_encode.luau @@ -30,10 +30,11 @@ local JOBS = { { "mp4_h264_aac.flv", "mp4", Src.mp4 }, { "mkv_h264_mp3.flv", "mkv", Src.mkv }, { "ts_h264_aac.flv", "ts", Src.ts }, + { "ts_aac.flv", "ts", Src.ts, { video = false } }, -- ADTS AAC re-framed } for _, j in JOBS do local name, source, data = j[1], j[2], j[3] - local out, err = F.convert(buffer.fromstring(data), "flv") + local out, err = F.convert(buffer.fromstring(data), "flv", j[4]) if out then print(name .. " " .. source .. " " .. hex(out)) else diff --git a/tools/fixtures/docs_flv.py b/tools/fixtures/docs_flv.py new file mode 100644 index 0000000..347b850 --- /dev/null +++ b/tools/fixtures/docs_flv.py @@ -0,0 +1,29 @@ +# Docs inputs for the FLV / H.263 sections of docs/guide/video.md (tools/fixtures.py; tools/docs_check.py reads them): +# tiny ffmpeg clips (testsrc 64x48, 10 frames at 10 fps, with a sine track): FLV with Sorenson Spark + MP3, a 3GP with +# H.263 (QCIF, the H.263 size closest to the clip) and AAC. +# tests/gen/docs_flv.luau { files = { [name] = bytes }, vars = { flvBytes = "clip.flv", h263Bytes = "clip.3gp" } } +import importlib.util, os, shutil, subprocess, tempfile + +INPUTS = ["tools/docs_check.py"] + + +def generate(ctx): + spec = importlib.util.spec_from_file_location("docs_flv_check", os.path.join(ctx.root, "tools", "docs_check.py")) + dc = importlib.util.module_from_spec(spec) + spec.loader.exec_module(dc) + files = {} + with tempfile.TemporaryDirectory() as d: + for name, size, codec in ( + ("clip.flv", "64x48", ["-c:v", "flv", "-g", "5", "-c:a", "libmp3lame", "-b:a", "64k"]), + ("clip.3gp", "176x144", ["-c:v", "h263", "-g", "5", "-c:a", "aac", "-b:a", "32k", "-ar", "22050"]), + ): + src = ["-f", "lavfi", "-i", "testsrc=size=%s:rate=10:duration=1" % size, + "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=44100:duration=1"] + out = os.path.join(d, name) + argv = ["-v", "error", "-y"] + src + codec + ["-shortest", out] + if shutil.which("ffmpeg"): + subprocess.run(["ffmpeg"] + argv, check=True, capture_output=True) + else: + ctx.nix("ffmpeg", ["ffmpeg"] + argv) + files[name] = open(out, "rb").read() + dc.emit_inputs(ctx, files, {"flvBytes": "clip.flv", "h263Bytes": "clip.3gp"})