ws-m4a: CELT-only Opus encoder in audio/opus (RangeEncoder + CeltEncoder ported from the libopus 1.6 encoder paths to mirror the absorbed decoder: 20 ms full-band CBR, mono and mid/side stereo, silence frames; Encode: Ogg Opus with OpusHead/OpusTags, end trimming, 48 kHz resampling); checked by opusdec / opusinfo / ffmpeg; Opus.caps.encode = true (opus test updated), deps += codecs/Mdct

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 09:22:52 -05:00
1 parent c3cf73e29f
commit c43c56eadd
13 files changed
+1761 -8

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
-- tests/audio/_opus_cases: the inputs of the CELT encoder reference check (tools/fixtures/opus_celt.py runs
-- tools/fixtures/_opus_encode.luau over them; tests/audio/opus_encode.luau re-encodes and compares): a deterministic
-- music-like signal (chord + vibrato + chirp + clicks + a silent stretch) at a given rate / channel count.
local Types = require("@formats/Types")
local Cases = {}
Cases.list = {
{ name = "celt_mono_64k", rate = 48000, channels = 1, seconds = 1.5, bitrate = 64000, minSnr = 15 },
{ name = "celt_stereo_128k", rate = 48000, channels = 2, seconds = 1.5, bitrate = 128000, minSnr = 15 },
{ name = "celt_stereo_256k", rate = 48000, channels = 2, seconds = 1, bitrate = 256000, minSnr = 22 },
{ name = "celt_mono_24k", rate = 48000, channels = 1, seconds = 1, bitrate = 24000, minSnr = 8 },
{ name = "celt_44k_stereo_96k", rate = 44100, channels = 2, seconds = 1, bitrate = 96000, minSnr = 0, tags = { title = "Celt \u{e9}", artist = "formats-luau", album = "Opus" } },
{ name = "celt_short", rate = 48000, channels = 1, seconds = 0.01, bitrate = 48000, minSnr = -100 },
}
function Cases.signal(c: any): Types.Audio
local n = math.floor(c.seconds * c.rate + 0.5)
local a = Types.Audio.new(c.rate, c.channels, n)
local s = 7
for i = 0, n - 1 do
local t = i / c.rate
for ch = 0, c.channels - 1 do
s = (s * 48271) % 2147483647
local v = 0.18 * math.sin(2 * math.pi * 220 * t + 2 * math.sin(2 * math.pi * 5 * t))
v += 0.12 * math.sin(2 * math.pi * (277.2 + ch * 2) * t) + 0.1 * math.sin(2 * math.pi * 329.6 * t)
v += 0.08 * math.sin(2 * math.pi * (1000 + 3000 * t) * t)
v += 0.01 * (s / 2147483647 - 0.5)
if i % (c.rate // 4) < 30 then
v += 0.4 * (if (i // 5) % 2 == 0 then 1 else -1)
end
if t > 0.6 and t < 0.75 then
v = 0 -- digital silence (the encoder's silence frames)
end
buffer.writef32(a.samples, (i * c.channels + ch) * 4, v)
end
end
a.tags = c.tags or {}
return a
end
return Cases
+1 -1
View File
@@ -81,7 +81,7 @@ end
-- ------------------------------------------------------------------------------------------------ contract
t:eq(Opus.name, "opus", "name")
t:eq(Opus.category, "audio", "category")
t:check(Opus.caps.decode and not Opus.caps.encode and Opus.open ~= nil and Opus.decode ~= nil, "caps")
t:check(Opus.caps.decode and Opus.caps.encode and Opus.open ~= nil and Opus.decode ~= nil and Opus.encode ~= nil, "caps (the CELT encoder: tests/audio/opus_encode.luau)")
t:eq(Opus.sniff(Bytes.reader(FX.celt_st_128.data)), 1, "sniff an Opus file")
t:eq(Opus.sniff(Bytes.reader(buffer.fromstring("OggS" .. string.rep("\0", 40)))), 0, "sniff another Ogg")
t:eq(Opus.sniff(Bytes.reader(buffer.create(0))), 0, "sniff nothing")
+114
View File
@@ -0,0 +1,114 @@
-- audio/opus encoder tests (the CELT-only writer, ws-m4a; mono and mid / side stereo): our Ogg Opus files (re-encoded here, compared with the bytes
-- checked in the fixture step) decode in libopus' opusdec to the input's exact length (pre-skip + end trimming) with a
-- minimum SNR, opusinfo finds no problem, ffmpeg reads them, OpusTags carry the tags; our decoder agrees with opusdec;
-- resampled 44.1 kHz input; silence frames; streaming input; errors; a fuzz no-throw pass over our files.
-- Fixtures: tools/fixtures/opus_celt.py.
local T = require("@tests/T")
local Opus = require("@formats/audio/opus")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Cases = require("@tests/audio/_opus_cases")
local Fx = require("@tests/gen/opus_celt")
local t = T.suite("opus-encode")
t:check(Opus.caps.encode and Opus.encode ~= nil, "caps: encode")
local byName = {}
for _, f in Fx do
byName[f.name] = f
end
local function snr(ref: buffer, refBase: number, got: buffer, n: number): number
local s, e = 0, 0
for i = 0, n - 1 do
local x = buffer.readf32(ref, (refBase + i) * 4)
local d = buffer.readf32(got, (refBase + i) * 4) - x
s += x * x
e += d * d
end
return if e == 0 then math.huge else 10 * math.log10(s / math.max(e, 1e-30))
end
local files = {}
for _, c in Cases.list do
local fx = byName[c.name]
t:check(fx, c.name .. ": fixture")
local input = Cases.signal(c)
local file, err = Opus.encode(input, { bitrate = c.bitrate })
t:check(file, c.name .. ": encode: " .. tostring(err))
table.insert(files, file)
t:eq(string.format("%08x", Crc32.of(file :: buffer)), string.format("%08x", fx.crc), c.name .. ": the bytes the reference tools checked")
t:check(fx.opusinfo, c.name .. ": opusinfo reports no problem")
t:check(fx.ffmpeg, c.name .. ": ffmpeg decodes it")
local frames48 = math.floor(input.frames * 48000 / c.rate + 0.5)
local ch = c.channels
t:eq(#fx.opusdec // (4 * ch), frames48, c.name .. ": opusdec length = the input's (pre-skip + end trimming)")
t:eq(fx.ffmpegFrames, frames48, c.name .. ": ffmpeg length")
-- our decoder vs opusdec
local ours = Opus.decode(Bytes.reader(file :: buffer)) :: any
t:eq(ours.frames, frames48, c.name .. ": our decoder's length")
local ref = buffer.fromstring(fx.opusdec)
local agree = snr(ref, 0, ours.samples, frames48 * ch)
t:check(agree > 60, string.format("%s: our decoder vs opusdec %.1f dB", c.name, agree))
-- quality vs the input (48 kHz inputs; skip the edges)
if c.rate == 48000 and frames48 > 4000 then
local s = snr(input.samples, 960 * ch, ref, (frames48 - 1920) * ch)
t:check(s >= c.minSnr, string.format("%s: SNR %.1f dB (minimum %d)", c.name, s, c.minSnr))
t:note("%s: %d bytes, SNR %.1f dB vs the input, our decoder vs opusdec %.1f dB", c.name, buffer.len(file :: buffer), s, agree)
end
if c.tags then
for k, v in c.tags do
t:eq(fx.comments[k], v, c.name .. ": opusinfo comment " .. k)
t:eq(ours.tags[k], v, c.name .. ": our tag " .. k)
end
end
end
-- silence frames decode to silence
do
local c = Cases.list[1]
local ref = buffer.fromstring(byName[c.name].opusdec)
local peak = 0
for i = math.floor(0.62 * 48000), math.floor(0.73 * 48000) do
peak = math.max(peak, math.abs(buffer.readf32(ref, i * 4)))
end
t:check(peak < 1e-3, "digital silence stays silent (peak " .. peak .. ")")
end
-- a streaming decoder as the input
do
local c = Cases.list[1]
local dec = Opus.open(Bytes.reader(files[1] :: buffer)) :: any
local again, err = Opus.encode(dec, { bitrate = 64000 })
t:check(again, "encode from a decoder: " .. tostring(err))
local back = Opus.decode(Bytes.reader(again :: buffer)) :: any
t:eq(back.frames, math.floor(c.seconds * 48000 + 0.5), "re-encoded length")
end
-- errors
do
local three = Cases.signal({ rate = 48000, channels = 3, seconds = 0.05 })
local _, e = Opus.encode(three)
t:check(e and e.code == Err.UNSUPPORTED, "3 channels: " .. tostring(e))
local _, e2 = Opus.encode(Cases.signal(Cases.list[6]), { bitrate = 1000 })
t:check(e2 and e2.code == Err.ARG, "bitrate too low")
local _, e3 = Opus.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "not audio")
end
-- fuzz the decoder over our files
do
local samples = {}
for i, f in files do
if i <= 3 then
table.insert(samples, string.sub(buffer.tostring(f :: buffer), 1, 4000))
end
end
t:fuzz("decode our files", function(b: buffer)
return Opus.decode(Bytes.reader(b))
end, samples, { mutations = 10, garbage = 30 })
end
t:done()