merge ws/video2-legacy (FLIC, Cinepak, MSV1, QT RLE / SMC, Smacker; video/fli, video/smk)

This commit is contained in:
4DBug committed 2026-10-31 15:23:13 -05:00
commit ab7d0b2034
32 files changed
+4786 -5

No files matched your search

+4
View File
@@ -110,6 +110,10 @@ local function videoTrack(s: Avi.Stream, id: number): Types.Track
t.pixelFormat = "yuv420p"
end
end
local bi = Avi.bitmapInfo(f)
if bi and bi.palette then
t.palette = bi.palette -- 1..8-bit codecs (Microsoft Video 1, Cinepak, QuickTime RLE / SMC in AVI, ...)
end
local size = buffer.readu32(f, 0)
if size > 40 and buffer.len(f) > 40 then
local c = buffer.create(buffer.len(f) - 40)
+157
View File
@@ -0,0 +1,157 @@
--!strict
-- video/fli: Autodesk Animator FLI / Animator Pro FLC / FLX animations. New in formats-luau, written from the FLIC
-- file format description (Jim Kent, "FLC - Animator Pro Flic Files"; MultimediaWiki "FLIC").
-- Read: the 128-byte header (FLI 0xAF11: speed in 1/70 s; FLC 0xAF12 / DTA 0xAF44: speed in ms; depth 8, 15, 16, 24:
-- an FLC that says 16 bits holds 5-5-5 pixels, as Animator Pro wrote them), prefix and other non-frame chunks skipped,
-- every frame chunk (0xF1FA, and 0xF5FA) a packet of a Demuxer; open() returns a VideoDecoder whose frames are rgba8
-- (codecs/video/Legacy: palette chunks, BRUN, LC, SS2, BLACK, COPY, the deep-colour DTA chunks). The ring frame (the
-- extra frame after `frames` that leads back to the first) is not shown. Fli.demux(r) gives the Demuxer.
-- Not supported: writing, 1-bit FLICs, the X-COM / Magic Carpet game variants (audio chunks, 6-bit COLOR_256).
-- ------------------------------------------------------------------------------------------------ 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/Video")
-- ------------------------------------------------------------------------------------------------ contract
local Fli = {
name = "fli",
category = "video",
extensions = { "fli", "flc", "flx" },
mime = { "video/x-fli", "video/fli", "video/x-flc" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Demux", "codecs/video/Video" },
caps = { decode = true, encode = false, streaming = true, video = { "flic" }, depths = { 8, 15, 16, 24 } },
}
-- ------------------------------------------------------------------------------------------------ types
export type OpenOptions = {
rgba: boolean?, -- (frames are rgba8 already)
maxPixels: number?, -- default 2^26
ringFrame: boolean?, -- true: also show the ring frame at the end (as ffmpeg does)
}
-- ------------------------------------------------------------------------------------------------ constants
local HEADER = 128
local FLI, FLC, DTA = 0xAF11, 0xAF12, 0xAF44
local FRAME, FRAME2 = 0xF1FA, 0xF5FA
-- ------------------------------------------------------------------------------------------------ sniff
function Fli.sniff(r: Bytes.Reader): number
local s = r:peek(18)
if #s < 18 then
return 0
end
local magic = string.byte(s, 5) + string.byte(s, 6) * 256
if magic ~= FLI and magic ~= FLC and magic ~= DTA then
return 0
end
local w = string.byte(s, 9) + string.byte(s, 10) * 256
local h = string.byte(s, 11) + string.byte(s, 12) * 256
if w > 4096 or h > 4096 then
return 0
end
return 0.9
end
-- ------------------------------------------------------------------------------------------------ decode
-- demux(r, opts?) -> the Demuxer | nil, Err
function Fli.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
local b, base, limit = r.buf, r.base, r.limit
local n = limit - base
if n < HEADER then
return nil, Err.new(Err.TRUNCATED, "file shorter than the 128-byte header")
end
local hr = Bytes.reader(b, base, HEADER)
hr:skip(4)
local magic = hr:u16le()
local frames = hr:u16le()
local w, h = hr:u16le(), hr:u16le()
local depth = hr:u16le()
hr:skip(2)
local speed = hr:u32le()
if hr.err then
return nil, hr.err
end
if magic ~= FLI and magic ~= FLC and magic ~= DTA then
return nil, Err.new(Err.CORRUPT, string.format("bad magic 0x%04x", magic), 4)
end
if magic == FLI then
speed = speed % 65536
end
if w == 0 or h == 0 then
return nil, Err.new(Err.CORRUPT, "no picture size")
end
local max = (opts and opts.maxPixels) or 2 ^ 26
if w * h > max then
return nil, Err.new(Err.LIMIT, w .. " x " .. h .. " over maxPixels")
end
if magic == FLI or depth == 0 then
depth = 8
elseif magic == FLC and depth == 16 then
depth = 15 -- Animator Pro's 16-bit FLCs hold 5-5-5 pixels
end
if speed == 0 then
speed = 5
end
local scale = if magic == FLI then 70 else 1000
local track: Types.Track = {
id = 1,
kind = "video",
codec = "flic",
codecTag = string.format("0x%04X", magic),
timescale = scale,
width = w,
height = h,
frameRate = scale / speed,
default = true,
bitsPerPixel = depth,
}
-- the frame chunks
local offs, sizes = {}, {}
local p = base + HEADER
while p + 6 <= limit do
local size = buffer.readu32(b, p)
local cm = buffer.readu16(b, p + 4)
if size < 6 then
break
end
if (cm == FRAME or cm == FRAME2) and size > 6 then
if p + size > limit then
break -- a truncated last frame
end
table.insert(offs, p)
table.insert(sizes, size)
end
p += size
end
local count = #offs
if frames > 0 and count > frames and not (opts and opts.ringFrame) then
count = frames -- the ring frame is not a frame of the animation
end
local dmx = Demux.new("fli", { track }, b, {
type = magic,
frames = frames,
depth = depth,
speed = speed,
ringFrame = #offs > frames and frames > 0,
})
for i = 1, count do
dmx:add(1, (i - 1) * speed, nil, speed, i == 1, offs[i], sizes[i])
end
dmx:finish()
return dmx, nil
end
-- ------------------------------------------------------------------------------------------------ open
-- open(r, opts?) -> a VideoDecoder | nil, Err
function Fli.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
local dmx, err = Fli.demux(r, opts)
if not dmx then
return nil, err
end
return Video.open(dmx, opts :: Video.OpenOptions?)
end
return Fli
+39
View File
@@ -9,6 +9,8 @@ local Types = require("@formats/Types")
local Ebml = require("@formats/containers/Ebml")
local Demux = require("@formats/containers/Demux")
local Video = require("@formats/codecs/video/Video")
local Avi = require("@formats/containers/Avi")
local QtPalette = require("@formats/containers/IsoBmff/QtPalette")
local Deflate = require("@formats/codecs/compress/Deflate")
local Parse = {}
@@ -152,6 +154,39 @@ local function codecFor(id: string, kind: string, bits: number?): string
end
Parse.codecFor = codecFor
-- V_MS/VFW/FOURCC (CodecPrivate = a BITMAPINFOHEADER) and V_QUICKTIME (CodecPrivate = a QuickTime sample
-- description, sometimes without its size field): the codec from the FourCC, the depth and palette, the extradata
local function vfwOrQuickTime(t: any, codecId: string, priv: buffer)
local n = buffer.len(priv)
if codecId == "V_MS/VFW/FOURCC" then
local bi = Avi.bitmapInfo(priv)
if bi then
t.codec = Video.fromFourcc(bi.compression) or t.codec
t.codecTag = bi.compression
t.bitsPerPixel = bi.bits
t.palette = bi.palette
t.config = bi.extra
end
elseif codecId == "V_QUICKTIME" and n >= 21 then
local desc = priv
if Video.fromFourcc(buffer.readstring(priv, 0, 4)) then
desc = buffer.create(n + 4)
local size = n + 4
buffer.writeu8(desc, 0, size // 16777216 % 256)
buffer.writeu8(desc, 1, size // 65536 % 256)
buffer.writeu8(desc, 2, size // 256 % 256)
buffer.writeu8(desc, 3, size % 256)
buffer.copy(desc, 4, priv, 0, n)
end
local fcc = buffer.readstring(desc, 4, 4)
t.codec = Video.fromFourcc(fcc) or t.codec
t.codecTag = fcc
local depth, pal = QtPalette.read(desc, 0, buffer.len(desc), t.codec)
t.bitsPerPixel = depth
t.palette = pal
end
end
type TrackInfo = Types.Track & { strip: buffer?, lacing: boolean?, defaultDurationNs: number? }
local function parseVideo(buf: buffer, off: number, len: number, t: any)
@@ -278,6 +313,10 @@ local function parseTracks(buf: buffer, off: number, len: number, scaleNs: numbe
t.kind = if ttype == 1 then "video" elseif ttype == 2 then "audio" elseif ttype == 17 then "subtitle" else "data"
t.codecTag = codecId
t.codec = codecFor(codecId, t.kind, t.bitsPerSample)
local cfg: buffer? = t.config
if t.kind == "video" and cfg then
vfwOrQuickTime(t, codecId, cfg)
end
if t.kind == "audio" then
t.sampleRate = t.sampleRate or 8000
t.channels = t.channels or 1
+2 -2
View File
@@ -27,8 +27,8 @@ local Mkv = {
category = "video",
extensions = { "mkv", "webm", "mka", "mk3d" },
mime = { "video/x-matroska", "video/webm", "audio/x-matroska", "audio/webm" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ebml", "containers/Demux", "codecs/compress/Deflate", "codecs/video/Video", "codecs/video/Video/Source" },
caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "mpeg2", "rawvideo" }, audio = true, subtitles = true },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ebml", "containers/Demux", "containers/Avi", "containers/IsoBmff/QtPalette", "codecs/compress/Deflate", "codecs/video/Video", "codecs/video/Video/Source" },
caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "mpeg2", "theora", "rawvideo" }, audio = true, subtitles = true },
}
-- ------------------------------------------------------------------------------------------------ types
+2
View File
@@ -133,6 +133,8 @@ function Mp4.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
bitsPerSample = it.bitsPerSample,
config = it.config,
default = true,
bitsPerPixel = it.bitsPerPixel,
palette = it.palette,
}
if it.kind == "video" then
if not Video.entry(t.codec) then
+123
View File
@@ -0,0 +1,123 @@
--!strict
--!native
--!optimize 2
-- video/smk/Audio: Smacker's packed audio (SMKA: DPCM with Huffman-coded deltas), the one audio codec only Smacker
-- files carry. Written from the format description (MultimediaWiki "Smacker"). A packet: the unpacked size (u32),
-- then LSB-first bits: data flag, stereo, 16-bit, one 8-bit Huffman tree per channel and byte (codecs/video/Legacy/
-- Smacker trees: 1, 2 or 4), the first sample(s) raw (16-bit ones byte-swapped, the right channel first), then per
-- sample a delta added with wraparound (the low byte's tree, and the high byte's for 16 bits). Raw PCM tracks (u8 /
-- s16le) are converted here too. Output: interleaved floats (u8: (v - 128) / 128, s16: v / 32768), as ffmpeg gives
-- them. Never throws: trees are bounded and every read past the packet gives zero bits.
local Smacker = require("@formats/codecs/video/Legacy/Smacker")
local readu8, readi16 = buffer.readu8, buffer.readi16
local Audio = {}
-- decode(data, channels, bits, packed) -> frames, floats (1-based interleaved) | nil, message
function Audio.decode(d: buffer, channels: number, bits: number, packed: boolean): (number?, { number } | string)
local n = buffer.len(d)
if not packed then
local bps = bits // 8
local frames = n // (bps * channels)
local count = frames * channels
local out = table.create(count, 0)
if bps == 1 then
for i = 0, count - 1 do
out[i + 1] = (readu8(d, i) - 128) / 128
end
else
for i = 0, count - 1 do
out[i + 1] = readi16(d, i * 2) / 32768
end
end
return frames, out
end
if n <= 4 then
return nil, "packet too small"
end
local size = buffer.readu32(d, 0)
if size > 2 ^ 24 then
return nil, "packet too big"
end
local br = Smacker.bits(d, 4, n - 4)
local bit, read, walk = Smacker.bit, Smacker.read, Smacker.walk
if bit(br) == 0 then
return 0, {} -- no data
end
local stereo = bit(br)
local wide = bit(br)
if (stereo == 1) ~= (channels == 2) or (wide == 1) ~= (bits == 16) then
return nil, "packet does not match the track's format"
end
local bytes = wide + 1
local ch = stereo + 1
if size % (ch * bytes) ~= 0 then
return nil, "unpacked size is not a whole number of samples"
end
local trees: { { number } } = {}
for k = 1, 2 ^ (wide + stereo) do
bit(br)
local t, msg = Smacker.tree8(br)
if not t then
return nil, msg or "bad tree"
end
bit(br)
trees[k] = t
end
if br.nbits - br.pos < ch * bytes * 8 then
return nil, "truncated packet"
end
-- whether any tree needs bits (else every delta is a constant and no end check applies)
local coded = false
for _, t in trees do
if #t > 1 then
coded = true
end
end
local frames = size // (ch * bytes)
local total = frames * ch
local out = table.create(total, 0)
if wide == 1 then
local pred = { 0, 0 }
for c = ch, 1, -1 do
local v = read(br, 16)
pred[c] = (v // 256) + (v % 256) * 256
end
for c = 1, math.min(ch, total) do
local v = pred[c]
out[c] = (if v >= 32768 then v - 65536 else v) / 32768
end
for i = ch, total - 1 do
local c = if ch == 2 then i % 2 else 0
if coded and br.pos > br.nbits then
return nil, "truncated packet"
end
local lo = walk(trees[2 * c + 1], br)
local hi = walk(trees[2 * c + 2], br)
local v = (pred[c + 1] + lo + hi * 256) % 65536
pred[c + 1] = v
out[i + 1] = (if v >= 32768 then v - 65536 else v) / 32768
end
else
local pred = { 0, 0 }
for c = ch, 1, -1 do
pred[c] = read(br, 8)
end
for c = 1, math.min(ch, total) do
out[c] = (pred[c] - 128) / 128
end
for i = ch, total - 1 do
local c = if ch == 2 then i % 2 else 0
if br.pos > br.nbits then
return nil, "truncated packet"
end
local v = (pred[c + 1] + walk(trees[c + 1], br)) % 256
pred[c + 1] = v
out[i + 1] = (v - 128) / 128
end
end
return frames, out
end
return Audio
+320
View File
@@ -0,0 +1,320 @@
--!strict
-- video/smk: RAD Game Tools' Smacker video (SMK2 and SMK4). New in formats-luau, written from the format description
-- (MultimediaWiki "Smacker").
-- Read: the header (size, frame count, frame time: > 0 ms, < 0 units of 10 us; the ring-frame flag), up to 7 audio
-- tracks (rate, packed / 16-bit / stereo flags), the frame sizes and flags, the four Huffman trees; per frame a palette
-- update (skip / copy-from-old / new 6-bit entries), the audio chunks and the video data, into a Demuxer. open()
-- returns a VideoDecoder: rgba8 frames from the Smacker video codec (codecs/video/Legacy/Smacker, through the
-- codecs/video/Video registry: the frame's palette rides on each packet as packet.palette), and the first audio
-- track as an audio Decoder built here (Smacker's packed DPCM audio has no audio format, so the format decodes it
-- itself: video/smk/Audio over codecs/audio/Stream; raw u8 / s16le tracks too). The ring frame (the extra frame that
-- leads back to the first) is not shown unless opts.ringFrame. Smk.demux(r) gives the Demuxer; Smk.openAudio(dmx,
-- track) the Decoder of any of its audio tracks.
-- Not supported: writing, Bink audio tracks (flags 0x08 / 0x04: reported as "binkaudio", no decoder).
-- ------------------------------------------------------------------------------------------------ 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/Video")
local Stream = require("@formats/codecs/audio/Stream")
local Audio = require("@formats/video/smk/Audio")
-- ------------------------------------------------------------------------------------------------ contract
local Smk = {
name = "smk",
category = "video",
extensions = { "smk" },
mime = { "video/vnd.radgamettools.smacker" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Demux", "codecs/video/Video", "codecs/audio/Stream", "codecs/video/Legacy/Smacker" },
caps = { decode = true, encode = false, streaming = true, video = { "smackvid" }, audio = { "smackaudio", "pcm_u8", "pcm_s16le" } },
}
-- ------------------------------------------------------------------------------------------------ types
export type OpenOptions = {
rgba: boolean?, -- (frames are rgba8 already)
maxPixels: number?, -- default 2^26
audio: boolean?, -- false: do not open the audio track
ringFrame: boolean?, -- true: also show the ring frame at the end (as ffmpeg does)
openAudio: any?, -- the api's hook: used only for tracks this format cannot decode (Bink audio)
}
-- ------------------------------------------------------------------------------------------------ constants
local HEADER = 104
local RING = 1
-- 6-bit palette components -> 8 bits (bit replication: v << 2 | v >> 4)
local PAL6 = table.create(64, 0)
for i = 0, 63 do
PAL6[i + 1] = i * 4 + i // 16
end
-- ------------------------------------------------------------------------------------------------ sniff
function Smk.sniff(r: Bytes.Reader): number
local s = r:peek(4)
if s ~= "SMK2" and s ~= "SMK4" then
return 0
end
return 1
end
-- ------------------------------------------------------------------------------------------------ decode
-- a frame's palette update over the previous palette (pal: 256 u32 R, G, B, A); false when malformed
local function palette(b: buffer, p: number, stop: number, old: buffer): (buffer?)
local pal = buffer.create(1024)
buffer.copy(pal, 0, old, 0, 1024)
local function u8(): number
local v = if p < stop then buffer.readu8(b, p) else 0
p += 1
return v
end
local sz = 0
while sz < 256 do
local t = u8()
if t >= 0x80 then
sz += t - 0x80 + 1
elseif t >= 0x40 then
local off = u8()
local j = t - 0x40 + 1
if off + j > 256 then
return nil
end
while j > 0 and sz < 256 do
buffer.writeu32(pal, sz * 4, buffer.readu32(old, off * 4))
sz += 1
off += 1
j -= 1
end
else
local r = PAL6[t + 1]
local g = PAL6[u8() % 64 + 1]
local bl = PAL6[u8() % 64 + 1]
buffer.writeu32(pal, sz * 4, r + g * 256 + bl * 65536 + 0xFF000000)
sz += 1
end
end
return pal
end
-- demux(r, opts?) -> the Demuxer | nil, Err
function Smk.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
local b, base, limit = r.buf, r.base, r.limit
if limit - base < HEADER then
return nil, Err.new(Err.TRUNCATED, "file shorter than the header")
end
local hr = Bytes.reader(b, base, HEADER)
local magic = hr:string(4)
local w, h, frames = hr:u32le(), hr:u32le(), hr:u32le()
local ptsInc = hr:i32le()
local flags = hr:u32le()
hr:skip(28)
local treeSize = hr:u32le()
local sizes = { hr:u32le(), hr:u32le(), hr:u32le(), hr:u32le() }
local rates, aflags = {}, {}
for k = 1, 7 do
rates[k] = hr:u24le()
aflags[k] = hr:u8()
end
if hr.err then
return nil, hr.err
end
if magic ~= "SMK2" and magic ~= "SMK4" then
return nil, Err.new(Err.CORRUPT, "not a Smacker file")
end
if w == 0 or h == 0 or w > 32768 or h > 32768 then
return nil, Err.new(Err.CORRUPT, "bad picture size")
end
local max = (opts and opts.maxPixels) or 2 ^ 26
if w * h > max then
return nil, Err.new(Err.LIMIT, w .. " x " .. h .. " over maxPixels")
end
local shown = frames
if bit32.band(flags, RING) ~= 0 then
frames += 1
end
local p = base + HEADER
if frames > 0xFFFFFF or p + frames * 5 + treeSize > limit then
return nil, Err.new(Err.TRUNCATED, "frame tables or trees past the end of the file")
end
-- the trees (with the four sizes in front, as the video codec takes them)
local config = buffer.create(16 + treeSize)
for k = 1, 4 do
buffer.writeu32(config, (k - 1) * 4, sizes[k])
end
buffer.copy(config, 16, b, p + frames * 5, treeSize)
-- frame time: > 0 ms, < 0 units of 10 us (ticks of 1/100000 s)
local tick = if ptsInc < 0 then -ptsInc elseif ptsInc > 0 then ptsInc * 100 else 10000
local tracks: { Types.Track } = {
{
id = 1,
kind = "video",
codec = "smackvid",
codecTag = magic,
timescale = 100000,
width = w,
height = h,
frameRate = 100000 / tick,
default = true,
config = config,
flags = flags,
} :: Types.Track,
}
local audio: { [number]: Types.Track } = {}
for k = 1, 7 do
if rates[k] > 0 then
local f = aflags[k]
local bits = if bit32.band(f, 0x20) ~= 0 then 16 else 8
local codec = if bit32.band(f, 0x0C) ~= 0 then "binkaudio"
elseif bit32.band(f, 0x80) ~= 0 then "smackaudio"
elseif bits == 16 then "pcm_s16le"
else "pcm_u8"
local t: Types.Track = {
id = k + 1,
kind = "audio",
codec = codec,
codecTag = string.format("0x%02X", f),
timescale = rates[k],
sampleRate = rates[k],
channels = if bit32.band(f, 0x10) ~= 0 then 2 else 1,
bitsPerSample = bits,
default = #tracks == 1,
}
audio[k] = t
table.insert(tracks, t)
end
end
local dmx = Demux.new("smk", tracks, b, { version = magic, flags = flags, frames = shown, ringFrame = frames > shown })
local count = if opts and opts.ringFrame then frames else shown
local pal = buffer.create(1024)
local apts = { 0, 0, 0, 0, 0, 0, 0 }
local at = p + frames * 5 + treeSize
for i = 0, frames - 1 do
local fsize = buffer.readu32(b, p + i * 4)
local fflags = buffer.readu8(b, p + frames * 4 + i)
local size = fsize - fsize % 4
local q, stop = at, math.min(limit, at + size)
at += size
local ok = true
if fflags % 2 == 1 and q < stop then
local psize = buffer.readu8(b, q) * 4
if psize > size then
ok = false
else
local np = palette(b, q + 1, math.min(stop, q + psize), pal)
if np then
pal = np
else
ok = false
end
q += psize
end
end
for k = 1, 7 do
if not ok then
break
end
if bit32.band(fflags, 2 ^ k) ~= 0 then
if q + 4 > stop then
ok = false
break
end
local asize = buffer.readu32(b, q)
local t = audio[k]
local min = if t and t.codec ~= "pcm_s16le" then 8 else 4
if asize < min or q + asize > stop then
ok = false
break
end
if t then
local len = asize - 4
local frameBytes = (t.channels :: number) * (t.bitsPerSample :: number) // 8
local n = if t.codec == "smackaudio" then buffer.readu32(b, q + 4) // frameBytes
elseif t.codec == "binkaudio" then 0
else len // frameBytes
dmx:add(t.id, apts[k], nil, n, true, q + 4, len)
apts[k] += n
end
q += asize
end
end
if ok and i < count then
dmx:add(1, i * tick, nil, tick, i == 0 or fsize % 2 == 1, q, math.max(0, stop - q))
dmx:setExtra("palette", pal)
end
end
dmx:finish()
return dmx, nil
end
-- ------------------------------------------------------------------------------------------------ open
-- openAudio(dmx, track) -> an audio Decoder for one of the demuxer's audio tracks | nil, Err
function Smk.openAudio(dmx: any, track: Types.Track): (Types.Decoder?, Err.Err?)
local codec = track.codec
if codec ~= "smackaudio" and codec ~= "pcm_u8" and codec ~= "pcm_s16le" then
return nil, Err.new(Err.UNSUPPORTED, "audio codec " .. codec)
end
local id = track.id
local count = dmx:count(id)
local starts: { number } = {}
local total = 0
for k = 1, count do
local pts, _, dur = dmx:info(id, k)
starts[k] = pts or total
total = (pts or total) + (dur or 0)
end
starts[count + 1] = total
local rate, ch, bits = track.sampleRate :: number, track.channels :: number, track.bitsPerSample :: number
local packed = codec == "smackaudio"
local floats: { number } = {}
local dec: any = {
sampleRate = rate,
channels = ch,
frames = total,
duration = total / rate,
bitrate = rate * ch * bits,
tags = {},
}
return Stream.packets(dec, {
count = count,
start = function(i: number): number
return starts[math.clamp(i, 1, count + 1)]
end,
decode = function(i: number): (number?, Err.Err?)
local pkt = dmx:get(id, i)
if not pkt then
return nil, Err.new(Err.TRUNCATED, "no audio packet " .. i)
end
local n, out = Audio.decode(pkt.data, ch, bits, packed)
if not n then
return nil, Err.from(out :: string)
end
floats = out :: { number }
return n, nil
end,
pcm = function(): { number }
return floats
end,
reset = function() end,
cumulative = true,
})
end
-- open(r, opts?) -> a VideoDecoder | nil, Err
function Smk.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
local dmx, err = Smk.demux(r, opts)
if not dmx then
return nil, err
end
local o: any = table.clone(opts or {})
type Hook = (any, Types.Track) -> (Types.Decoder?, Err.Err?)
local hook = o.openAudio :: Hook?
o.openAudio = function(d: any, track: Types.Track): (Types.Decoder?, Err.Err?)
if track.codec == "binkaudio" and hook then
return hook(d, track)
end
return Smk.openAudio(d, track)
end
return Video.open(dmx, o)
end
return Smk