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,16 +1,6 @@
|
||||
--!strict
|
||||
-- codecs/video/Video/Source: what a video writer (Matroska / WebM, AVI, Y4M, MP4) gets to write, from whatever it was given:
|
||||
-- a VideoDecoder (F.load / F.video.open of any video or animated image) or a Demuxer. Each output stream is either
|
||||
-- copied (the source track's packets, when the target container takes that codec: remuxing, e.g. VP8 into WebM,
|
||||
-- since VP8 is decode-only) or produced (decoded frames encoded with a registered encoder such as MJPEG, raw
|
||||
-- yuv420p frames, PCM audio from the audio Decoder). New in rosetta.
|
||||
--
|
||||
-- local streams, err = Source.streams(value, opts, {
|
||||
-- video = { copy = { vp8 = true }, encode = "mjpeg" }, -- copy: codec ids the container takes ("*" = any)
|
||||
-- audio = { copy = { opus = true, vorbis = true }, pcm = true },
|
||||
-- })
|
||||
-- -- streams[i] = { kind, codec, copied, width, height, frameRate, sampleRate, channels, bitsPerSample, config,
|
||||
-- -- track, packets = { { pts, duration (seconds), keyframe, data } } }
|
||||
-- codecs/video/Video/Source: what a video writer (Matroska / WebM, AVI, Y4M, MP4) gets to write, from a VideoDecoder
|
||||
-- or a Demuxer: each stream is copied (remuxed) or produced (decoded frames re-encoded, PCM audio).
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Yuv = require("@rosetta/codecs/image/Yuv")
|
||||
@@ -41,7 +31,6 @@ local function allows(set: { [string]: boolean }?, codec: string): boolean
|
||||
return set ~= nil and (set[codec] == true or set["*"] == true)
|
||||
end
|
||||
|
||||
-- the packets of a source track, times in seconds
|
||||
local function copyPackets(dmx: any, track: Types.Track): { OutPacket }
|
||||
local out = {}
|
||||
local scale = if track.timescale and track.timescale > 0 then track.timescale else 1
|
||||
@@ -72,8 +61,6 @@ local function trackStream(track: Types.Track, packets: { OutPacket }): Stream
|
||||
}
|
||||
end
|
||||
|
||||
-- encodeFrames(video, codec, opts) -> the video's frames encoded with the registry's encoder for codec ("rawvideo":
|
||||
-- yuv420p planes)
|
||||
local function encodeFrames(v: any, codec: string, opts: any): (Stream?, Err.Err?)
|
||||
local encode: ((img: Types.Image, opts: any) -> (buffer?, Err.Err?))? = nil
|
||||
if codec ~= "rawvideo" then
|
||||
@@ -140,7 +127,6 @@ local function encodeFrames(v: any, codec: string, opts: any): (Stream?, Err.Err
|
||||
} :: Stream, nil
|
||||
end
|
||||
|
||||
-- pcmFrom(decoder, opts) -> an s16le stream from an audio Decoder (chunks of opts.audioChunk frames, default 4096)
|
||||
local function pcmFrom(dec: any, opts: any): (Stream?, Err.Err?)
|
||||
local ch, rate = dec.channels, dec.sampleRate
|
||||
if type(ch) ~= "number" or ch < 1 or type(rate) ~= "number" or rate <= 0 then
|
||||
@@ -184,7 +170,6 @@ local function pcmFrom(dec: any, opts: any): (Stream?, Err.Err?)
|
||||
} :: Stream, nil
|
||||
end
|
||||
|
||||
-- streams(value, opts, want) -> { Stream } | nil, Err
|
||||
function Source.streams(value: any, opts: any?, want: Want): ({ Stream }?, Err.Err?)
|
||||
local o = opts or {}
|
||||
if type(value) ~= "table" or (value.kind ~= "video" and value.kind ~= "demuxer") then
|
||||
@@ -192,7 +177,6 @@ function Source.streams(value: any, opts: any?, want: Want): ({ Stream }?, Err.E
|
||||
end
|
||||
local dmx = if value.kind == "demuxer" then value else value.demuxer
|
||||
local out: { Stream } = {}
|
||||
-- video
|
||||
local vt: Types.Track? = if value.kind == "video" then value.track else (dmx and dmx:track("video"))
|
||||
local wv = want.video
|
||||
if wv and o.video ~= false then
|
||||
@@ -208,7 +192,6 @@ function Source.streams(value: any, opts: any?, want: Want): ({ Stream }?, Err.E
|
||||
return nil, Err.new(Err.UNSUPPORTED, "cannot write " .. vt.codec .. " video" .. (if wv.encode then "" else " (this container cannot take it and it cannot be re-encoded)"))
|
||||
end
|
||||
end
|
||||
-- audio
|
||||
local wa = want.audio
|
||||
if wa and o.audio ~= false then
|
||||
local at: Types.Track? = dmx and dmx:track("audio")
|
||||
@@ -232,7 +215,7 @@ function Source.streams(value: any, opts: any?, want: Want): ({ Stream }?, Err.E
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- rational(fps) -> num, den: integer rates as n / 1, NTSC rates as n * 1000 / 1001, else to a millisecond
|
||||
-- integer rates as n / 1, NTSC rates as n * 1000 / 1001, else to a millisecond
|
||||
function Source.rational(fps: number): (number, number)
|
||||
if fps ~= fps or fps <= 0 then
|
||||
return 25, 1
|
||||
@@ -247,9 +230,6 @@ function Source.rational(fps: number): (number, number)
|
||||
return math.floor(fps * 1000 + 0.5), 1000
|
||||
end
|
||||
|
||||
-- constantRate(stream, maxFps?) -> num, den, { OutPacket }: a video stream at a constant frame rate for the
|
||||
-- containers that have one (AVI, Y4M): the rate of the shortest frame (at most maxFps, default 100), each packet
|
||||
-- repeated to fill its duration (a GIF's 100 / 50 / 200 ms frames become 2, 1 and 4 frames at 20 fps)
|
||||
function Source.constantRate(s: Stream, maxFps: number?): (number, number, { OutPacket })
|
||||
local fd = math.huge
|
||||
for _, p in s.packets do
|
||||
@@ -277,8 +257,6 @@ function Source.constantRate(s: Stream, maxFps: number?): (number, number, { Out
|
||||
return num, den, out
|
||||
end
|
||||
|
||||
-- interleave(streams) -> { { stream index, packet } } merged by decode time (dts, else pts); each stream keeps its
|
||||
-- own order
|
||||
function Source.interleave(streams: { Stream }): { { any } }
|
||||
local order = {}
|
||||
local pos = table.create(#streams, 1)
|
||||
|
||||
@@ -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