codecs/video: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:54:40 -05:00
1 parent 0bd7b87d2b
commit 8e78869f0e
36 files changed
+219 -809

No files matched your search

+3 -25
View File
@@ -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)