Files
rosetta/src/Formats/codecs/audio/Mpa/Header.luau
T
4DBugandClaude Opus 5.5 c4f6e2ca1c ws-cleanup: audio codecs out of the format folders (codecs/audio/Opus, Vorbis, Flac, Mpa)
The decoders / encoders are libraries, not the file format: audio/opus/{Celt, CeltTables, CeltEncoder, Mdct, Packet,
Silk, SilkTables, Decoder} -> codecs/audio/Opus; audio/vorbis/{Codebook, Packet, Setup, Tables} ->
codecs/audio/Vorbis; audio/flac/{Frame, Encode} -> codecs/audio/Flac; audio/mp3/{Core, Tables, Header, Encode2,
Encode3, EncTables, Analysis} -> codecs/audio/Mpa (tools/move.py; code unchanged). Each gets an index init.luau.
The format folders keep the file mapping: Ogg Opus / Ogg Vorbis (headers, granules, seeking, the Vorbis encoder's
Ogg writer), native / Ogg FLAC streams and metadata, ID3 / Xing / the MP3 frame index. docs: STYLE says a codec is
not the format. Full suite: 248 runs pass on both runtimes, lint green.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:47:02 -05:00

141 lines
3.9 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/audio/Mpa/Header: MPEG audio frame headers (minimp3's hdr_* macros and functions), frame sync (mp3d_find_frame /
-- mp3d_match_frame). Every function reads 4 header bytes at a byte offset of a buffer; callers keep offset + 4
-- inside the buffer. Never throws.
local T = require("@formats/codecs/audio/Mpa/Tables")
local band, bxor, rshift = bit32.band, bit32.bxor, bit32.rshift
local ru8 = buffer.readu8
local HALFRATE = T.halfrate
local HZ = { [0] = 44100, 48000, 32000 }
local H = {}
H.MAX_FREE_FORMAT_FRAME_SIZE = 2304
H.MAX_FRAME_SYNC_MATCHES = 10
-- hdr_valid
function H.valid(b: buffer, o: number): boolean
if ru8(b, o) ~= 0xFF then
return false
end
local h1, h2 = ru8(b, o + 1), ru8(b, o + 2)
return (band(h1, 0xF0) == 0xF0 or band(h1, 0xFE) == 0xE2)
and band(rshift(h1, 1), 3) ~= 0
and rshift(h2, 4) ~= 15
and band(rshift(h2, 2), 3) ~= 3
end
local valid = H.valid
-- hdr_compare: h2 is a valid header of the same version / layer / sample rate / free-format-ness as h1
function H.compare(b: buffer, o1: number, o2: number): boolean
if not valid(b, o2) then
return false
end
local a1, a2 = ru8(b, o1 + 1), ru8(b, o1 + 2)
local c1, c2 = ru8(b, o2 + 1), ru8(b, o2 + 2)
return band(bxor(a1, c1), 0xFE) == 0
and band(bxor(a2, c2), 0x0C) == 0
and (band(a2, 0xF0) == 0) == (band(c2, 0xF0) == 0)
end
local compare = H.compare
-- from the header bytes h1 = byte 1, h2 = byte 2
function H.bitrateKbps(h1: number, h2: number): number
local mpeg1 = if band(h1, 8) ~= 0 then 1 else 0
local layer = band(rshift(h1, 1), 3)
return 2 * HALFRATE[mpeg1 * 45 + (layer - 1) * 15 + rshift(h2, 4)]
end
local bitrateKbps = H.bitrateKbps
function H.sampleRate(h1: number, h2: number): number
local hz = HZ[band(rshift(h2, 2), 3)]
if band(h1, 8) == 0 then
hz //= 2
end
if band(h1, 0x10) == 0 then
hz //= 2
end
return hz
end
local sampleRate = H.sampleRate
function H.frameSamples(h1: number): number
if band(h1, 6) == 6 then
return 384
elseif band(h1, 14) == 2 then
return 576
end
return 1152
end
local frameSamples = H.frameSamples
-- hdr_frame_bytes (without padding); free format = 0 -> freeSize
function H.frameBytes(b: buffer, o: number, freeSize: number): number
local h1, h2 = ru8(b, o + 1), ru8(b, o + 2)
local hz = sampleRate(h1, h2)
local fb = frameSamples(h1) * bitrateKbps(h1, h2) * 125 // hz
if band(h1, 6) == 6 then
fb = band(fb, 0xFFFFFFFC)
end
return if fb ~= 0 then fb else freeSize
end
local frameBytes = H.frameBytes
function H.padding(b: buffer, o: number): number
if band(ru8(b, o + 2), 2) == 0 then
return 0
end
return if band(ru8(b, o + 1), 6) == 6 then 4 else 1
end
local padding = H.padding
-- mp3d_match_frame: the frames chained after o (n bytes left) keep comparing equal
local function matchFrame(b: buffer, o: number, n: number, fb: number): boolean
local i = 0
for nmatch = 0, H.MAX_FRAME_SYNC_MATCHES - 1 do
i += frameBytes(b, o + i, fb) + padding(b, o + i)
if i + 4 > n then
return nmatch > 0
end
if not compare(b, o, o + i) then
return false
end
end
return true
end
-- mp3d_find_frame over b[base, base + n): returns (skip, frame bytes incl. padding or 0, free format size)
function H.findFrame(b: buffer, base: number, n: number, ffb: number): (number, number, number)
for i = 0, n - 5 do
local o = base + i
if valid(b, o) then
local fb = frameBytes(b, o, ffb)
local fap = fb + padding(b, o)
local k = 4
while fb == 0 and k < H.MAX_FREE_FORMAT_FRAME_SIZE and i + 2 * k < n - 4 do
if compare(b, o, o + k) then
local f = k - padding(b, o)
local nextfb = f + padding(b, o + k)
if not (i + k + nextfb + 4 > n or not compare(b, o, o + k + nextfb)) then
fap = k
fb = f
ffb = f
end
end
k += 1
end
if (fb ~= 0 and i + fap <= n and matchFrame(b, o, n - i, fb)) or (i == 0 and fap == n) then
return i, fap, ffb
end
ffb = 0
end
end
return n, 0, ffb
end
return H