ws-audio: FLAC encoder (audio/flac/Encode: levels 0-8 like libFLAC presets, fixed + LPC + Rice/Rice2 partition search, wasted bits, stereo decorrelation, MD5, VORBIS_COMMENT, SEEKTABLE, padding; streams from a Decoder) wired into audio/flac encode; core/Md5 (new core module, RFC 1321) + test; fixtures: flac -t + MD5 bit-exact on 76 encodes, sizes within 1.1% of libFLAC; bench lines
This commit is contained in:
1 parent
1ea2f34159
commit
471b7636ef
10 files changed
+1553
-6
No files matched your search
@@ -0,0 +1,109 @@
|
||||
-- audio/flac encoder tests: every fixture-step encode (levels 0-8, 1 / 2 / 6 channels, 8 / 12 / 16 / 20 / 24 bits,
|
||||
-- odd lengths and block sizes, odd rates, seek tables, padding) passed `flac -t` and carries the MD5 of the quantized
|
||||
-- input; sizes within 10% of libFLAC at the same level; each decodes bit-exactly through our own decoder; streaming
|
||||
-- from a Decoder gives the same bytes; tags; option errors. Fixtures: tools/fixtures/flac_enc.py.
|
||||
local T = require("@tests/T")
|
||||
local Flac = require("@formats/audio/flac")
|
||||
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/flac_enc")
|
||||
local Sigs = require("@tests/gen/flac_enc_sigs")
|
||||
|
||||
local t = T.suite("flac_encode")
|
||||
|
||||
local sigCache: { [string]: any } = {}
|
||||
local function sig(name: string): Types.Audio
|
||||
local s = sigCache[name]
|
||||
if not s then
|
||||
local m = Sigs[name]
|
||||
s = Types.Audio.new(m.rate, m.channels, m.frames)
|
||||
buffer.writestring(s.samples, 0, m.data)
|
||||
sigCache[name] = s
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
t:check(Flac.caps.encode and Flac.encode ~= nil, "caps.encode")
|
||||
|
||||
local worst = 0
|
||||
for _, e in Enc do
|
||||
t:eq(e.flacTest, "", e.name .. ": flac -t")
|
||||
t:eq(e.md5, e.wantMd5, e.name .. ": STREAMINFO MD5 = MD5 of the quantized input")
|
||||
if e.refSize then
|
||||
local ratio = e.size / e.refSize
|
||||
worst = math.max(worst, ratio)
|
||||
t:check(ratio < 1.1, string.format("%s: %d bytes, libFLAC %d (%.3f)", e.name, e.size, e.refSize, ratio))
|
||||
end
|
||||
-- our decoder: bit-exact against the input quantized to e.bits
|
||||
local a = sig(e.sig)
|
||||
local back, err = Flac.decode(Bytes.reader(buffer.fromstring(e.data)))
|
||||
t:check(back, e.name .. ": our decoder: " .. tostring(err))
|
||||
t:check(back.frames == a.frames and back.channels == a.channels and back.sampleRate == a.sampleRate, e.name .. ": format")
|
||||
local s = 2 ^ (e.bits - 1)
|
||||
local exact = true
|
||||
for i = 0, a.frames * a.channels - 1 do
|
||||
local want = math.clamp(math.floor(buffer.readf32(a.samples, i * 4) * s + 0.5), -s, s - 1)
|
||||
if math.floor(buffer.readf32(back.samples, i * 4) * s + 0.5) ~= want then
|
||||
exact = false
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(exact, e.name .. ": bit-exact round trip")
|
||||
t:eq(back.meta.source.bitDepth, e.bits, e.name .. ": bits per sample")
|
||||
end
|
||||
t:note("%d encodes pass flac -t with the right MD5 and decode bit-exactly; worst size vs libFLAC %.3f", #Enc, worst)
|
||||
|
||||
-- streaming: a Decoder source gives the same bytes as the Audio
|
||||
do
|
||||
local a = sig("flacsig_music_2_7000_44100")
|
||||
local f1 = Flac.encode(a, { level = 5 }) :: buffer
|
||||
local f2 = Flac.encode(Pcm.audioDecoder(a), { level = 5 }) :: buffer
|
||||
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
|
||||
-- a decoder of our own output re-encodes to the same stream (its bit depth carries over)
|
||||
local dec = Flac.open(Bytes.reader(f1)) :: any
|
||||
local f3 = Flac.encode(dec, { level = 5 }) :: buffer
|
||||
t:check(buffer.tostring(f3) == buffer.tostring(f1), "FLAC -> FLAC through the streaming decoder")
|
||||
end
|
||||
|
||||
-- tags, bit depth from the source
|
||||
do
|
||||
local a = Types.Audio.new(48000, 1, 3000)
|
||||
for i = 0, 2999 do
|
||||
buffer.writef32(a.samples, i * 4, math.floor(math.sin(i / 7) * 100) / 128)
|
||||
end
|
||||
a.tags = { title = "Tïtle", artist = "Artist", track = "3" }
|
||||
a.meta.source = { format = "wav", bitDepth = 8 }
|
||||
local f = Flac.encode(a) :: buffer
|
||||
local back = Flac.decode(Bytes.reader(f)) :: Types.Audio
|
||||
t:eq(back.meta.source.bitDepth, 8, "bit depth from meta.source")
|
||||
-- (the absorbed FLAC decoder keeps Vorbis comment keys upper case)
|
||||
local tags = back.tags :: any
|
||||
t:eq(tags.title or tags.TITLE, "Tïtle", "title")
|
||||
t:eq(tags.artist or tags.ARTIST, "Artist", "artist")
|
||||
t:eq(tags.track or tags.TRACKNUMBER, "3", "track number")
|
||||
local empty = Flac.encode(Types.Audio.new(44100, 2, 0)) :: buffer
|
||||
local e0 = Flac.decode(Bytes.reader(empty))
|
||||
t:check(e0 and e0.frames == 0, "empty audio")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local a = Types.Audio.new(44100, 2, 10)
|
||||
local function code(v: any, o: any): string?
|
||||
local _, e = Flac.encode(v, o)
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code({}, nil), Err.ARG, "junk value")
|
||||
t:eq(code(a, { level = 9 }), Err.ARG, "level 9")
|
||||
t:eq(code(a, { level = 2.5 }), Err.ARG, "level 2.5")
|
||||
t:eq(code(a, { bits = 32 }), Err.ARG, "32 bits")
|
||||
t:eq(code(a, { bits = 3 }), Err.ARG, "3 bits")
|
||||
t:eq(code(a, { blockSize = 8 }), Err.ARG, "block size 8")
|
||||
t:eq(code(Types.Audio.new(44100, 9, 1), nil), Err.ARG, "9 channels")
|
||||
t:eq(code(Types.Audio.new(44100.5, 1, 1), nil), Err.ARG, "fractional rate")
|
||||
end
|
||||
|
||||
-- (the decoder's fuzz pass is in tests/audio/flac.luau; the encoder takes no untrusted bytes)
|
||||
t:done()
|
||||
@@ -0,0 +1,30 @@
|
||||
-- core/Md5 tests: the RFC 1321 test suite, incremental updates at every split, sub-ranges, a long input.
|
||||
local T = require("@tests/T")
|
||||
local Md5 = require("@formats/core/Md5")
|
||||
|
||||
local t = T.suite("md5")
|
||||
local VECTORS = {
|
||||
{ "", "d41d8cd98f00b204e9800998ecf8427e" },
|
||||
{ "a", "0cc175b9c0f1b6a831c399e269772661" },
|
||||
{ "abc", "900150983cd24fb0d6963f7d28e17f72" },
|
||||
{ "message digest", "f96b697d7cb7938d525a2f31aaf161d0" },
|
||||
{ "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b" },
|
||||
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f" },
|
||||
{ string.rep("1234567890", 8), "57edf4a22be3c955ac49da2e2107b67a" },
|
||||
}
|
||||
for _, v in VECTORS do
|
||||
t:eq(Md5.of(buffer.fromstring(v[1])), v[2], "md5 of " .. string.format("%q", v[1]))
|
||||
end
|
||||
-- incremental: every split point of a 130-byte message, three pieces
|
||||
local msg = string.rep("The quick brown fox jumps over the lazy dog. ", 3)
|
||||
local want = Md5.of(buffer.fromstring(msg))
|
||||
for cut = 0, #msg, 7 do
|
||||
local h = Md5.new()
|
||||
h:update(buffer.fromstring(string.sub(msg, 1, cut)))
|
||||
h:update(buffer.fromstring(string.sub(msg, cut + 1, cut + 20)))
|
||||
h:update(buffer.fromstring(string.sub(msg, cut + 21)))
|
||||
t:eq(Md5.hex(h:digest()), want, "split at " .. cut)
|
||||
end
|
||||
t:eq(Md5.of(buffer.fromstring("xxabcyy"), 2, 3), "900150983cd24fb0d6963f7d28e17f72", "sub-range")
|
||||
t:eq(Md5.of(buffer.fromstring(string.rep("a", 1000000))), "7707d6ae4e027c70eea2a935c2296f21", "a million a's")
|
||||
t:done()
|
||||
Reference in new issue
Block a user