195 lines
5.7 KiB
Luau
195 lines
5.7 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- audio/mp3/Index: the frame index of a stream, built once on open. It follows minimp3_ex's mp3dec_load_buf frame by frame
|
|
-- (same sync, resync and Xing / LAME handling), and simulates the layer III bit reservoir from the side info, so
|
|
-- it knows in advance which frames give output and where each one's samples land. That makes the total length
|
|
-- exact and seeking sample-accurate. Never throws.
|
|
local Header = require("@formats/audio/mp3/Header")
|
|
local Core = require("@formats/audio/mp3/Core")
|
|
|
|
local band, rshift = bit32.band, bit32.rshift
|
|
local ru8 = buffer.readu8
|
|
local min, max = math.min, math.max
|
|
|
|
local compare, frameBytes, padding, findFrame =
|
|
Header.compare, Header.frameBytes, Header.padding, Header.findFrame
|
|
|
|
local Index = {}
|
|
|
|
Index.FLAG_RESET = 1 -- decoder state is zeroed before this frame (minimp3 re-synced here)
|
|
Index.FLAG_OUTPUT = 2 -- this frame gives samples in a continuous decode
|
|
|
|
export type Index = {
|
|
n: number, -- frames
|
|
off: buffer, -- u32 byte offset per frame
|
|
size: buffer, -- u16 byte size per frame (header and padding included)
|
|
flags: buffer, -- u8 FLAG_*
|
|
out: buffer, -- u32 first output sample (before the encoder delay is cut) per frame
|
|
main: buffer, -- u16 layer III main data bytes per frame (0 for layers I / II)
|
|
outTotal: number, -- samples (per channel) of a continuous decode, delay not cut
|
|
bytes: number, -- audio bytes in indexed frames
|
|
-- format of the first frame
|
|
sampleRate: number,
|
|
channels: number, -- the most channels of any frame
|
|
layer: number,
|
|
version: number, -- 1, 2 or 2.5
|
|
spf: number, -- samples per frame
|
|
-- Xing / Info / LAME
|
|
vbrTag: number, -- mp3dec_check_vbrtag result of the first frame (0 = none)
|
|
tagFrames: number,
|
|
delay: number,
|
|
padding: number,
|
|
skipped: number, -- frames with another sample rate or layer, left out
|
|
}
|
|
|
|
local function grow(b: buffer, n: number): buffer
|
|
local nb = buffer.create(n)
|
|
buffer.copy(nb, 0, b, 0, math.min(buffer.len(b), n))
|
|
return nb
|
|
end
|
|
|
|
function Index.build(b: buffer, start: number, stop: number): Index?
|
|
local cap = 1024
|
|
local OFF, SIZE, FLAGS, OUT, MAIN = buffer.create(cap * 4), buffer.create(cap * 2), buffer.create(cap), buffer.create(cap * 4), buffer.create(cap * 2)
|
|
local n = 0
|
|
local pos = start
|
|
local prev = -1 -- offset of the last decoded header (dec->header), -1 = none
|
|
local ffb = 0 -- free format bytes
|
|
local reserv = 0
|
|
local outTotal = 0
|
|
local bytes = 0
|
|
local first = true
|
|
local skipped = 0
|
|
local outCh = 0 -- the most channels of a frame that gives output
|
|
local hz, nch, layer, spf, version = 0, 0, 0, 0, 0
|
|
local vbr, tagFrames, delay, pad = 0, 0, 0, 0
|
|
|
|
while true do
|
|
local remaining = stop - pos
|
|
if remaining <= 0 then
|
|
break
|
|
end
|
|
local fs = 0
|
|
local reset = false
|
|
if remaining > 4 and prev >= 0 and compare(b, prev, pos) then
|
|
fs = frameBytes(b, pos, ffb) + padding(b, pos)
|
|
if fs ~= remaining and (fs + 4 > remaining or not compare(b, pos, pos + fs)) then
|
|
fs = 0
|
|
end
|
|
end
|
|
if fs == 0 then
|
|
reset = true
|
|
reserv = 0
|
|
local i
|
|
i, fs, ffb = findFrame(b, pos, remaining, 0)
|
|
if fs == 0 or i + fs > remaining then
|
|
break
|
|
end
|
|
pos += i
|
|
end
|
|
if fs < 4 or fs > Core.MAX_FRAME then
|
|
-- cannot happen with a well-formed header, but never index an impossible frame
|
|
prev = -1
|
|
pos += max(fs, 1)
|
|
continue
|
|
end
|
|
local h1, h2, h3 = ru8(b, pos + 1), ru8(b, pos + 2), ru8(b, pos + 3)
|
|
local fLayer = 4 - band(rshift(h1, 1), 3)
|
|
local fHz = Header.sampleRate(h1, h2)
|
|
local fCh = if band(h3, 0xC0) == 0xC0 then 1 else 2
|
|
|
|
if first then
|
|
first = false
|
|
hz, nch, layer, spf = fHz, fCh, fLayer, Header.frameSamples(h1)
|
|
version = if band(h1, 8) ~= 0 then 1 elseif band(h1, 0x10) ~= 0 then 2 else 2.5
|
|
if fLayer == 3 then
|
|
local ret, frames, d, p = Core.vbrTag(b, pos, fs)
|
|
if ret ~= 0 then
|
|
vbr, tagFrames, delay, pad = ret, frames, d, p
|
|
if ret < 0 then
|
|
delay, pad = 0, 0
|
|
end
|
|
-- the tag frame is not audio; the next frame is found again from scratch
|
|
prev = -1
|
|
pos += fs
|
|
continue
|
|
end
|
|
end
|
|
elseif fHz ~= hz or fLayer ~= layer then
|
|
skipped += 1
|
|
prev = pos
|
|
pos += fs
|
|
continue
|
|
end
|
|
|
|
local output = true
|
|
local mainBytes = 0
|
|
local nextPrev = pos
|
|
if fLayer == 3 then
|
|
local mdb, p23, mb = Core.probeL3(b, pos, fs)
|
|
if mdb < 0 then
|
|
output = false
|
|
nextPrev = -1 -- mp3dec_init: the next frame re-syncs
|
|
else
|
|
mainBytes = mb
|
|
local have = min(reserv, mdb)
|
|
output = reserv >= mdb
|
|
local remains = have + mb - (if output then (p23 + 7) // 8 else 0)
|
|
reserv = max(0, min(remains, 511))
|
|
end
|
|
else
|
|
output = Core.probeL12(b, pos, fs)
|
|
if not output then
|
|
nextPrev = -1
|
|
end
|
|
end
|
|
|
|
if output and fCh > outCh then
|
|
outCh = fCh -- mono / stereo switches: the stream is stereo, mono frames are duplicated
|
|
end
|
|
if n == cap then
|
|
cap *= 2
|
|
OFF, SIZE, FLAGS, OUT, MAIN = grow(OFF, cap * 4), grow(SIZE, cap * 2), grow(FLAGS, cap), grow(OUT, cap * 4), grow(MAIN, cap * 2)
|
|
end
|
|
buffer.writeu32(OFF, n * 4, pos)
|
|
buffer.writeu16(SIZE, n * 2, fs)
|
|
buffer.writeu8(FLAGS, n, (if reset then Index.FLAG_RESET else 0) + (if output then Index.FLAG_OUTPUT else 0))
|
|
buffer.writeu32(OUT, n * 4, outTotal)
|
|
buffer.writeu16(MAIN, n * 2, min(mainBytes, 65535))
|
|
n += 1
|
|
bytes += fs
|
|
if output then
|
|
outTotal += spf
|
|
end
|
|
prev = nextPrev
|
|
pos += fs
|
|
end
|
|
|
|
if first then
|
|
return nil
|
|
end
|
|
return {
|
|
n = n,
|
|
off = OFF,
|
|
size = SIZE,
|
|
flags = FLAGS,
|
|
out = OUT,
|
|
main = MAIN,
|
|
outTotal = outTotal,
|
|
bytes = bytes,
|
|
sampleRate = hz,
|
|
channels = if outCh > 0 then outCh else nch,
|
|
layer = layer,
|
|
version = version,
|
|
spf = spf,
|
|
vbrTag = vbr,
|
|
tagFrames = tagFrames,
|
|
delay = delay,
|
|
padding = pad,
|
|
skipped = skipped,
|
|
}
|
|
end
|
|
|
|
return Index
|