--!strict -- video/y4m: YUV4MPEG2 (the mjpegtools / ffmpeg raw video stream). New in formats-luau. -- Read: the stream header (W H F I A C X tags) and every FRAME into a raw-video Demuxer; open() returns a VideoDecoder -- with yuv420p frames (C420 / C420jpeg / C420mpeg2 / C420paldv as they are; C422 / C444 chroma averaged down; Cmono -- with neutral chroma; 9..16-bit samples (C420p10, ...) scaled to 8 bits). Y4m.demux(r) gives the Demuxer. -- Write: yuv420p 8-bit frames ("C420jpeg", as ffmpeg writes for yuv420p) from any video or animated image; rgba8 -- frames are converted (BT.601 limited range); uneven frame times (a GIF's) become a constant rate with repeated frames. Not supported: interlaced field handling (the I tag is kept in meta), -- alpha (C444alpha drops it). -- ------------------------------------------------------------------------------------------------ requires local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") local Types = require("@formats/Types") local Demux = require("@formats/containers/Demux") local Video = require("@formats/codecs/Video") local Source = require("@formats/codecs/Video/Source") -- ------------------------------------------------------------------------------------------------ contract local Y4m = { name = "y4m", category = "video", extensions = { "y4m" }, mime = { "video/x-yuv4mpeg" }, deps = { "core/Err", "core/Bytes", "Types", "containers/Demux", "codecs/Video", "codecs/Video/Source" }, caps = { decode = true, encode = true, streaming = true, video = { "rawvideo" }, depths = { 8, 10, 12, 16 } }, } -- ------------------------------------------------------------------------------------------------ types export type OpenOptions = { rgba: boolean?, -- nextFrame returns rgba8 (default false: yuv420p) maxPixels: number?, -- default 2^26 } export type EncodeOptions = { frameRate: { number }?, -- { num, den } for the F tag, one frame per source frame (default: the constant rate of the -- shortest frame, longer frames repeated) maxFps: number?, -- the highest rate uneven frame times may need (default 100) } -- ------------------------------------------------------------------------------------------------ constants local MAGIC = "YUV4MPEG2 " -- C tag -> pixel format, bits local COLOR: { [string]: { any } } = { ["420jpeg"] = { "yuv420p", 8 }, ["420paldv"] = { "yuv420p", 8 }, ["420mpeg2"] = { "yuv420p", 8 }, ["420"] = { "yuv420p", 8 }, ["422"] = { "yuv422p", 8 }, ["444"] = { "yuv444p", 8 }, ["444alpha"] = { "yuva444p", 8 }, mono = { "gray", 8 }, } for _, b in { 9, 10, 12, 14, 16 } do COLOR["420p" .. b] = { "yuv420p", b } COLOR["422p" .. b] = { "yuv422p", b } COLOR["444p" .. b] = { "yuv444p", b } COLOR["mono" .. b] = { "gray", b } end -- ------------------------------------------------------------------------------------------------ sniff function Y4m.sniff(r: Bytes.Reader): number return if r:peek(10) == MAGIC then 1 else 0 end -- ------------------------------------------------------------------------------------------------ decode local function frameSize(pf: string, bits: number, w: number, h: number): number local bps = if bits > 8 then 2 else 1 local cw, ch = (w + 1) // 2, (h + 1) // 2 if pf == "yuv420p" then return (w * h + 2 * cw * ch) * bps elseif pf == "yuv422p" then return (w * h + 2 * cw * h) * bps elseif pf == "yuv444p" then return w * h * 3 * bps elseif pf == "yuva444p" then return w * h * 4 * bps end return w * h * bps end -- demux(r, opts?) -> the raw-video Demuxer | nil, Err function Y4m.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?) local buf, base, limit = r.buf, r.base, r.limit local nl = -1 for i = base, math.min(limit, base + 4096) - 1 do if buffer.readu8(buf, i) == 10 then nl = i break end end if limit - base < 10 or buffer.readstring(buf, base, 10) ~= MAGIC then return nil, Err.new(Err.CORRUPT, "not a YUV4MPEG2 stream", base) end if nl < 0 then return nil, Err.new(Err.TRUNCATED, "no end of the stream header", base) end local header = buffer.readstring(buf, base + 10, nl - base - 10) local w, h, fn, fd = 0, 0, 25, 1 local ctag, itag, atag = "420jpeg", "p", "0:0" local extra = {} for tok in string.gmatch(header, "%S+") do local k, v = string.sub(tok, 1, 1), string.sub(tok, 2) if k == "W" then w = tonumber(v) or 0 elseif k == "H" then h = tonumber(v) or 0 elseif k == "F" then local a, b = string.match(v, "^(%d+):(%d+)$") if a and b and tonumber(a) > 0 and tonumber(b) > 0 then fn, fd = tonumber(a) :: number, tonumber(b) :: number end elseif k == "C" then ctag = v elseif k == "I" then itag = v elseif k == "A" then atag = v elseif k == "X" then table.insert(extra, v) end end local color = COLOR[ctag] if not color then return nil, Err.new(Err.UNSUPPORTED, "colour space C" .. ctag) end if w <= 0 or h <= 0 or w > 65535 or h > 65535 then return nil, Err.new(Err.CORRUPT, "bad frame size " .. w .. "x" .. h) end local maxPixels = opts and opts.maxPixels or 2 ^ 26 if w * h > maxPixels then return nil, Err.new(Err.LIMIT, "frame of " .. w .. "x" .. h .. " pixels") end local pf, bits = color[1], color[2] local size = frameSize(pf, bits, w, h) local track: Types.Track = { id = 1, kind = "video", codec = "rawvideo", codecTag = "C" .. ctag, timescale = fn / fd, width = w, height = h, frameRate = fn / fd, pixelFormat = pf, bitDepth = bits, default = true, } local meta = { interlace = itag, aspect = atag, extra = extra, colorTag = ctag } local dmx = Demux.new("y4m", { track }, buf, meta) local pos = nl + 1 local k = 0 while pos + 5 <= limit do if buffer.readstring(buf, pos, 5) ~= "FRAME" then return nil, Err.new(Err.CORRUPT, "expected FRAME", pos) end local e = -1 for i = pos + 5, math.min(limit, pos + 4096) - 1 do if buffer.readu8(buf, i) == 10 then e = i break end end if e < 0 then break end local data = e + 1 if data + size > limit then break -- a truncated last frame is dropped end dmx:add(1, k, k, 1, true, data, size) k += 1 pos = data + size end dmx:finish() return dmx, nil end -- ------------------------------------------------------------------------------------------------ encode -- encode(video, opts?) -> buffer | nil, Err function Y4m.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?) local streams, err = Source.streams(value, { audio = false }, { video = { copy = nil, encode = "rawvideo" } }) if not streams then return nil, err end local s = streams[1] local fn, fd, packets = Source.constantRate(s, opts and opts.maxFps) if opts and type(opts.frameRate) == "table" then fn, fd, packets = opts.frameRate[1], opts.frameRate[2], s.packets end s.packets = packets local w = Bytes.writer(64 + #s.packets * (buffer.len(s.packets[1].data) + 6)) w:string(string.format("YUV4MPEG2 W%d H%d F%d:%d Ip A1:1 C420jpeg XYSCSS=420JPEG\n", s.width or 0, s.height or 0, fn, fd)) for _, p in s.packets do w:string("FRAME\n") w:bytes(p.data) end return w:finish(), nil end -- ------------------------------------------------------------------------------------------------ open -- open(r, opts?) -> a VideoDecoder | nil, Err function Y4m.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?) local dmx, err = Y4m.demux(r, opts) if not dmx then return nil, err end return Video.open(dmx, opts) end return Y4m