diff --git a/docs/FORMATS.md b/docs/FORMATS.md index d612dc3..c98a249 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -27,12 +27,12 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | audio | voc | .voc | yes | yes | streaming: true, depths: 8, 16, lossy: false | `tests/audio/voc.luau` | | audio | vorbis | .ogg .oga | yes | yes | streaming: true, seek: true, lossy: true, channels: 255 | `tests/audio/vorbis.luau` | | audio | wav | .wav .wave .rf64 .bw64 | yes | yes | streaming: true, depths: 8, 16, 24, 32, float: true, rf64: true | `tests/audio/wav.luau` | -| video | avi | .avi | yes | yes | streaming: true, video: "mjpeg", "mpeg1", "rawvideo", audio: true, odml: true | `tests/video/avi.luau` | +| video | avi | .avi | yes | yes | streaming: true, video: "mjpeg", "mpeg1", "mpeg2", "rawvideo", audio: true, odml: true | `tests/video/avi.luau` | | video | h264 | .h264 .264 .avc .jsv .26l .jvt | yes | yes | streaming: true, video: "h264", depths: 8 | `tests/video/h264.luau` | -| video | mkv | .mkv .webm .mka .mk3d | yes | yes | streaming: true, remux: true, video: "vp8", "mjpeg", "mpeg1", "rawvideo", audio: true, subtitles: true | `tests/video/mkv.luau` | -| video | mp4 | .mp4 .m4v .mov .qt .3gp .3g2 .mj2 | yes | yes | streaming: true, remux: true, video: "mjpeg", "mpeg1", "h264", audio: true, fragments: true | `tests/video/mp4.luau` | -| video | mpegps | .mpg .mpeg .vob .m1v .mpv .dat | yes | - | streaming: true, video: "mpeg1", audio: true | `tests/video/mpegps.luau` | -| video | mpegts | .ts .m2ts .mts .tsv | yes | - | streaming: true, video: "mpeg1", "h264", audio: true | `tests/video/mpegts.luau` | +| video | mkv | .mkv .webm .mka .mk3d | yes | yes | streaming: true, remux: true, video: "vp8", "mjpeg", "mpeg1", "mpeg2", "rawvideo", audio: true, subtitles: true | `tests/video/mkv.luau` | +| video | mp4 | .mp4 .m4v .mov .qt .3gp .3g2 .mj2 | yes | yes | streaming: true, remux: true, video: "mjpeg", "mpeg1", "mpeg2", "h264", audio: true, fragments: true | `tests/video/mp4.luau` | +| video | mpegps | .mpg .mpeg .vob .m1v .mpv .dat | yes | - | streaming: true, video: "mpeg1", "mpeg2", audio: true | `tests/video/mpegps.luau` | +| video | mpegts | .ts .m2ts .mts .tsv | yes | - | streaming: true, video: "mpeg1", "mpeg2", "h264", audio: true | `tests/video/mpegts.luau` | | video | y4m | .y4m | yes | yes | streaming: true, video: "rawvideo", depths: 8, 10, 12, 16 | `tests/video/y4m.luau` | | model | 3mf | .3mf | yes | yes | materials: true, vertexColors: true, textures: true, units: true | `tests/model/3mf.luau` | | model | dae | .dae .zae | yes | yes | materials: true, polygons: true, skins: true, morphTargets: true, animation: true, cameras: true, lights: true, zae: true | `tests/model/dae.luau` | diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 429bc2a..7dfc437 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -285,7 +285,7 @@ local list: { Entry } = { extensions = { "avi" }, mime = { "video/x-msvideo", "video/avi" }, deps = { "core/Err", "core/Bytes", "Types", "containers/Avi", "containers/Demux", "codecs/video/Video", "codecs/video/Video/Source" }, - caps = { decode = true, encode = true, streaming = true, video = { "mjpeg", "mpeg1", "rawvideo" }, audio = true, odml = true }, + caps = { decode = true, encode = true, streaming = true, video = { "mjpeg", "mpeg1", "mpeg2", "rawvideo" }, audio = true, odml = true }, load = function() return require("@formats/video/avi") end, @@ -309,7 +309,7 @@ local list: { Entry } = { extensions = { "mkv", "webm", "mka", "mk3d" }, mime = { "video/x-matroska", "video/webm", "audio/x-matroska", "audio/webm" }, deps = { "core/Err", "core/Bytes", "Types", "containers/Ebml", "containers/Demux", "codecs/compress/Deflate", "codecs/video/Video", "codecs/video/Video/Source" }, - caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "rawvideo" }, audio = true, subtitles = true }, + caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "mpeg2", "rawvideo" }, audio = true, subtitles = true }, load = function() return require("@formats/video/mkv") end, @@ -321,7 +321,7 @@ local list: { Entry } = { extensions = { "mp4", "m4v", "mov", "qt", "3gp", "3g2", "mj2" }, mime = { "video/mp4", "video/quicktime", "video/3gpp" }, deps = { "core/Err", "core/Bytes", "Types", "containers/IsoBmff", "containers/Demux", "codecs/video/Video", "codecs/video/Video/Source" }, - caps = { decode = true, encode = true, streaming = true, remux = true, video = { "mjpeg", "mpeg1", "h264" }, audio = true, fragments = true }, + caps = { decode = true, encode = true, streaming = true, remux = true, video = { "mjpeg", "mpeg1", "mpeg2", "h264" }, audio = true, fragments = true }, load = function() return require("@formats/video/mp4") end, @@ -333,7 +333,7 @@ local list: { Entry } = { extensions = { "mpg", "mpeg", "vob", "m1v", "mpv", "dat" }, mime = { "video/mpeg" }, deps = { "core/Err", "core/Bytes", "Types", "containers/MpegPs", "containers/MpegEs", "containers/Demux", "codecs/video/Video" }, - caps = { decode = true, encode = false, streaming = true, video = { "mpeg1" }, audio = true }, + caps = { decode = true, encode = false, streaming = true, video = { "mpeg1", "mpeg2" }, audio = true }, load = function() return require("@formats/video/mpegps") end, @@ -345,7 +345,7 @@ local list: { Entry } = { extensions = { "ts", "m2ts", "mts", "tsv" }, mime = { "video/mp2t" }, deps = { "core/Err", "core/Bytes", "Types", "containers/MpegTs", "containers/MpegEs", "containers/Demux", "codecs/video/Video" }, - caps = { decode = true, encode = false, streaming = true, video = { "mpeg1", "h264" }, audio = true }, + caps = { decode = true, encode = false, streaming = true, video = { "mpeg1", "mpeg2", "h264" }, audio = true }, load = function() return require("@formats/video/mpegts") end, diff --git a/src/Formats/video/avi/init.luau b/src/Formats/video/avi/init.luau index fc46a32..1f0fdf0 100644 --- a/src/Formats/video/avi/init.luau +++ b/src/Formats/video/avi/init.luau @@ -25,7 +25,7 @@ local AviFormat = { extensions = { "avi" }, mime = { "video/x-msvideo", "video/avi" }, deps = { "core/Err", "core/Bytes", "Types", "containers/Avi", "containers/Demux", "codecs/video/Video", "codecs/video/Video/Source" }, - caps = { decode = true, encode = true, streaming = true, video = { "mjpeg", "mpeg1", "rawvideo" }, audio = true, odml = true }, + caps = { decode = true, encode = true, streaming = true, video = { "mjpeg", "mpeg1", "mpeg2", "rawvideo" }, audio = true, odml = true }, } -- ------------------------------------------------------------------------------------------------ types diff --git a/src/Formats/video/mkv/init.luau b/src/Formats/video/mkv/init.luau index 8e009e9..9e068c7 100644 --- a/src/Formats/video/mkv/init.luau +++ b/src/Formats/video/mkv/init.luau @@ -28,7 +28,7 @@ local Mkv = { extensions = { "mkv", "webm", "mka", "mk3d" }, mime = { "video/x-matroska", "video/webm", "audio/x-matroska", "audio/webm" }, deps = { "core/Err", "core/Bytes", "Types", "containers/Ebml", "containers/Demux", "codecs/compress/Deflate", "codecs/video/Video", "codecs/video/Video/Source" }, - caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "rawvideo" }, audio = true, subtitles = true }, + caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "mpeg2", "rawvideo" }, audio = true, subtitles = true }, } -- ------------------------------------------------------------------------------------------------ types diff --git a/src/Formats/video/mp4/init.luau b/src/Formats/video/mp4/init.luau index 48b57c7..eb643df 100644 --- a/src/Formats/video/mp4/init.luau +++ b/src/Formats/video/mp4/init.luau @@ -30,7 +30,7 @@ local Mp4 = { extensions = { "mp4", "m4v", "mov", "qt", "3gp", "3g2", "mj2" }, mime = { "video/mp4", "video/quicktime", "video/3gpp" }, deps = { "core/Err", "core/Bytes", "Types", "containers/IsoBmff", "containers/Demux", "codecs/video/Video", "codecs/video/Video/Source" }, - caps = { decode = true, encode = true, streaming = true, remux = true, video = { "mjpeg", "mpeg1", "h264" }, audio = true, fragments = true }, + caps = { decode = true, encode = true, streaming = true, remux = true, video = { "mjpeg", "mpeg1", "mpeg2", "h264" }, audio = true, fragments = true }, } -- ------------------------------------------------------------------------------------------------ types diff --git a/src/Formats/video/mpegps/init.luau b/src/Formats/video/mpegps/init.luau index 8708894..91ef418 100644 --- a/src/Formats/video/mpegps/init.luau +++ b/src/Formats/video/mpegps/init.luau @@ -1,10 +1,11 @@ --!strict -- video/mpegps: MPEG program streams (ISO/IEC 11172-1 system streams and 13818-1 program streams: .mpg / .mpeg / -- .vob / VCD .dat). New in formats-luau, on containers/MpegPs. Video PES payloads are joined into the elementary --- stream and cut into pictures (containers/MpegEs: one packet per picture with its sequence / GOP headers); a --- picture takes the PTS of the PES it starts in, pictures without one get theirs from the GOP's temporal references. --- Audio packets are the PES payloads (MP1 / MP2 / MP3 frames, AC-3, LPCM). open() returns a VideoDecoder: MPEG-1 --- video through the codecs/video/Video registry (MPEG-2 detected and reported), audio through the api's opts.openAudio hook. +-- stream and cut into pictures (containers/MpegEs: one packet per picture with its sequence / GOP headers, both +-- fields of a field-coded frame in one); a picture takes the PTS of the PES it starts in, pictures without one get +-- theirs from the GOP's temporal references. Audio packets are the PES payloads (MP1 / MP2 / MP3 frames, AC-3, LPCM). +-- open() returns a VideoDecoder: MPEG-1 / MPEG-2 video through the codecs/video/Video registry (MPEG-2 told by its +-- sequence extension), audio through the api's opts.openAudio hook. -- Timescale 90 kHz. Not supported: writing, H.264 in PS (packets are whole PES payloads), DVD navigation packets. -- ------------------------------------------------------------------------------------------------ requires @@ -23,7 +24,7 @@ local Ps = { extensions = { "mpg", "mpeg", "vob", "m1v", "mpv", "dat" }, mime = { "video/mpeg" }, deps = { "core/Err", "core/Bytes", "Types", "containers/MpegPs", "containers/MpegEs", "containers/Demux", "codecs/video/Video" }, - caps = { decode = true, encode = false, streaming = true, video = { "mpeg1" }, audio = true }, + caps = { decode = true, encode = false, streaming = true, video = { "mpeg1", "mpeg2" }, audio = true }, } -- ------------------------------------------------------------------------------------------------ types diff --git a/src/Formats/video/mpegts/init.luau b/src/Formats/video/mpegts/init.luau index 1cac2a4..4213838 100644 --- a/src/Formats/video/mpegts/init.luau +++ b/src/Formats/video/mpegts/init.luau @@ -2,8 +2,9 @@ -- video/mpegts: MPEG transport streams (ISO/IEC 13818-1: .ts / .m2ts segments, HLS). The demuxer is containers/MpegTs, -- absorbed from mpegts-luau (resync, PAT / PMT with CRC, continuity checks, PES reassembly, 33-bit PTS / DTS -- unwrapped); this format turns its units into a Demuxer (timescale 90 kHz; one PES = one packet: a picture as --- muxed by ffmpeg, one or more audio frames) and returns a VideoDecoder: MPEG-1 video through the codecs/video/Video --- registry (stream type 0x02 checked for an MPEG-2 sequence extension), H.264 when registered, audio (MP1 / MP2 / +-- muxed by ffmpeg, one or more audio frames) and returns a VideoDecoder: MPEG-1 / MPEG-2 video through the +-- codecs/video/Video registry (stream types 0x01 / 0x02, and private ones such as 0x80 with a video PES stream id, +-- told apart by an MPEG-2 sequence extension), H.264 when registered, audio (MP1 / MP2 / -- MP3, AAC in ADTS) through the api's opts.openAudio hook. Keyframes: MPEG-1 / 2 I-pictures, H.264 IDR access units. -- M2TS (192-byte packets with a 4-byte timestamp prefix) is read too. Not supported: writing, scrambled streams, -- PES without PTS for video before the first one (dropped). @@ -24,7 +25,7 @@ local Ts = { extensions = { "ts", "m2ts", "mts", "tsv" }, mime = { "video/mp2t" }, deps = { "core/Err", "core/Bytes", "Types", "containers/MpegTs", "containers/MpegEs", "containers/Demux", "codecs/video/Video" }, - caps = { decode = true, encode = false, streaming = true, video = { "mpeg1", "h264" }, audio = true }, + caps = { decode = true, encode = false, streaming = true, video = { "mpeg1", "mpeg2", "h264" }, audio = true }, } -- ------------------------------------------------------------------------------------------------ types diff --git a/tests/video/mpeg2.luau b/tests/video/mpeg2.luau new file mode 100644 index 0000000..9fe1724 --- /dev/null +++ b/tests/video/mpeg2.luau @@ -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()