Bit-exact with vgmstream (ADX, PS-ADPCM) and ffmpeg (KVAG IMA); ADX and PS-ADPCM encoders in codecs/audio/Adpcm.
232 lines
9.2 KiB
Luau
232 lines
9.2 KiB
Luau
--!strict
|
|
-- audio/vag: Sony VAG (PlayStation SPU PS-ADPCM, .vag), and Simon & Schuster / Hyperscan KVAG (IMA ADPCM, the same
|
|
-- extension), over codecs/audio/Adpcm. New in formats-luau; the header variants and the PS-ADPCM decoding follow
|
|
-- vgmstream r2117 (ISC, licenses/vgmstream-LICENSE.txt), whose output it matches bit for bit. Supports: "VAGp" mono
|
|
-- (versions 0, 2, 3, 4, 6, 0x20; the name field as tags.title; loop points from the frame flags, as dec.loop and
|
|
-- meta.source.loop = { start, stop } in frames, stop exclusive), "VAGi" stereo (interleaved blocks), "KVAG" mono /
|
|
-- stereo IMA. The output starts with the blank frame Sony's tools put first (28 silent samples), as vgmstream plays it.
|
|
-- Encode (Audio or Decoder): mono to VAGp, stereo to VAGi (2048-byte interleave), loop points from opts.loop (frame
|
|
-- aligned: 28 samples). Not supported: HEVAG (VAGp versions 0x00020001 / 0x00030000, Vita / PS4), the many game
|
|
-- specific variants vgmstream knows (pGAV, VAG1 / VAG2, Jak, ...).
|
|
--
|
|
-- local dec = Vag.open(Bytes.reader(buf)) -- Types.Decoder (+ loop, variant)
|
|
-- local file = Vag.encode(audio, { loop = { start = 0, stop = audio.frames } })
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local Pcm = require("@formats/codecs/audio/Pcm")
|
|
local Adpcm = require("@formats/codecs/audio/Adpcm")
|
|
local Decoder = require("@formats/codecs/audio/Adpcm/Decoder")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Vag = {
|
|
name = "vag",
|
|
category = "audio",
|
|
extensions = { "vag" },
|
|
mime = { "audio/x-vag" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Pcm", "codecs/audio/Adpcm", "codecs/audio/Adpcm/Decoder" },
|
|
caps = { decode = true, encode = true, streaming = true, lossy = true, loops = true, channels = 2 },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type Loop = { start: number, stop: number }
|
|
export type DecodeOptions = {
|
|
maxFrames: number?, -- decode(): Err.LIMIT past this (default 2^28)
|
|
}
|
|
export type EncodeOptions = {
|
|
loop: Loop?, -- loop points in frames of the input (stop exclusive), rounded out to 28-sample frames; default: the
|
|
-- source's meta.source.loop, else none
|
|
name: string?, -- the 16-byte name field (default: tags.title)
|
|
interleave: number?, -- stereo: bytes per channel block (default 2048, a multiple of 16)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
local HEVAG = { [0x00020001] = true, [0x00030000] = true }
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Vag.sniff(r: Bytes.Reader): number
|
|
local m = r:peek(4)
|
|
return if m == "VAGp" or m == "VAGi" or m == "KVAG" then 1 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function Vag.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.Err?)
|
|
local dec, err = Vag.open(r)
|
|
if not dec then
|
|
return nil, err
|
|
end
|
|
local d = dec :: any
|
|
local limit = opts and opts.maxFrames or 2 ^ 28
|
|
if d.frames > limit or d.frames * d.channels > 2 ^ 28 then
|
|
return nil, Err.new(Err.LIMIT, string.format("%d frames is over the limit", d.frames))
|
|
end
|
|
return Pcm.drain(d, { format = "vag", variant = d.variant, codec = d.codec, loop = d.loop }, limit)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Vag.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
local o: EncodeOptions = opts or {}
|
|
local src, serr = Pcm.source(value)
|
|
if not src then
|
|
return nil, serr
|
|
end
|
|
local ch, rate = src.channels, src.sampleRate
|
|
if ch > 2 then
|
|
return nil, Err.new(Err.ARG, "VAG holds mono or stereo")
|
|
end
|
|
if rate ~= math.floor(rate) or rate < 1 or rate >= 2 ^ 32 then
|
|
return nil, Err.new(Err.ARG, "the sample rate must be a positive integer")
|
|
end
|
|
local il = o.interleave or 2048
|
|
if type(il) ~= "number" or il < 16 or il % 16 ~= 0 then
|
|
return nil, Err.new(Err.ARG, "interleave must be a multiple of 16")
|
|
end
|
|
local loop = o.loop
|
|
local v = value :: any
|
|
if loop == nil and type(v.meta) == "table" and type(v.meta.source) == "table" and type(v.meta.source.loop) == "table" then
|
|
loop = v.meta.source.loop
|
|
end
|
|
local a, cerr = Pcm.collect(src)
|
|
if not a then
|
|
return nil, cerr
|
|
end
|
|
local n = a.frames
|
|
local nf = (n + 27) // 28 -- data frames (after the blank one)
|
|
local ls, le = -1, -1
|
|
if loop then
|
|
if type(loop.start) ~= "number" or type(loop.stop) ~= "number" or loop.start < 0 or loop.stop > n then
|
|
return nil, Err.new(Err.ARG, "loop must be { start, stop } inside the audio")
|
|
end
|
|
ls, le = loop.start // 28, (loop.stop + 27) // 28 - 1
|
|
if le <= ls then
|
|
return nil, Err.new(Err.ARG, "the loop must span two 28-sample frames")
|
|
end
|
|
end
|
|
local chBytes = (nf + 1) * 16
|
|
local stride = if ch == 1 then chBytes else (chBytes + il - 1) // il * il
|
|
local start = if ch == 1 then 0x30 else 0x800
|
|
local out = buffer.create(start + stride * ch)
|
|
local title = o.name or (a.tags and a.tags.title) or ""
|
|
buffer.writestring(out, 0, if ch == 1 then "VAGp" else "VAGi")
|
|
buffer.writeu32(out, 4, bit32.byteswap(0x20))
|
|
if ch == 2 then
|
|
buffer.writeu32(out, 8, il)
|
|
end
|
|
buffer.writeu32(out, 0x0C, bit32.byteswap(if ch == 1 then chBytes else stride))
|
|
buffer.writeu32(out, 0x10, bit32.byteswap(rate))
|
|
buffer.writestring(out, 0x20, string.sub(title, 1, 16))
|
|
local st = buffer.create(8 * ch)
|
|
local plane = buffer.create(chBytes)
|
|
for c = 0, ch - 1 do
|
|
buffer.fill(plane, 0, 0)
|
|
Adpcm.psxEncode(a.samples, c, ch, n, st, c * 8, plane, 16, 16)
|
|
for k = 0, nf - 1 do
|
|
local flag = if k == ls then 6 elseif k == le then 3 elseif k > ls and k < le then 2 else 0
|
|
if not loop and k == nf - 1 then
|
|
flag = 1
|
|
end
|
|
buffer.writeu8(plane, 16 + k * 16 + 1, flag)
|
|
end
|
|
if ch == 1 then
|
|
buffer.copy(out, start, plane, 0, chBytes)
|
|
else
|
|
for b = 0, stride // il - 1 do
|
|
local take = math.clamp(chBytes - b * il, 0, il)
|
|
if take > 0 then
|
|
buffer.copy(out, start + (b * ch + c) * il, plane, b * il, take)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ open
|
|
-- the loop from the PS-ADPCM frame flags of channel 0 (vgmstream's reading: the first start flag 6, the first end
|
|
-- flag 3), or nil
|
|
local function findLoop(buf: buffer, p: number, frames: number, il: number, ch: number): Loop?
|
|
local ls, le = -1, -1
|
|
for k = 0, frames - 1 do
|
|
local off = p + (k * 16) // il * il * ch + (k * 16) % il
|
|
local flag = bit32.band(buffer.readu8(buf, off + 1), 15)
|
|
if flag == 6 and ls < 0 then
|
|
ls = k * 28
|
|
elseif flag == 3 and le < 0 then
|
|
le = k * 28 + 28
|
|
if ls >= 0 then
|
|
break
|
|
end
|
|
end
|
|
end
|
|
return if ls >= 0 and le > ls then { start = ls, stop = le } else nil
|
|
end
|
|
|
|
function Vag.open(r: Bytes.Reader, _opts: any?): (Types.Decoder?, Err.Err?)
|
|
if type(r) ~= "table" or type(r.buf) ~= "buffer" then
|
|
return nil, Err.new(Err.ARG, "open: expected a Reader")
|
|
end
|
|
local h = r:sub(0)
|
|
local magic = h:string(4)
|
|
local total = r.limit - r.base
|
|
if magic == "KVAG" then
|
|
local size, rate, stereo = h:u32le(), h:u32le(), h:u16le()
|
|
if h.err then
|
|
return nil, h.err
|
|
end
|
|
if rate < 1 or stereo > 1 then
|
|
return nil, Err.new(Err.CORRUPT, "bad KVAG header")
|
|
end
|
|
local dec: any = { sampleRate = rate, channels = stereo + 1, tags = {}, variant = "KVAG", codec = "ima" }
|
|
local d, err = Decoder.nibbles(dec, { buf = r.buf, offset = r.base + 14, size = math.min(size, total - 14), hi = true })
|
|
if d then
|
|
d.bitrate = rate * 4 * (stereo + 1)
|
|
end
|
|
return d, err
|
|
end
|
|
if magic ~= "VAGp" and magic ~= "VAGi" then
|
|
return nil, Err.new(Err.CORRUPT, "not a VAG file")
|
|
end
|
|
local version, il = h:u32be(), h:u32le()
|
|
local chSize, rate = h:u32be(), h:u32be()
|
|
local name = r:sub(0x20, 16):cstring(16)
|
|
if h.err then
|
|
return nil, h.err
|
|
end
|
|
if HEVAG[version] then
|
|
return nil, Err.new(Err.UNSUPPORTED, "HEVAG (Vita / PS4 VAG)")
|
|
end
|
|
if rate < 1 or rate >= 2 ^ 31 then
|
|
return nil, Err.new(Err.CORRUPT, "bad sample rate " .. rate)
|
|
end
|
|
local ch, start = 1, 0x30
|
|
if magic == "VAGi" then
|
|
ch, start = 2, 0x800
|
|
if il < 16 or il % 16 ~= 0 then
|
|
return nil, Err.new(Err.CORRUPT, "bad interleave " .. il)
|
|
end
|
|
else
|
|
il = 16
|
|
end
|
|
if total < start then
|
|
return nil, Err.new(Err.TRUNCATED, "the header runs past the end")
|
|
end
|
|
local size = math.min(chSize * ch, total - start)
|
|
local p = r.base + start
|
|
local frames = math.min(chSize // 16, (size // (il * ch)) * (il // 16) + math.clamp(size % (il * ch) - (ch - 1) * il, 0, il) // 16)
|
|
local loop = if magic == "VAGp" then findLoop(r.buf, p, frames, il, ch) else nil -- vgmstream: VAGi never loops
|
|
local tags = {}
|
|
if name ~= "" then
|
|
tags.title = name
|
|
end
|
|
local dec: any = { sampleRate = rate, channels = ch, tags = tags, variant = magic, codec = "psx", loop = loop }
|
|
local d, err = Decoder.interleaved(dec, { kind = "psx", buf = r.buf, offset = p, size = size, interleave = il, frames = frames * 28 })
|
|
if d then
|
|
d.bitrate = rate * 16 * 8 * ch // 28
|
|
end
|
|
return d, err
|
|
end
|
|
|
|
return Vag
|