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.
This commit is contained in:
4DBug committed 2026-10-31 11:01:22 -05:00
1 parent f784b4eb9b
commit c0eafd29a5
27 files changed
+164 -170

No files matched your search

+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