C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.
215 lines
8.0 KiB
Luau
215 lines
8.0 KiB
Luau
--!strict
|
|
-- playlist/xspf: XSPF 1 ("spiff", https://xspf.org/spec) playlists on codecs/text/Xml. New in formats-luau (playlist-luau
|
|
-- had no XSPF). Decode: the playlist's title / creator / annotation / info / location / identifier / image / date,
|
|
-- every track's location (the first one is the entry url, all of them in entry.locations), title, creator, album,
|
|
-- annotation, info, image, trackNum and duration (milliseconds -> seconds), xml:base on the playlist and on tracks;
|
|
-- locations resolve against xml:base, then opts.baseUrl (RFC 3986, codecs/text/Url), and are classified like the other
|
|
-- playlist formats (local / relative / non-HTTP locations are unplayable). Encode: XSPF version 1 with those fields.
|
|
-- Not supported: extension / link / meta elements (skipped), XSPF version 0 differences.
|
|
--
|
|
-- local pl = Xspf.decode(Bytes.reader(buf), { baseUrl = "https://example.com/list.xspf" })
|
|
-- for _, e in pl.entries do print(e.url, e.title, e.creator, e.duration) end
|
|
-- local out = Xspf.encode(pl)
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local Url = require("@formats/codecs/text/Url")
|
|
local Xml = require("@formats/codecs/text/Xml")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Xspf = {
|
|
name = "xspf",
|
|
category = "playlist",
|
|
extensions = { "xspf" },
|
|
mime = { "application/xspf+xml" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Url", "codecs/text/Xml" },
|
|
caps = { decode = true, encode = true },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type DecodeOptions = {
|
|
baseUrl: string?, -- the playlist's own URL (or Windows path), for relative locations
|
|
maxBytes: number?, -- refuse bigger files (default 64 MB)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
local NS = "http://xspf.org/ns/0/"
|
|
local PLAYLIST_FIELDS = { "title", "creator", "annotation", "info", "location", "identifier", "image", "date", "license" }
|
|
local TRACK_FIELDS = { "title", "creator", "album", "annotation", "info", "image" }
|
|
|
|
local function trim(s: string): string
|
|
return (string.match(s, "^%s*(.-)%s*$") :: string)
|
|
end
|
|
|
|
-- the text of the first child element with this local name, trimmed (nil when missing or empty)
|
|
local function field(node: any, name: string): string?
|
|
for _, c in node.children do
|
|
if type(c) == "table" and Xml.localName(c.name) == name then
|
|
local s = trim(Xml.text(c))
|
|
return if s ~= "" then s else nil
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function elements(node: any, name: string): { any }
|
|
local out = {}
|
|
for _, c in node.children do
|
|
if type(c) == "table" and Xml.localName(c.name) == name then
|
|
table.insert(out, c)
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Xspf.sniff(r: Bytes.Reader): number
|
|
local s = r:peek(1024)
|
|
local i = string.find(s, "<%w*:?playlist[%s>]")
|
|
if i and string.find(s, NS, 1, true) then
|
|
return 0.95
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- decode(r, opts?) -> Types.Playlist | nil, Err (malformed XML or a root that is not <playlist> is an error; odd
|
|
-- tracks become warnings)
|
|
function Xspf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?)
|
|
local n = r.limit - r.pos
|
|
if n > (opts and opts.maxBytes or 2 ^ 26) then
|
|
return nil, Err.new(Err.LIMIT, "playlist over the size limit")
|
|
end
|
|
local doc, err = Xml.decode(r:string(n))
|
|
if not doc then
|
|
return nil, err
|
|
end
|
|
local root = (doc :: any).root
|
|
if not root or Xml.localName(root.name) ~= "playlist" then
|
|
return nil, Err.new(Err.CORRUPT, "the root element is not <playlist>")
|
|
end
|
|
local warnings: { string } = {}
|
|
local base, baseScheme = Url.base(opts and opts.baseUrl, warnings)
|
|
local function withXmlBase(node: any, b: string?, s: string?): (string?, string?)
|
|
local xb = node.attrs and node.attrs["xml:base"]
|
|
if type(xb) ~= "string" or xb == "" then
|
|
return b, s
|
|
end
|
|
local l = Url.locate(xb, b, s)
|
|
if Url.scheme(l.url) then
|
|
return l.url, Url.scheme(l.url)
|
|
end
|
|
return b, s
|
|
end
|
|
local pbase, pscheme = withXmlBase(root, base, baseScheme)
|
|
local pl: any = {
|
|
kind = "playlist",
|
|
format = "xspf",
|
|
entries = {},
|
|
warnings = warnings,
|
|
title = field(root, "title"),
|
|
meta = { source = { format = "xspf", version = root.attrs and root.attrs.version } },
|
|
}
|
|
for _, k in PLAYLIST_FIELDS do
|
|
if k ~= "title" then
|
|
pl.meta[k] = field(root, k)
|
|
end
|
|
end
|
|
local nLocal = 0
|
|
for _, list in elements(root, "trackList") do
|
|
for i, track in elements(list, "track") do
|
|
local tbase, tscheme = withXmlBase(track, pbase, pscheme)
|
|
local locs: { string } = {}
|
|
for _, l in elements(track, "location") do
|
|
local s = trim(Xml.text(l))
|
|
if s ~= "" then
|
|
table.insert(locs, s)
|
|
end
|
|
end
|
|
if #locs == 0 then
|
|
table.insert(warnings, string.format("track %d: no location", i))
|
|
else
|
|
local e: any = Url.locate(locs[1], tbase, tscheme)
|
|
for _, k in TRACK_FIELDS do
|
|
e[k] = field(track, k)
|
|
end
|
|
local d = tonumber(field(track, "duration"))
|
|
e.duration = if d and d >= 0 and d == d and d ~= math.huge then d / 1000 else nil
|
|
e.trackNum = tonumber(field(track, "trackNum"))
|
|
if #locs > 1 then
|
|
local all = {}
|
|
for _, l in locs do
|
|
table.insert(all, Url.locate(l, tbase, tscheme).url)
|
|
end
|
|
e.locations = all
|
|
end
|
|
if e.reason == "local path" then
|
|
nLocal += 1
|
|
elseif e.reason then
|
|
table.insert(warnings, string.format("track %d: %s: %s", i, e.reason, e.url))
|
|
end
|
|
table.insert(pl.entries, e)
|
|
end
|
|
end
|
|
end
|
|
if nLocal > 0 then
|
|
table.insert(warnings, string.format("%d local path%s (not playable)", nLocal, if nLocal == 1 then "" else "s"))
|
|
end
|
|
return pl, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
-- encode(playlist, opts?) -> buffer | nil, Err: XSPF version 1. playlist: a Types.Playlist (any format's) or a list
|
|
-- of entries ({ url, title?, duration?, creator?, album?, ... }). opts.indent (default 2) pretty-prints.
|
|
function Xspf.encode(value: any, opts: { indent: number?, title: string? }?): (buffer?, Err.Err?)
|
|
if type(value) ~= "table" then
|
|
return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries")
|
|
end
|
|
local entries = if type(value.entries) == "table" then value.entries else value
|
|
local root = Xml.element("playlist", { version = "1", xmlns = NS })
|
|
local function add(parent: any, name: string, v: any)
|
|
if type(v) == "string" and v ~= "" then
|
|
Xml.append(parent, Xml.element(name, nil, { v }))
|
|
elseif type(v) == "number" and v == v and v ~= math.huge and v ~= -math.huge then
|
|
Xml.append(parent, Xml.element(name, nil, { tostring(math.floor(v + 0.5)) }))
|
|
end
|
|
end
|
|
local title = if opts and opts.title then opts.title else value.title
|
|
add(root, "title", title)
|
|
local meta = if type(value.meta) == "table" then value.meta else {}
|
|
for _, k in PLAYLIST_FIELDS do
|
|
if k ~= "title" then
|
|
add(root, k, meta[k])
|
|
end
|
|
end
|
|
local list = Xml.append(root, Xml.element("trackList"))
|
|
for _, e in entries do
|
|
if type(e) == "table" and type(e.url) == "string" then
|
|
local tr = Xml.append(list, Xml.element("track"))
|
|
add(tr, "location", e.url)
|
|
if type(e.locations) == "table" then
|
|
for i = 2, #e.locations do
|
|
add(tr, "location", e.locations[i])
|
|
end
|
|
end
|
|
for _, k in TRACK_FIELDS do
|
|
add(tr, k, e[k])
|
|
end
|
|
local d = tonumber(e.duration)
|
|
if d and d >= 0 then
|
|
add(tr, "duration", d * 1000)
|
|
end
|
|
add(tr, "trackNum", tonumber(e.trackNum))
|
|
end
|
|
end
|
|
local s = Xml.encode(Xml.document(root), { indent = if opts and opts.indent ~= nil then opts.indent else 2 })
|
|
if type(s) ~= "string" then
|
|
return nil, Err.new(Err.ARG, "encode: the XML serializer failed")
|
|
end
|
|
return buffer.fromstring(s), nil
|
|
end
|
|
|
|
return Xspf
|