ws-audio: MP3 layer III encoder (Encode3: shine-style CBR, MPEG-1/2/2.5, mono/stereo/joint MS, global-gain rate loop, region/table choice, count1 A/B) + MP2 layer II encoder (Encode2: standard alloc tables, greedy noise allocation, grouping), shared polyphase Analysis, EncTables (PDMP3 public-domain window + Huffman trees -> codeword tables); mp3 init encode() with ID3v2.4; fixtures decode with ffmpeg + lame (no errors, SNR floors); bench lines
This commit is contained in:
1 parent
4dbd236962
commit
e817053a40
14 files changed
+1859
-12
No files matched your search
@@ -82,7 +82,7 @@ local rand = T.rng(12345)
|
||||
t:eq(Mp3.name, "mp3", "name")
|
||||
t:eq(Mp3.category, "audio", "category")
|
||||
t:check(table.find(Mp3.extensions, "mp3") ~= nil and table.find(Mp3.extensions, "mp2") ~= nil, "extensions")
|
||||
t:check(Mp3.caps.decode and not Mp3.caps.encode and Mp3.open ~= nil, "caps")
|
||||
t:check(Mp3.caps.decode and Mp3.caps.encode and Mp3.open ~= nil, "caps") -- encode: ws-audio (tests/audio/mp3_encode.luau)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode vs references
|
||||
for _, c in Cases do
|
||||
@@ -487,8 +487,8 @@ do
|
||||
t:check(a and a.kind == "audio" and a.frames == 22050 and a.tags.title == "Test Title", "F.audio.load: " .. tostring(err))
|
||||
local dec = F.audio.open(data)
|
||||
t:check(dec and dec.sampleRate == 44100 and dec.frames == 22050, "F.audio.open")
|
||||
local _, serr = F.save(a, nil, { format = "mp3" })
|
||||
t:check(serr and serr.code == Err.UNSUPPORTED, "F.save mp3: read-only")
|
||||
local saved, serr = F.save(a, nil, { format = "mp3" }) -- the layer III encoder (ws-audio)
|
||||
t:check(saved and F.audio.load(saved).sampleRate == 44100, "F.save mp3: " .. tostring(serr))
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
-- audio/mp3 encoder tests (layer III and layer II): every fixture-step encode (sample rates of MPEG-1 / 2 / 2.5,
|
||||
-- bit rates 24 .. 384 kbps, stereo / joint / mono) decodes in ffmpeg without errors and in LAME, with the expected
|
||||
-- length and an SNR over a per-bit-rate floor after aligning the decoder delay; our own decoder (the minimp3 port)
|
||||
-- agrees; streaming from a Decoder gives the same bytes; the ID3v2 tag round-trips; option errors.
|
||||
-- Fixtures: tools/fixtures/mp3_enc.py.
|
||||
local T = require("@tests/T")
|
||||
local Mp3 = require("@formats/audio/mp3")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
local Enc = require("@tests/gen/mp3_enc")
|
||||
local Sigs = require("@tests/gen/mp3_enc_sigs")
|
||||
|
||||
local t = T.suite("mp3_encode")
|
||||
|
||||
local function audio(name: string): Types.Audio
|
||||
local s = Sigs[name]
|
||||
local a = Types.Audio.new(s.rate, s.channels, s.frames)
|
||||
buffer.writestring(a.samples, 0, s.data)
|
||||
return a
|
||||
end
|
||||
|
||||
-- SNR of y (delayed by lag frames) against x over frames [from, to)
|
||||
local function snr(x: Types.Audio, y: Types.Audio, lag: number, from: number, to: number): number
|
||||
local ch = x.channels
|
||||
local sig, er = 0, 0
|
||||
for i = from, to - 1 do
|
||||
local j = i + lag
|
||||
if j >= 0 and j < y.frames then
|
||||
for c = 0, ch - 1 do
|
||||
local a = buffer.readf32(x.samples, (i * ch + c) * 4)
|
||||
local b = buffer.readf32(y.samples, (j * y.channels + math.min(c, y.channels - 1)) * 4)
|
||||
sig += a * a
|
||||
er += (a - b) * (a - b)
|
||||
end
|
||||
end
|
||||
end
|
||||
if er == 0 then
|
||||
return 200
|
||||
end
|
||||
return if sig > 0 then 10 * math.log10(sig / er) else -200
|
||||
end
|
||||
|
||||
-- the SNR floors (dB) on the music / sine / sweep signals, by layer and bit rate per channel
|
||||
local function floorFor(e: any): number
|
||||
if string.find(e.name, "silence", 1, true) then
|
||||
return -math.huge
|
||||
elseif string.find(e.name, "noise", 1, true) then
|
||||
return 8 -- white noise has no spectral structure to exploit
|
||||
end
|
||||
local perCh = e.bitrate / (if e.mode == "mono" then 1 else 2)
|
||||
local nyquistBand = e.rate <= 16000 -- low rates lose little to the encoder's band limit
|
||||
if e.layer == 3 then
|
||||
if perCh >= 64 then
|
||||
return 14
|
||||
elseif perCh >= 32 or nyquistBand then
|
||||
return 8
|
||||
end
|
||||
return 4
|
||||
end
|
||||
if perCh >= 96 then
|
||||
return 14
|
||||
elseif perCh >= 48 then
|
||||
return 8
|
||||
end
|
||||
return 4
|
||||
end
|
||||
|
||||
t:check(Mp3.caps.encode and Mp3.encode ~= nil, "caps.encode")
|
||||
|
||||
local minByLayer = { [2] = math.huge, [3] = math.huge }
|
||||
for _, e in Enc do
|
||||
t:eq(e.ffErrors, "", e.name .. ": ffmpeg decodes without errors")
|
||||
t:check(e.ffRate == e.rate, e.name .. ": rate")
|
||||
t:check(e.ffChannels == (if e.mode == "mono" then 1 else 2), e.name .. ": channels")
|
||||
-- the stream covers the input: the encoder adds one frame of flush and the filterbank delay
|
||||
local spf = if e.layer == 3 and e.rate < 32000 then 576 else 1152
|
||||
t:check(e.ffFrames >= e.frames and e.ffFrames <= e.frames + 3 * spf, string.format("%s: %d decoded frames for %d", e.name, e.ffFrames, e.frames))
|
||||
local f = floorFor(e)
|
||||
t:check(e.ffSnr >= f, string.format("%s: ffmpeg SNR %.1f dB (floor %g)", e.name, e.ffSnr, f))
|
||||
if e.lameSnr then
|
||||
t:check(e.lameSnr >= f, string.format("%s: LAME SNR %.1f dB (floor %g)", e.name, e.lameSnr, f))
|
||||
end
|
||||
if f > -math.huge then
|
||||
minByLayer[e.layer] = math.min(minByLayer[e.layer], e.ffSnr)
|
||||
end
|
||||
-- our decoder agrees with ffmpeg
|
||||
local x = audio(e.sig)
|
||||
local y, err = Mp3.decode(Bytes.reader(buffer.fromstring(e.data)))
|
||||
t:check(y, e.name .. ": our decoder: " .. tostring(err))
|
||||
if e.channels == y.channels then
|
||||
local s = snr(x, y, e.ffLag, 200, x.frames)
|
||||
t:check(s >= f and math.abs(s - e.ffSnr) < 3, string.format("%s: our decoder %.1f dB, ffmpeg %.1f dB", e.name, s, e.ffSnr))
|
||||
end
|
||||
end
|
||||
t:note("%d encodes: ffmpeg + LAME decode them; lowest SNR layer III %.1f dB, layer II %.1f dB", #Enc, minByLayer[3], minByLayer[2])
|
||||
|
||||
-- streaming, tags, defaults
|
||||
do
|
||||
local a = audio("music_2_44100")
|
||||
local f1 = Mp3.encode(a, { bitrate = 128 }) :: buffer
|
||||
local f2 = Mp3.encode(Pcm.audioDecoder(a), { bitrate = 128 }) :: buffer
|
||||
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
|
||||
a.tags = { title = "Ünïcode title", artist = "An Artist", album = "Album", track = "4", comment = "made here" }
|
||||
local f3 = Mp3.encode(a, { layer = 2 }) :: buffer
|
||||
t:eq(buffer.readstring(f3, 0, 3), "ID3", "an ID3v2 tag in front")
|
||||
local back = Mp3.open(Bytes.reader(f3)) :: any
|
||||
t:eq(back.tags.title, "Ünïcode title", "title")
|
||||
t:eq(back.tags.artist, "An Artist", "artist")
|
||||
t:eq(back.tags.album, "Album", "album")
|
||||
t:eq(back.tags.track, "4", "track")
|
||||
t:eq(back.layer, 2, "layer II by default bit rate 192")
|
||||
local f4 = Mp3.encode(a, { id3 = false }) :: buffer
|
||||
t:check(buffer.readu8(f4, 0) == 0xFF, "id3 = false")
|
||||
local m = Types.Audio.new(22050, 1, 3000)
|
||||
local fm = Mp3.encode(m) :: buffer
|
||||
local dm = Mp3.open(Bytes.reader(fm)) :: any
|
||||
t:check(dm.channels == 1 and dm.sampleRate == 22050, "mono MPEG-2 default")
|
||||
local empty = Mp3.encode(Types.Audio.new(44100, 2, 0)) :: buffer
|
||||
t:check(empty and buffer.len(empty) > 0, "empty input: one flush frame")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local function code(v: any, o: any): string?
|
||||
local _, e = Mp3.encode(v, o)
|
||||
return e and e.code
|
||||
end
|
||||
local a = Types.Audio.new(44100, 2, 100)
|
||||
t:eq(code({}, nil), Err.ARG, "junk")
|
||||
t:eq(code(a, { layer = 1 }), Err.ARG, "layer I")
|
||||
t:eq(code(a, { bitrate = 100 }), Err.ARG, "100 kbps")
|
||||
t:eq(code(a, { mode = "dual" }), Err.ARG, "dual mode")
|
||||
t:eq(code(Types.Audio.new(44000, 2, 10), nil), Err.ARG, "44 kHz")
|
||||
t:eq(code(Types.Audio.new(8000, 2, 10), { layer = 2 }), Err.ARG, "layer II at 8 kHz")
|
||||
t:eq(code(Types.Audio.new(44100, 3, 10), nil), Err.ARG, "3 channels")
|
||||
t:eq(code(a, { layer = 2, bitrate = 32 }), Err.ARG, "layer II stereo at 32 kbps")
|
||||
t:eq(code(Types.Audio.new(44100, 1, 10), { layer = 2, bitrate = 320 }), Err.ARG, "layer II mono at 320 kbps")
|
||||
end
|
||||
|
||||
-- (the decoder's fuzz pass is in tests/audio/mp3.luau; the encoders take no untrusted bytes)
|
||||
t:done()
|
||||
Reference in new issue
Block a user