ws-cleanup: consistency (lower-case tags, WAVE channel order, loops = play count, Err messages, caps)

- Audio tags keys are lower case everywhere (Types.Tags): FLAC no longer returns TITLE / ARTIST (the raw fields stay
  in meta.source.comments; also for Vorbis and Opus); Vorbis / Opus / FLAC read and write tags through
  codecs/VorbisComment (TRACKNUMBER -> track, DISCNUMBER -> disc, DESCRIPTION -> comment); MP3 albumArtist ->
  albumartist and year -> date (ID3 TYER / TDRC, ID3v1); RIFF INFO / CAF / MP4 freeform names are lower-cased (MP4
  writes iTunSMPB / iTunNORM back in their usual spelling; m4a reads tags.itunsmpb).
- Channel order: Vorbis (and Opus mapping family 1) decode to WAVE order like FLAC / WAV / AAC (both used to return
  the Vorbis order); the Vorbis encoder takes WAVE order and writes Vorbis order. Pcm.WAVE_FROM_VORBIS / Pcm.reorder;
  Vorbis.packetDecoder gives pd.order. Tests reorder oggdec's / opusdec's raw (Vorbis-order) references.
- img.loops is the play count in GIF, APNG and WebP (they already agreed; Types / WebP docs said "loop count");
  tests/api round-trips loops 0 / 1 / 3 through all three.
- Err: messages without the format name (png / midi encoders said "png encode: ..."); codecs/Bzip2 and codecs/Zstd
  check arguments through Bytes.span.
- caps: mp3 encodeLayers = { 2, 3 }, opus encodeChannels = 2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 11:01:22 -05:00
1 parent 8158acf3d5
commit fb2dd98db2
27 files changed
+164 -170

No files matched your search

+11 -11
View File
@@ -200,7 +200,7 @@ local list: { Entry } = {
path = "audio/flac",
extensions = { "flac", "oga" },
mime = { "audio/flac", "audio/x-flac" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "core/Md5", "codecs/Pcm" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "core/Md5", "codecs/Pcm", "codecs/VorbisComment" },
caps = { decode = true, encode = true, streaming = true, depths = { 4, 8, 12, 16, 20, 24, 32 }, ogg = true, seek = true },
load = function()
return require("@formats/audio/flac")
@@ -224,8 +224,8 @@ local list: { Entry } = {
path = "audio/mp3",
extensions = { "mp3", "mp2", "mp1", "mpga" },
mime = { "audio/mpeg", "audio/mp3", "audio/mpa" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm" },
caps = { decode = true, encode = true, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm", "codecs/Resample" },
caps = { decode = true, encode = true, streaming = true, seek = true, layers = { 1, 2, 3 }, encodeLayers = { 2, 3 }, channels = 2 },
load = function()
return require("@formats/audio/mp3")
end,
@@ -236,8 +236,8 @@ local list: { Entry } = {
path = "audio/opus",
extensions = { "opus" },
mime = { "audio/opus", "audio/ogg" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct" },
caps = { decode = true, encode = true, streaming = true, seek = true, lossy = true, channels = 255, rates = { 48000 }, encodeModes = { "celt" } },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct", "codecs/RangeCoder/Decoder", "codecs/RangeCoder/Encoder", "codecs/Pcm", "codecs/Resample", "codecs/VorbisComment" },
caps = { decode = true, encode = true, streaming = true, seek = true, lossy = true, channels = 255, rates = { 48000 }, encodeModes = { "celt" }, encodeChannels = 2 },
load = function()
return require("@formats/audio/opus")
end,
@@ -260,7 +260,7 @@ local list: { Entry } = {
path = "audio/vorbis",
extensions = { "ogg", "oga" },
mime = { "audio/ogg", "audio/vorbis" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct", "codecs/Pcm" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct", "codecs/Pcm", "codecs/VorbisComment" },
caps = { decode = true, encode = true, streaming = true, seek = true, lossy = true, channels = 255 },
load = function()
return require("@formats/audio/vorbis")
@@ -368,7 +368,7 @@ local list: { Entry } = {
path = "model/dae",
extensions = { "dae", "zae" },
mime = { "model/vnd.collada+xml", "model/vnd.collada+zip" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "codecs/Base64", "containers/Zip" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "core/Base64", "containers/Zip" },
caps = { decode = true, encode = true, materials = true, polygons = true, skins = true, morphTargets = true, animation = true, cameras = true, lights = true, zae = true },
load = function()
return require("@formats/model/dae")
@@ -392,7 +392,7 @@ local list: { Entry } = {
path = "model/gltf",
extensions = { "gltf", "glb" },
mime = { "model/gltf+json", "model/gltf-binary" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "codecs/Base64" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "core/Base64" },
caps = { decode = true, encode = true, binary = true, materials = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true },
load = function()
return require("@formats/model/gltf")
@@ -692,7 +692,7 @@ local list: { Entry } = {
path = "data/plist",
extensions = { "plist" },
mime = { "application/x-plist", "application/x-bplist" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "codecs/Json" },
deps = { "core/Err", "core/Bytes", "Types", "core/Base64", "codecs/Xml", "codecs/Json" },
caps = { decode = true, encode = true, binary = "read", ordered = true },
load = function()
return require("@formats/data/plist")
@@ -704,7 +704,7 @@ local list: { Entry } = {
path = "data/toml",
extensions = { "toml" },
mime = { "application/toml" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "codecs/Text" },
caps = { decode = true, encode = true, ordered = true, dates = true },
load = function()
return require("@formats/data/toml")
@@ -728,7 +728,7 @@ local list: { Entry } = {
path = "data/yaml",
extensions = { "yaml", "yml" },
mime = { "application/yaml", "application/x-yaml", "text/yaml" },
deps = { "core/Err", "core/Bytes", "Types" },
deps = { "core/Err", "core/Bytes", "Types", "core/Base64", "codecs/Text" },
caps = { decode = true, encode = true, ordered = true, multiDocument = true, anchors = true },
load = function()
return require("@formats/data/yaml")
+2 -1
View File
@@ -59,7 +59,8 @@ export type Image = {
format: PixelFormat,
pixels: buffer,
frames: { Frame }?, -- animations: frame 1 is also width x height pixels (the composited first frame)
loops: number, -- 0 = forever
loops: number, -- play count: how many times the animation plays, 0 = forever (GIF stores repeats: plays - 1; APNG
-- num_plays and the WebP ANIM loop count are play counts)
meta: ImageMeta,
}
+1 -1
View File
@@ -222,7 +222,7 @@ local function parse(r: Bytes.Reader, o: DecodeOptions): (Parsed?, Err.Err?)
if c.err then
break
end
p.tags[INFO_TAGS[string.lower(k)] or k] = v
p.tags[INFO_TAGS[string.lower(k)] or string.lower(k)] = v
end
end
r.pos = stop
+6 -40
View File
@@ -11,6 +11,8 @@
-- SEEKTABLE. Streams: input blocks are pulled from a codecs/Pcm Source, so a Decoder is encoded without decoding it
-- whole. Never throws: bad options are rejected by audio/flac before calling in.
local Md5 = require("@formats/core/Md5")
local VorbisComment = require("@formats/codecs/VorbisComment")
local Frame = require("@formats/audio/flac/Frame")
local Encode = {}
@@ -23,33 +25,12 @@ for i = 0, 63 do
end
-- ------------------------------------------------------------------------------------------------ CRCs
local CRC8 = table.create(256, 0)
local CRC16 = table.create(256, 0)
for i = 0, 255 do
local c = i
for _ = 1, 8 do
c = if c >= 128 then bit32.bxor((c * 2) % 256, 0x07) else c * 2
end
CRC8[i + 1] = c
local d = i * 256
for _ = 1, 8 do
d = if d >= 32768 then bit32.bxor((d * 2) % 65536, 0x8005) else d * 2
end
CRC16[i + 1] = d
end
-- (the decoder's tables: audio/flac/Frame; (b, offset, count) here)
local function crc8(b: buffer, o: number, n: number): number
local c = 0
for i = o, o + n - 1 do
c = CRC8[bit32.bxor(c, buffer.readu8(b, i)) + 1]
end
return c
return Frame.crc8(b, o, o + n)
end
local function crc16(b: buffer, o: number, n: number): number
local c = 0
for i = o, o + n - 1 do
c = bit32.bxor((c * 256) % 65536, CRC16[bit32.bxor(c // 256, buffer.readu8(b, i)) + 1])
end
return c
return Frame.crc16(b, o, o + n)
end
Encode.crc8, Encode.crc16 = crc8, crc16
@@ -702,8 +683,6 @@ export type Options = {
vendor: string,
}
local TAG_NAMES = { track = "TRACKNUMBER" }
-- encode(src, opts) -> buffer | nil, message
function Encode.encode(src: Source, opts: Options): (buffer?, string?)
local cfg = PRESETS[opts.level + 1]
@@ -730,20 +709,7 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
end
end
-- vorbis comment
local comments = {}
local keys = {}
for k in src.tags do
if type(k) == "string" then
table.insert(keys, k)
end
end
table.sort(keys)
for _, k in keys do
local v = src.tags[k]
if type(v) == "string" then
table.insert(comments, (TAG_NAMES[k] or string.upper(k)) .. "=" .. v)
end
end
local comments = VorbisComment.fields(src.tags)
local vendor = opts.vendor
local vcLen = 4 + #vendor + 4
for _, c in comments do
+5 -2
View File
@@ -4,6 +4,8 @@
-- audio/flac/Meta: FLAC metadata blocks: STREAMINFO, SEEKTABLE, VORBIS_COMMENT and PICTURE (its fields are listed, the image is
-- left in place); every other block is skipped. Nothing throws: bad data gives nil, err.
-- Absorbed from flac-luau 7083d0d (same author, no third-party code).
local VorbisComment = require("@formats/codecs/VorbisComment")
local readu8, readu32 = buffer.readu8, buffer.readu32
local Meta = {}
@@ -102,8 +104,9 @@ local function vorbisComment(b: buffer, p: number, e: number, info: Info): boole
p += l
local eq = string.find(s, "=", 1, true)
if eq then
local key, value = string.upper(string.sub(s, 1, eq - 1)), string.sub(s, eq + 1)
table.insert(info.comments, { key = key, value = value })
local name, value = string.sub(s, 1, eq - 1), string.sub(s, eq + 1)
table.insert(info.comments, { key = string.upper(name), value = value })
local key = VorbisComment.tagKey(name)
local old = info.tags[key]
info.tags[key] = if old then old .. "; " .. value else value
end
+2 -2
View File
@@ -29,7 +29,7 @@ local Flac = {
category = "audio",
extensions = { "flac", "oga" },
mime = { "audio/flac", "audio/x-flac" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "core/Md5", "codecs/Pcm" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "core/Md5", "codecs/Pcm", "codecs/VorbisComment" },
caps = { decode = true, encode = true, streaming = true, depths = { 4, 8, 12, 16, 20, 24, 32 }, ogg = true, seek = true },
}
@@ -115,7 +115,7 @@ function Flac.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.Er
o += n
end
a.tags = d.tags
a.meta.source = { format = "flac", bitDepth = d.bitsPerSample, container = d.container, vendor = d.vendor, md5 = d.md5, pictures = d.pictures }
a.meta.source = { format = "flac", bitDepth = d.bitsPerSample, container = d.container, vendor = d.vendor, md5 = d.md5, pictures = d.pictures, comments = d.comments }
return a, nil
end
+1 -1
View File
@@ -167,7 +167,7 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
end
local tags, pictures = IsoBmff.tags(r, movie)
-- iTunes gapless info (fdkaac, iTunes) when there is no edit list: " 0 <priming> <padding> <samples> ..." (hex)
local smpb = tags.iTunSMPB
local smpb = tags.itunsmpb
if gapless and not t.edits and smpb then
local f = {}
for w in string.gmatch(smpb, "%x+") do
+4 -4
View File
@@ -35,8 +35,8 @@ local FIELDS = {
TIT2 = "title", TT2 = "title",
TPE1 = "artist", TP1 = "artist",
TALB = "album", TAL = "album",
TPE2 = "albumArtist", TP2 = "albumArtist",
TYER = "year", TYE = "year", TDRC = "year",
TPE2 = "albumartist", TP2 = "albumartist",
TYER = "date", TYE = "date", TDRC = "date",
TRCK = "track", TRK = "track",
TPOS = "disc", TPA = "disc",
TCON = "genre", TCO = "genre",
@@ -257,7 +257,7 @@ local function v1String(b: buffer, o: number, n: number): string
end
-- Parses tags and returns (tags, audio start, audio end) for the buffer b.
-- tags = { title?, artist?, album?, albumArtist?, year?, track?, disc?, genre?, composer?, comment?,
-- tags = { title?, artist?, album?, albumartist?, date?, track?, disc?, genre?, composer?, comment?,
-- raw = { [frame id] = text }, id3v2 = "2.3"?, id3v1 = true? }
function Tags.parse(b: buffer): (any, number, number)
local len = buffer.len(b)
@@ -277,7 +277,7 @@ function Tags.parse(b: buffer): (any, number, number)
title = v1String(b, o + 3, 30),
artist = v1String(b, o + 33, 30),
album = v1String(b, o + 63, 30),
year = v1String(b, o + 93, 4),
date = v1String(b, o + 93, 4),
}
if ru8(b, o + 125) == 0 and ru8(b, o + 126) ~= 0 then
v1.comment = v1String(b, o + 97, 28)
+1 -1
View File
@@ -33,7 +33,7 @@ local Mp3 = {
extensions = { "mp3", "mp2", "mp1", "mpga" },
mime = { "audio/mpeg", "audio/mp3", "audio/mpa" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm", "codecs/Resample" },
caps = { decode = true, encode = true, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
caps = { decode = true, encode = true, streaming = true, seek = true, layers = { 1, 2, 3 }, encodeLayers = { 2, 3 }, channels = 2 },
}
-- ------------------------------------------------------------------------------------------------ types
+1 -1
View File
@@ -38,7 +38,7 @@ local Opus = {
extensions = { "opus" },
mime = { "audio/opus", "audio/ogg" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct", "codecs/RangeCoder/Decoder", "codecs/RangeCoder/Encoder", "codecs/Pcm", "codecs/Resample", "codecs/VorbisComment" },
caps = { decode = true, encode = true, streaming = true, seek = true, lossy = true, channels = 255, rates = { 48000 }, encodeModes = { "celt" } },
caps = { decode = true, encode = true, streaming = true, seek = true, lossy = true, channels = 255, rates = { 48000 }, encodeModes = { "celt" }, encodeChannels = 2 },
}
-- ------------------------------------------------------------------------------------------------ types
+14 -12
View File
@@ -11,6 +11,8 @@
-- models computed at load time (codewords assigned exactly as the spec's decoder does). Granules are sample-exact
-- (frames centred on multiples of 1024 from sample 0; the last page's granule trims the end). Never throws.
local Mdct = require("@formats/codecs/Mdct")
local Pcm = require("@formats/codecs/Pcm")
local VorbisComment = require("@formats/codecs/VorbisComment")
local Ogg = require("@formats/containers/Ogg")
local Tables = require("@formats/audio/vorbis/Tables")
@@ -448,17 +450,9 @@ local function commentHeader(vendor: string, tags: { [string]: string }): buffer
putString(w, "vorbis")
put(w, #vendor, 32)
putString(w, vendor)
local keys = {}
for k, v in tags do
if type(k) == "string" and type(v) == "string" then
table.insert(keys, k)
end
end
table.sort(keys)
put(w, #keys, 32)
for _, k in keys do
local name = if k == "track" then "TRACKNUMBER" else string.upper(k)
local s = name .. "=" .. tags[k]
local fields = VorbisComment.fields(tags)
put(w, #fields, 32)
for _, s in fields do
put(w, #s, 32)
putString(w, s)
end
@@ -614,6 +608,14 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
for c = 1, ch do
frame[c] = table.create(N, 0)
end
-- the input is in WAVE channel order; input channel c goes to Vorbis channel order[c]
local order = Pcm.WAVE_FROM_VORBIS[ch] or {}
if #order ~= ch then
order = table.create(ch, 0)
for c = 1, ch do
order[c] = c
end
end
local blk = table.create(N, 0)
local X = table.create(ch)
local R = table.create(ch)
@@ -658,7 +660,7 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
for i = 0, take - 1 do
local base = (pendOff + i) * ch * 4
for c = 1, ch do
frame[c][N2 + got + i + 1] = buffer.readf32(pb, base + (c - 1) * 4)
frame[order[c]][N2 + got + i + 1] = buffer.readf32(pb, base + (c - 1) * 4)
end
end
got += take
+28 -24
View File
@@ -19,6 +19,7 @@ local Setup = require("@formats/audio/vorbis/Setup")
local Packet = require("@formats/audio/vorbis/Packet")
local Encode = require("@formats/audio/vorbis/Encode")
local Pcm = require("@formats/codecs/Pcm")
local VorbisComment = require("@formats/codecs/VorbisComment")
-- ------------------------------------------------------------------------------------------------ contract
local Vorbis = {
@@ -26,7 +27,7 @@ local Vorbis = {
category = "audio",
extensions = { "ogg", "oga" },
mime = { "audio/ogg", "audio/vorbis" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct", "codecs/Pcm" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct", "codecs/Pcm", "codecs/VorbisComment" },
caps = { decode = true, encode = true, streaming = true, seek = true, lossy = true, channels = 255 },
}
@@ -40,38 +41,36 @@ export type EncodeOptions = {
}
-- ------------------------------------------------------------------------------------------------ constants
local TAG_KEYS = { tracknumber = "track", description = "comment" }
local function tagsOf(comments: { string }): Types.Tags
local tags: Types.Tags = {}
for _, s in comments do
local eq = string.find(s, "=", 1, true)
if eq then
local k = string.lower(string.sub(s, 1, eq - 1))
local v = string.sub(s, eq + 1)
k = TAG_KEYS[k] or k
tags[k] = if tags[k] then tags[k] .. "; " .. v else v
end
-- waveOrder(channels) -> output channel c is Vorbis channel order[c] (codecs/Pcm; identity past 8 channels)
local function waveOrder(ch: number): { number }
local m = Pcm.WAVE_FROM_VORBIS[ch]
if m then
return m
end
return tags
local id = table.create(ch, 0)
for c = 1, ch do
id[c] = c
end
return id
end
-- the three headers -> ident, setup, vendor, tags | nil, Err
local function headers(h1: any, h2: any, h3: any): (any, any, string?, Types.Tags?, Err.Err?)
-- the three headers -> ident, setup, vendor, tags, raw comment fields | nil, Err
local function headers(h1: any, h2: any, h3: any): (any, any, string?, Types.Tags?, { VorbisComment.Field }?, Err.Err?)
local id, ierr = Setup.ident(h1.data, h1.offset, h1.size)
if not id then
return nil, nil, nil, nil, Err.new(Err.CORRUPT, ierr or "bad identification header")
return nil, nil, nil, nil, nil, Err.new(Err.CORRUPT, ierr or "bad identification header")
end
local vendor, comments = Setup.comment(h2.data, h2.offset, h2.size)
if not vendor then
return nil, nil, nil, nil, Err.new(Err.CORRUPT, "bad comment header")
return nil, nil, nil, nil, nil, Err.new(Err.CORRUPT, "bad comment header")
end
local setup, serr = Setup.setup(h3.data, h3.offset, h3.size, id)
if not setup then
local code = if serr == "floor 0" then Err.UNSUPPORTED else Err.CORRUPT
return nil, nil, nil, nil, Err.new(code, serr or "bad setup header")
return nil, nil, nil, nil, nil, Err.new(code, serr or "bad setup header")
end
return id, setup, vendor, tagsOf(comments or {}), nil
local tags, raw = VorbisComment.tags(comments or {})
return id, setup, vendor, tags, raw, nil
end
-- ------------------------------------------------------------------------------------------------ sniff
@@ -244,8 +243,9 @@ function Stream.read(self: any, n: number, out: buffer?): (buffer, number)
end
local arrays, first, last = q[1], q[2], q[3]
local take = math.min(n - got, last - first + 1)
local order = self.order
for c = 1, ch do
local a = arrays[c]
local a = arrays[order[c]]
local o = got * ch * 4 + (c - 1) * 4
for i = first, first + take - 1 do
writef32(b, o, a[i])
@@ -373,7 +373,7 @@ function Vorbis.open(r: any, opts: OpenOptions?): (Types.Decoder?, Err.Err?)
if h3 == nil then
return nil, Err.new(Err.TRUNCATED, "missing setup header")
end
local id, setup, vendor, tags, herr = headers(h1, h2copy, h3)
local id, setup, vendor, tags, raw, herr = headers(h1, h2copy, h3)
if not id then
return nil, herr
end
@@ -420,10 +420,13 @@ function Vorbis.open(r: any, opts: OpenOptions?): (Types.Decoder?, Err.Err?)
tags = tags,
vendor = vendor,
ident = id,
-- WAVE channel order out (codecs/Pcm): output channel c is Vorbis channel order[c]
order = waveOrder(id.channels),
source = {
format = "vorbis",
codec = "vorbis",
vendor = vendor,
comments = raw,
blocksizes = { id.blocksize0, id.blocksize1 },
nominalBitrate = id.bitrateNominal,
},
@@ -456,7 +459,7 @@ function Vorbis.packetDecoder(h1: buffer, h2: buffer, h3: buffer): (any, Err.Err
local function pk(b: buffer): any
return { data = b, offset = 0, size = buffer.len(b) }
end
local id, setup, vendor, tags, err = headers(pk(h1), pk(h2), pk(h3))
local id, setup, vendor, tags, _, err = headers(pk(h1), pk(h2), pk(h3))
if not id then
return nil, err
end
@@ -469,7 +472,8 @@ function Vorbis.packetDecoder(h1: buffer, h2: buffer, h3: buffer): (any, Err.Err
channels = id.channels,
vendor = vendor,
tags = tags,
out = st.out,
out = st.out, -- per Vorbis channel; WAVE-order channel c is out[order[c]]
order = waveOrder(id.channels),
decode = function(_self: any, b: buffer, offset: number, size: number): number
if offset < 0 or size < 0 or offset + size > buffer.len(b) then
return -1
+3 -2
View File
@@ -304,7 +304,8 @@ local function encodeParts(value: any, opts: EncodeOptions?, asParts: boolean):
table.sort(keys)
for _, k in keys do
local v = tags[k]
local id = TAG_IDS[k] or (if #k == 4 and string.upper(k) == k then k else nil)
-- other RIFF INFO ids come back as their lower-cased id ("ixyz") and are written as such
local id = TAG_IDS[k] or (if string.match(string.lower(k), "^i[%l%d][%l%d][%l%d]$") then string.upper(k) else nil)
if id and type(v) == "string" and v ~= "" then
table.insert(items, { id, v })
end
@@ -383,7 +384,7 @@ function Wav.open(r: Bytes.Reader, _opts: OpenOptions?): (Decoder?, Err.Err?)
if sc.avail > 0 then
local v = trimTag(buffer.readstring(buf, sc.offset, sc.avail))
if v ~= "" then
tags[name or sc.id] = v
tags[name or string.lower(sc.id)] = v
end
end
end
+4 -17
View File
@@ -10,6 +10,7 @@
--
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / CHECKSUM / LIMIT / ARG).
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Decode = require("@formats/codecs/Bzip2/Decode")
local Bzip2 = {}
@@ -28,24 +29,10 @@ local KINDS = {
-- decompress(buf, offset?, length?, opts?) -> buffer, consumedEnd | nil, Err
function Bzip2.decompress(buf: buffer | string, offset: number?, length: number?, opts: Options?): (buffer?, any)
local b: buffer
if type(buf) == "string" then
b = buffer.fromstring(buf)
elseif type(buf) == "buffer" then
b = buf
else
return nil, Err.new(Err.ARG, "Bzip2.decompress: expected a buffer or a string, got " .. typeof(buf))
local b, o, l, aerr = Bytes.span("Bzip2.decompress", buf, offset, length)
if not b then
return nil, aerr
end
local n = buffer.len(b)
local o = offset or 0
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 then
return nil, Err.new(Err.ARG, "Bzip2.decompress: bad offset " .. tostring(offset))
end
local l = length or (n - o)
if type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
return nil, Err.new(Err.ARG, "Bzip2.decompress: bad length " .. tostring(length))
end
l = math.min(l, n - o)
local maxBytes = if opts and opts.maxBytes ~= nil then opts.maxBytes else 2 ^ 30
if type(maxBytes) ~= "number" or maxBytes < 0 then
return nil, Err.new(Err.ARG, "Bzip2.decompress: bad maxBytes")
+4 -17
View File
@@ -11,6 +11,7 @@
--
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / CHECKSUM / UNSUPPORTED / LIMIT / ARG).
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Decode = require("@formats/codecs/Zstd/Decode")
local Zstd = {}
@@ -44,24 +45,10 @@ end
-- decompress(buf, offset?, length?, opts?) -> buffer, consumedEnd | nil, Err. Every frame in the range is decoded
-- and the outputs concatenated; data after the last frame that is not a frame is an error (as zstd -d does).
function Zstd.decompress(buf: buffer | string, offset: number?, length: number?, opts: Options?): (buffer?, any)
local b: buffer
if type(buf) == "string" then
b = buffer.fromstring(buf)
elseif type(buf) == "buffer" then
b = buf
else
return nil, Err.new(Err.ARG, "Zstd.decompress: expected a buffer or a string, got " .. typeof(buf))
local b, o, l, aerr = Bytes.span("Zstd.decompress", buf, offset, length)
if not b then
return nil, aerr
end
local n = buffer.len(b)
local o = offset or 0
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 then
return nil, Err.new(Err.ARG, "Zstd.decompress: bad offset " .. tostring(offset))
end
local l = length or (n - o)
if type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
return nil, Err.new(Err.ARG, "Zstd.decompress: bad length " .. tostring(length))
end
l = math.min(l, n - o)
local maxBytes = if opts and opts.maxBytes ~= nil then opts.maxBytes else 2 ^ 30
if type(maxBytes) ~= "number" or maxBytes < 0 then
return nil, Err.new(Err.ARG, "Zstd.decompress: bad maxBytes " .. tostring(maxBytes))
+4 -2
View File
@@ -312,6 +312,8 @@ for atom, name in TAG do
end
Meta.TAG = TAG
Meta.TAG_ATOM = TAG_ATOM
-- freeform (----) names: tags keys are lower case; these keep their usual spelling when written back
local FREEFORM: { [string]: string } = { itunsmpb = "iTunSMPB", itunnorm = "iTunNORM", itunes_cddb_ids = "iTunes_CDDB_IDs" }
-- the ID3v1 genres gnre indexes (1-based in gnre)
local GENRES = {
@@ -384,7 +386,7 @@ function Meta.tags(b: buffer, ilst: Box.Box?): ({ [string]: string }, { Picture
table.insert(pics, { mime = mime, data = data })
elseif kind == 1 or kind == 0 then
local s = readstring(b, p, n)
local key = freeName or TAG[typ] or typ
local key = if freeName then string.lower(freeName) else TAG[typ] or string.lower(typ)
if typ == "\169gen" or not tags[key] then
tags[key] = s
end
@@ -487,7 +489,7 @@ function Meta.writeIlst(w: Bytes.Writer, tags: { [string]: string }, pictures: {
end)
box(w, "name", function()
w:u32be(0)
w:string(k)
w:string(FREEFORM[k] or k)
end)
dataBox(w, 1, v)
end)
+5 -5
View File
@@ -413,7 +413,7 @@ local function animationFrames(img: Types.Image, opts: EncodeOptions?): ({ Types
return nil, nil
end
if frames ~= nil and type(frames) ~= "table" then
return nil, Err.new(Err.ARG, "png encode: frames must be a list")
return nil, Err.new(Err.ARG, "frames must be a list")
end
if frames == nil or #frames == 0 then
if mode then
@@ -445,12 +445,12 @@ end
function Png.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
if not validImage(img) then
return nil, Err.new(Err.ARG, "png encode: needs an rgba8 Image")
return nil, Err.new(Err.ARG, "needs an rgba8 Image")
end
local w, h = img.width, img.height
local level = opts and opts.level or 6
if type(level) ~= "number" or level < 0 or level > 9 then
return nil, Err.new(Err.ARG, "png encode: level must be 0..9")
return nil, Err.new(Err.ARG, "level must be 0..9")
end
local frames, ferr = animationFrames(img, opts)
if ferr then
@@ -458,7 +458,7 @@ function Png.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
end
local loops = img.loops or 0
if frames and (type(loops) ~= "number" or loops < 0 or loops > 4294967295 or loops % 1 ~= 0) then
return nil, Err.new(Err.ARG, "png encode: loops must be an integer 0..2^32-1")
return nil, Err.new(Err.ARG, "loops must be an integer 0..2^32-1")
end
local want = opts and opts.colorType or "auto"
local ctName: string = want
@@ -480,7 +480,7 @@ function Png.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
end
local ct = CT_CODES[ctName]
if ct == nil then
return nil, Err.new(Err.ARG, "png encode: unknown colorType " .. tostring(want))
return nil, Err.new(Err.ARG, "unknown colorType " .. tostring(want))
end
local function zdata(im: Types.Image): buffer
return Deflate.zlibCompress(Encode.filter(im.pixels, im.width, im.height, ct), { level = level }) :: buffer
+1 -1
View File
@@ -49,7 +49,7 @@ export type EncodeOptions = {
lossless: boolean?, -- must be true or nil (lossy encoding is not supported)
effort: number?, -- 0..9, default 5: VP8L encoder effort (more = smaller, slower)
exact: boolean?, -- keep the RGB of fully transparent pixels (default false: they may be changed)
loops: number?, -- animations: loop count (default img.loops; 0 = forever)
loops: number?, -- animations: play count (default img.loops; 0 = forever)
background: { number }?, -- animations: ANIM background colour { r, g, b, a } (default meta.background or 0)
}
+8 -8
View File
@@ -269,22 +269,22 @@ end
-- write(midi, opts?) -> buffer | nil, Err
function Smf.write(midi: Types.Midi, opts: WriteOptions?): (buffer?, Err.Err?)
if type(midi) ~= "table" or type(midi.tracks) ~= "table" then
return nil, Err.new(Err.ARG, "midi encode: needs a Midi value with tracks")
return nil, Err.new(Err.ARG, "needs a Midi value with tracks")
end
local running = not (opts and opts.runningStatus == false)
local format = midi.format or 1
local division = midi.division or 480
if type(format) ~= "number" or format < 0 or format > 2 or format % 1 ~= 0 then
return nil, Err.new(Err.ARG, "midi encode: format must be 0, 1 or 2")
return nil, Err.new(Err.ARG, "format must be 0, 1 or 2")
end
if type(division) ~= "number" or division == 0 or division < -32768 or division > 32767 or division % 1 ~= 0 then
return nil, Err.new(Err.ARG, "midi encode: division must be a non-zero 16-bit integer")
return nil, Err.new(Err.ARG, "division must be a non-zero 16-bit integer")
end
if format == 0 and #midi.tracks ~= 1 then
return nil, Err.new(Err.ARG, "midi encode: format 0 needs exactly one track")
return nil, Err.new(Err.ARG, "format 0 needs exactly one track")
end
if #midi.tracks > 65535 then
return nil, Err.new(Err.ARG, "midi encode: too many tracks")
return nil, Err.new(Err.ARG, "too many tracks")
end
local w = Bytes.writer(1024)
w:string("MThd")
@@ -295,7 +295,7 @@ function Smf.write(midi: Types.Midi, opts: WriteOptions?): (buffer?, Err.Err?)
for ti, track in midi.tracks do
local events = if type(track) == "table" then track.events else nil
if type(events) ~= "table" then
return nil, Err.new(Err.ARG, "midi encode: track " .. ti .. " has no events list")
return nil, Err.new(Err.ARG, "track " .. ti .. " has no events list")
end
-- stable order by tick
local sorted = true
@@ -338,7 +338,7 @@ function Smf.write(midi: Types.Midi, opts: WriteOptions?): (buffer?, Err.Err?)
end
local delta = ev.tick - prev
if delta > 0x0FFFFFFF then
return nil, Err.new(Err.ARG, "midi encode: a delta time over 0x0FFFFFFF ticks")
return nil, Err.new(Err.ARG, "a delta time over 0x0FFFFFFF ticks")
end
local base = TYPE_STATUS[t]
if base then
@@ -378,7 +378,7 @@ function Smf.write(midi: Types.Midi, opts: WriteOptions?): (buffer?, Err.Err?)
w:u8(if t == "sysex" then 0xF0 else 0xF7)
end
if #d > 0x0FFFFFFF then
return nil, Err.new(Err.ARG, "midi encode: event data too long")
return nil, Err.new(Err.ARG, "event data too long")
end
vlq(w, #d)
for _, c in d do