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:
1 parent
8158acf3d5
commit
fb2dd98db2
27 files changed
+164
-170
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user