diff --git a/docs/guide/video.md b/docs/guide/video.md index cbabe27..996da6f 100644 --- a/docs/guide/video.md +++ b/docs/guide/video.md @@ -2,7 +2,7 @@ A video file opens as a **VideoDecoder**: frames come out one at a time, as Images, with their presentation time. Underneath sits a **Demuxer**: the container's tracks and their compressed packets. Video tracks decode through a -codec registry (H.264, VP8, MPEG-1, Motion-JPEG, raw), and the first audio track opens as an audio +codec registry (H.264, VP8, MPEG-1, MPEG-2, Motion-JPEG, raw), and the first audio track opens as an audio [Decoder](audio.md#decoder). Animated GIF, APNG and WebP open as videos too. [docs/video.md](../video.md) covers the internals and how to add a codec. @@ -263,6 +263,30 @@ assert(C.register({ assert(C.fromFourcc("SLID") == "solid" and C.canDecode("solid")) ``` +## MPEG-2 (DVD, broadcast TS) + +MPEG-1 and MPEG-2 video decode through one decoder (`codecs/video/Mpeg2`; `codecs/video/Mpeg1` is the same module): +Main profile, progressive and interlaced, frame and field pictures, 4:2:0 and 4:2:2. It reads `.mpg` / `.vob` +program streams, transport streams (stream type 0x02, or a private type such as 0x80 carrying a video PES), raw +`.m2v` elementary streams, and MPEG-2 tracks of MP4 / MOV, Matroska and AVI. The output equals `ffmpeg -idct int` +sample for sample (the integer IDCT of the IJG; ffmpeg's default IDCT differs from it by at most a level or two). + +```luau +local v = assert(F.video.open(tsBytes)) -- MPEG-TS: interlaced MPEG-2 + MP2 +assert(v.format == "mpegts" and v.codec == "mpeg2") +local f = assert(v:nextFrame()) +local src = f.image.meta.source +assert(src.format == "mpeg2" and src.interlaced and src.topFieldFirst) +assert(f.image.format == "yuv420p") -- both fields woven: the top field on the even lines +local a = assert(v.audio, tostring(v.audioError)) -- the MP2 track through the audio hook +assert(a.sampleRate == 44100) +``` + +Interlaced frames come out woven, as they were coded: deinterlace them yourself if needed (`src.interlaced`, +`src.topFieldFirst`). 4:2:2 streams (studio / XDCAM) are converted to 4:2:0 for the frame Image, averaging chroma +lines of the same field. A 720x576 interlaced stream decodes at about 130 frames per second in Luau on one desktop +core (`tools/bench.luau`): real time with room to spare on a PC, less so on phones. + ## 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,10 +330,10 @@ 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 | -| 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 | +| mp4 / mov / m4v / 3gp | yes | yes | H.264, MJPEG, MPEG-1, MPEG-2 | +| mkv / webm | yes | yes | VP8, MJPEG, MPEG-1, MPEG-2, raw (+ H.264) | +| avi | yes | yes | MJPEG, MPEG-1, MPEG-2, raw (+ H.264) | +| mpegts, mpegps | yes | - | MPEG-1, MPEG-2, H.264 | | y4m | yes | yes | raw | | h264 (Annex B) | yes | yes (copy) | H.264 | | gif, png (APNG), webp | as video | as video via the image writers | frames | diff --git a/docs/video.md b/docs/video.md index 184ca45..8f5b6f5 100644 --- a/docs/video.md +++ b/docs/video.md @@ -28,6 +28,8 @@ F.convert(gif, "avi") -- animated images become videos (un | `codecs/video/Video/Source` | what writers write: copied packets or frames encoded by a registry encoder, PCM from the audio | | `codecs/image/Yuv` | yuv420p <-> rgba8 (BT.601 / 709, limited / full range from `image.meta.yuv`), range remap | | `codecs/image/Vp8`, `Jpeg`, `Mpeg1` | the decoders (and the JPEG encoder) | +| `codecs/video/Mpeg2` (`Mpeg1` is the same module), `H264` | MPEG-1 / MPEG-2 and H.264 video decoders | +| `codecs/video/Dsp` | 8x8 IDCT (bit-exact with `ffmpeg -idct int`) and half-pel motion compensation for the MPEG-style codecs | | `video/` | the formats: demux into `containers/Demux`, `open` = `Video.open(demuxer, opts)`, `encode` via Source | Audio tracks reach the audio formats through the api hook `opts.openAudio(demuxer, track)` (a format never requires diff --git a/tools/fixtures/docs_av.py b/tools/fixtures/docs_av.py index 8019408..9beb3a2 100644 --- a/tools/fixtures/docs_av.py +++ b/tools/fixtures/docs_av.py @@ -1,8 +1,8 @@ # Docs inputs for the audio / music / video / subtitle guide pages (tools/fixtures.py; tools/docs_check.py reads them): # tiny ffmpeg test clips (testsrc 64x48, 10 frames at 10 fps, with a sine track): MP4 (H.264 + AAC), AVI (MJPEG + -# PCM), WebM (VP8 + Vorbis); a MIDI file written here (SMF 1: a tempo track and a melody), the SoundFont of -# tools/fixtures/sf2.py (build_sf2), a ProTracker module from tools/fixtures/mod.py (song_basic), and SRT / WebVTT / -# ASS subtitles. +# PCM), WebM (VP8 + Vorbis), MPEG-TS (interlaced MPEG-2 + MP2); a MIDI file written here (SMF 1: a tempo track and a +# melody), the SoundFont of tools/fixtures/sf2.py (build_sf2), a ProTracker module from tools/fixtures/mod.py +# (song_basic), and SRT / WebVTT / ASS subtitles. # tests/gen/docs_av.luau { files = { [name] = bytes }, vars = { mp4Bytes = "clip.mp4", ... } } import importlib.util, os, struct, tempfile @@ -91,6 +91,9 @@ def generate(ctx): "-c:a", "aac", "-b:a", "64k"]), ("clip.avi", ["-c:v", "mjpeg", "-q:v", "4", "-pix_fmt", "yuvj420p", "-c:a", "pcm_s16le"]), ("clip.webm", ["-c:v", "libvpx", "-b:v", "200k", "-g", "5", "-c:a", "libvorbis", "-q:a", "2"]), + ("clip.ts", ["-c:v", "mpeg2video", "-b:v", "300k", "-g", "5", "-bf", "2", "-vf", "setfield=tff", + "-flags", "+ilme+ildct", + "-c:a", "mp2", "-b:a", "64k"]), ): out = os.path.join(d, name) ctx.nix("ffmpeg", ["ffmpeg", "-v", "error", "-y"] + src + codec + ["-shortest", out]) @@ -107,6 +110,7 @@ def generate(ctx): "mp4Bytes": "clip.mp4", "aviBytes": "clip.avi", "webmBytes": "clip.webm", + "tsBytes": "clip.ts", "midiBytes": "tune.mid", "sf2Bytes": "bank.sf2", "modBytes": "song.mod",