codecs/video: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
8e78869f0e
36 files changed
+219
-809
No files matched your search
@@ -1,19 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/video/Video: the video codec registry and the generic VideoDecoder. New in rosetta.
|
||||
--
|
||||
-- Registry: our codec ids ("vp8", "mjpeg", "mpeg1", "h264", "rawvideo", ...) with the containers' names for them
|
||||
-- (AVI / MOV FourCCs, Matroska CodecIDs, MPEG stream types) and, for the codecs we can decode, a constructor of a
|
||||
-- Types.VideoCodecDecoder (packets in, pictures out). Containers map their own ids through Video.fromFourcc /
|
||||
-- Video.fromMatroska so a track's codec is known even without a decoder (remuxing still works). Another codec plugs
|
||||
-- in with one line in BUILTIN (static: Roblox has no dynamic require) or at run time with Video.register(entry).
|
||||
--
|
||||
-- local Video = require("@rosetta/codecs/video/Video")
|
||||
-- Video.fromFourcc("MJPG") --> "mjpeg" Video.fromMatroska("V_VP8") --> "vp8"
|
||||
-- local dec = Video.decoder(track, opts) -- a VideoCodecDecoder | nil, Err (UNSUPPORTED: no decoder)
|
||||
-- local v = Video.open(demuxer, opts) -- a Types.VideoDecoder over a Types.Demuxer (formats' open())
|
||||
-- local v = Video.fromFrames(images, delays) -- a frames-backed VideoDecoder (animated GIF / APNG / WebP)
|
||||
--
|
||||
-- Never throws: bad data / missing codecs come back as nil, Err.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Yuv = require("@rosetta/codecs/image/Yuv")
|
||||
@@ -21,18 +7,15 @@ local Yuv = require("@rosetta/codecs/image/Yuv")
|
||||
local Video = {}
|
||||
|
||||
export type Entry = {
|
||||
id: string, -- our codec id
|
||||
name: string, -- a human name
|
||||
id: string,
|
||||
name: string,
|
||||
fourcc: { string }?, -- AVI biCompression / MOV sample entry FourCCs
|
||||
matroska: { string }?, -- Matroska / WebM CodecIDs
|
||||
-- new(track, opts) -> a decoder instance (nil = decode not supported)
|
||||
new: ((track: Types.Track, opts: any?) -> (Types.VideoCodecDecoder?, Err.Err?))?,
|
||||
[string]: any,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ simple decoders
|
||||
-- one picture per packet, no reordering: decodeOne(data, packet) -> Image | nil, Err (nil, nil: a packet with no
|
||||
-- picture to show, such as a hidden VP8 altref frame)
|
||||
-- decodeOne -> nil, nil: a packet with no picture to show (a hidden VP8 altref frame)
|
||||
function Video.simpleDecoder(decodeOne: (data: buffer, packet: Types.Packet) -> (Types.Image?, Err.Err?), resetFn: (() -> ())?): Types.VideoCodecDecoder
|
||||
local queue: { any } = {}
|
||||
local self = {}
|
||||
@@ -213,8 +196,7 @@ local function vp8Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecDec
|
||||
end), nil
|
||||
end
|
||||
|
||||
-- Motion JPEG: every packet one JPEG (or two fields), yuv420p straight from the planes (full range) when the layout
|
||||
-- allows, else rgba8
|
||||
-- Motion JPEG: yuv420p straight from the planes (full range) when the layout allows, else rgba8
|
||||
local function mjpegDecoder(_track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local Jpeg = require("@rosetta/codecs/image/Jpeg")
|
||||
local o: Jpeg.DecodeOptions = { yuv = true, maxPixels = opts and opts.maxPixels }
|
||||
@@ -240,20 +222,16 @@ local function mjpegEncode(img: Types.Image, opts: any?): (buffer?, Err.Err?)
|
||||
return Jpeg.encode(src, { quality = opts and opts.quality or 90 })
|
||||
end
|
||||
|
||||
-- MPEG-1 / MPEG-2 video (codecs/video/Mpeg2): pictures in decode order, reordered for display
|
||||
local function mpeg2Decoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local Mpeg2 = require("@rosetta/codecs/video/Mpeg2")
|
||||
return Mpeg2.newVideoDecoder(track, opts)
|
||||
end
|
||||
|
||||
-- H.264 / AVC (codecs/video/H264): avcC from track.config (MP4 / Matroska) or Annex B packets (MPEG-TS, AVI, raw .h264);
|
||||
-- pictures in display order with the pts of their packet
|
||||
local function h264Decoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local H264 = require("@rosetta/codecs/video/H264")
|
||||
return H264.newVideoDecoder(track, opts)
|
||||
end
|
||||
|
||||
-- H.263 / Sorenson Spark (codecs/video/H263): one picture per packet, no reordering
|
||||
local function h263Decoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local H263 = require("@rosetta/codecs/video/H263")
|
||||
local dec = H263.new({ flv = track.codec == "flv1", maxPixels = opts and opts.maxPixels })
|
||||
@@ -264,22 +242,18 @@ local function h263Decoder(track: Types.Track, opts: any?): (Types.VideoCodecDec
|
||||
end), nil
|
||||
end
|
||||
|
||||
-- Theora (codecs/video/Theora): track.config = the three headers, Xiph-laced (Matroska's CodecPrivate; video/ogv
|
||||
-- builds the same); one picture per packet, empty packets repeat the previous one
|
||||
local function theoraDecoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local Theora = require("@rosetta/codecs/video/Theora")
|
||||
return Theora.newVideoDecoder(track, opts)
|
||||
end
|
||||
|
||||
-- FLI / FLC, Cinepak, Microsoft Video 1, QuickTime RLE / SMC, Smacker (codecs/video/Legacy)
|
||||
local function legacyDecoder(id: string): (track: Types.Track, opts: any?) -> (Types.VideoCodecDecoder?, Err.Err?)
|
||||
return function(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
return require("@rosetta/codecs/video/Legacy").newDecoder(id, track, opts)
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ registry
|
||||
-- the codecs known by name (decoders where `new` is set). FourCCs compare case-sensitively first, then upper-cased.
|
||||
-- FourCCs compare case-sensitively first, then upper-cased
|
||||
local BUILTIN: { Entry } = {
|
||||
{ id = "vp8", name = "VP8", fourcc = { "VP80", "vp08" }, matroska = { "V_VP8" }, new = vp8Decoder },
|
||||
{ id = "mjpeg", name = "Motion JPEG", fourcc = { "MJPG", "mjpa", "mjpb", "jpeg", "AVRn", "AVDJ", "dmb1", "LJPG", "JPGL", "MJLS", "ijpg" }, matroska = { "V_MJPEG" }, new = mjpegDecoder, encode = mjpegEncode },
|
||||
@@ -299,7 +273,6 @@ local BUILTIN: { Entry } = {
|
||||
{ id = "flashsv", name = "Flash Screen Video", fourcc = { "FSV1" } },
|
||||
{ id = "flashsv2", name = "Flash Screen Video 2" },
|
||||
{ id = "png", name = "PNG", fourcc = { "png ", "MPNG" }, matroska = { "V_PNG" } },
|
||||
-- legacy inter-frame codecs (codecs/video/Legacy; rgba8 pictures, palettes from track.palette)
|
||||
{ id = "cinepak", name = "Cinepak", fourcc = { "cvid" }, new = legacyDecoder("cinepak") },
|
||||
{ id = "msvideo1", name = "Microsoft Video 1", fourcc = { "CRAM", "MSVC", "WHAM" }, new = legacyDecoder("msvideo1") },
|
||||
{ id = "qtrle", name = "QuickTime Animation (RLE)", fourcc = { "rle " }, new = legacyDecoder("qtrle") },
|
||||
@@ -312,7 +285,6 @@ local byId: { [string]: Entry } = {}
|
||||
local byFourcc: { [string]: string } = {}
|
||||
local byMatroska: { [string]: string } = {}
|
||||
|
||||
-- register(entry): adds or replaces a codec (by id); its fourcc / matroska names map to it
|
||||
function Video.register(entry: Entry): (boolean, Err.Err?)
|
||||
if type(entry) ~= "table" or type(entry.id) ~= "string" then
|
||||
return false, Err.new(Err.ARG, "register: needs an entry with an id")
|
||||
@@ -334,31 +306,25 @@ for _, e in BUILTIN do
|
||||
Video.register(e)
|
||||
end
|
||||
|
||||
-- the registry (read only by convention): id -> Entry
|
||||
Video.codecs = byId
|
||||
|
||||
-- fromFourcc(fourcc) -> codec id | nil
|
||||
function Video.fromFourcc(fcc: string): string?
|
||||
return byFourcc[fcc] or byFourcc[string.upper(fcc)] or byFourcc[string.lower(fcc)]
|
||||
end
|
||||
|
||||
-- fromMatroska(codecId) -> codec id | nil (V_MPEG4/ISO/AVC -> h264, ...)
|
||||
function Video.fromMatroska(id: string): string?
|
||||
return byMatroska[id]
|
||||
end
|
||||
|
||||
-- names(codecId) -> the Entry (FourCCs / Matroska ids for writers) | nil
|
||||
function Video.entry(id: string): Entry?
|
||||
return byId[id]
|
||||
end
|
||||
|
||||
-- canDecode(codecId) -> boolean
|
||||
function Video.canDecode(id: string): boolean
|
||||
local e = byId[id]
|
||||
return e ~= nil and e.new ~= nil
|
||||
end
|
||||
|
||||
-- decoder(track, opts?) -> a VideoCodecDecoder for the track's codec | nil, Err (UNSUPPORTED when none is registered)
|
||||
function Video.decoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local e = byId[track.codec]
|
||||
if not e or not e.new then
|
||||
@@ -371,7 +337,6 @@ function Video.decoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder
|
||||
return dec, err
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ VideoDecoder
|
||||
local function toRGBA(_self: any, f: any): Types.Image
|
||||
local img = if type(f) == "table" and f.image then f.image else f
|
||||
local out = Yuv.imageToRgba(img)
|
||||
@@ -379,22 +344,16 @@ local function toRGBA(_self: any, f: any): Types.Image
|
||||
end
|
||||
|
||||
export type OpenOptions = {
|
||||
rgba: boolean?, -- nextFrame returns rgba8 images (default false: the codec's native yuv420p)
|
||||
track: number?, -- the video track id (default: the first video track)
|
||||
rgba: boolean?,
|
||||
track: number?,
|
||||
maxPixels: number?,
|
||||
openAudio: ((demuxer: Types.Demuxer, track: Types.Track) -> (Types.Decoder?, Err.Err?))?, -- the api's audio hook
|
||||
audio: boolean?, -- false: do not open the audio track
|
||||
-- shrink yuv420p frames so the longer side is at most this many pixels (aspect kept; bilinear, Yuv.scale), before
|
||||
-- any rgba conversion. v.width / v.height stay the source size; frame.image has the output size. Default: none
|
||||
openAudio: ((demuxer: Types.Demuxer, track: Types.Track) -> (Types.Decoder?, Err.Err?))?,
|
||||
audio: boolean?,
|
||||
maxSide: number?,
|
||||
-- passed to codecs that support a cooperative yield point (H.264: codecs/video/H264 Options.yield)
|
||||
yield: (() -> ())?,
|
||||
[string]: any,
|
||||
}
|
||||
|
||||
-- open(demuxer, opts?) -> a Types.VideoDecoder over the demuxer's video track (and audio track through
|
||||
-- opts.openAudio). A video track without a decoder still opens: nextFrame returns nil, Err.UNSUPPORTED, and
|
||||
-- the demuxer (remuxing) and audio work.
|
||||
function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
|
||||
local o: OpenOptions = opts or {}
|
||||
local track: Types.Track? = nil
|
||||
@@ -435,7 +394,6 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
|
||||
if v.frames == 0 and v.frameRate > 0 and v.duration > 0 then
|
||||
v.frames = math.floor(v.duration * v.frameRate + 0.5)
|
||||
end
|
||||
-- audio
|
||||
if o.audio ~= false then
|
||||
local at = demuxer:track("audio")
|
||||
if at then
|
||||
@@ -469,8 +427,7 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
|
||||
local t = (pts or 0) / scale - start
|
||||
if t + 1e-9 >= skipUntil then
|
||||
if index < 0 then
|
||||
-- the first frame after a seek: its position in the stream (constant-rate: round(pts * fps);
|
||||
-- without a frame rate, counted from the seek)
|
||||
-- after a seek: constant-rate index = round(pts * fps), else counted from the seek
|
||||
index = if v.frameRate > 0 then math.max(0, math.floor(t * v.frameRate + 0.5)) else 0
|
||||
end
|
||||
index += 1
|
||||
@@ -531,9 +488,6 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
|
||||
return v, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ frames-backed
|
||||
-- fromFrames(images, delays, opts?) -> a VideoDecoder over full-canvas images (animated GIF / APNG / WebP after
|
||||
-- coalescing): delays[i] seconds (a number = every frame). opts.format / opts.loops / opts.meta are recorded.
|
||||
function Video.fromFrames(images: { Types.Image }, delays: (number | { number })?, opts: any?): (Types.VideoDecoder?, Err.Err?)
|
||||
if type(images) ~= "table" or #images == 0 or type(images[1]) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "fromFrames: needs a list of Images")
|
||||
|
||||
Reference in new issue
Block a user