ws-m4a: bench lines (AAC-LC / ALAC decode, ALAC encode, MDCT; bench.sh also builds the m4a fixtures); m4a decoder bitrate from the sample sizes

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 09:04:03 -05:00
1 parent f66594908c
commit fb339607a7
3 files changed
+45 -4

No files matched your search

+5 -3
View File
@@ -194,7 +194,7 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
channels = channels,
frames = frames,
duration = frames / sr,
bitrate = if movie.duration > 0 then math.floor((r.limit - r.base) * 8 / math.max(frames / sr, 1e-9)) else 0,
bitrate = 0,
tags = tags,
pictures = pictures,
codec = t.codec,
@@ -202,9 +202,11 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
movie = movie,
error = nil,
}
if t.bitrate then
dec.bitrate = t.bitrate
local bytes = 0
for i = 1, t.sampleCount do
bytes += t.sizes[i]
end
dec.bitrate = if frames > 0 then math.floor(bytes * 8 * sr / frames) else (t.bitrate or 0)
-- decode the next packet into pend; false at the end / on an error
local function fill(): boolean
+39
View File
@@ -56,3 +56,42 @@ do
Png.encode(img)
end)
end
-- ws-m4a: AAC-LC / ALAC decode and ALAC encode (MB/s of 16-bit PCM equivalent: frames * channels * 2), MDCT
do
local M4a = require("@formats/audio/m4a")
local Alac = require("@formats/codecs/Alac")
local Mdct = require("@formats/codecs/Mdct")
local MA = require("@tests/gen/m4a_aac")
local ML = require("@tests/gen/m4a_alac")
local aac = buffer.fromstring(MA[1].data)
local a = assert(M4a.decode(Bytes.reader(aac)))
bench("aac-lc decode (m4a, stereo)", 3, a.frames * a.channels * 2, function()
M4a.decode(Bytes.reader(aac))
end)
local alac = buffer.fromstring(ML[1].data)
local l = assert(M4a.decode(Bytes.reader(alac)))
bench("alac decode (m4a, stereo 16)", 3, l.frames * l.channels * 2, function()
M4a.decode(Bytes.reader(alac))
end)
local enc = assert(Alac.encoder({ sampleRate = 44100, channels = 2, bitDepth = 16 }))
local ints = table.create(a.frames * 2, 0)
for i = 1, a.frames * 2 do
ints[i] = math.floor(buffer.readf32(a.samples, (i - 1) * 4) * 32767)
end
bench("alac encode (stereo 16)", 2, a.frames * 2 * 2, function()
local f = 0
while f < a.frames do
local n = math.min(4096, a.frames - f)
enc:encode(ints, f * 2 + 1, n)
f += n
end
end)
local plan = assert(Mdct.new(2048))
local x, X = table.create(2048, 0.5), table.create(1024, 0)
bench("mdct 2048 x 1000 (forward)", 3, 1000 * 2048 * 4, function()
for _ = 1, 1000 do
Mdct.forward(plan, x, 1, X, 1)
end
end)
end
+1 -1
View File
@@ -3,7 +3,7 @@
cd "$(dirname "$0")/.." || exit 1
LUAU=${LUAU:-$(command -v luau || nix shell nixpkgs#luau -c sh -c 'command -v luau')}
LUAJIT=${LUAJIT:-$(command -v luajit || nix shell nixpkgs#luajit -c sh -c 'command -v luajit')}
python3 -I tools/fixtures.py deflate png > /dev/null || exit 1
python3 -I tools/fixtures.py deflate png m4a > /dev/null || exit 1
python3 -I tools/build.py luajit > /dev/null || exit 1
"$LUAU" --codegen -O2 tools/bench.luau
"$LUAJIT" -e 'package.path = "build/luajit/?.lua;build/luajit/?/init.lua;" .. package.path' build/luajit/tools/bench.lua