234 lines
6.0 KiB
Luau
234 lines
6.0 KiB
Luau
--!strict
|
|
--!optimize 2
|
|
-- codecs/audio/Alac: Apple Lossless (ALAC) decoder and encoder.
|
|
-- Written from the ALAC format as Apple published it; no third-party code. ALAC stores C L R ... for 3+ channels; the
|
|
-- API uses WAV order.
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Decode = require("@rosetta/codecs/audio/Alac/Decode")
|
|
local Encode = require("@rosetta/codecs/audio/Alac/Encode")
|
|
|
|
local Alac = {}
|
|
|
|
export type Config = {
|
|
frameLength: number,
|
|
compatibleVersion: number,
|
|
bitDepth: number,
|
|
pb: number,
|
|
mb: number,
|
|
kb: number,
|
|
channels: number,
|
|
maxRun: number,
|
|
maxFrameBytes: number,
|
|
avgBitRate: number,
|
|
sampleRate: number,
|
|
}
|
|
|
|
local TO_WAV: { { number } } = {
|
|
{ 1 },
|
|
{ 1, 2 },
|
|
{ 3, 1, 2 },
|
|
{ 3, 1, 2, 4 },
|
|
{ 3, 1, 2, 4, 5 },
|
|
{ 3, 1, 2, 5, 6, 4 },
|
|
{ 3, 1, 2, 5, 6, 7, 4 },
|
|
{ 3, 7, 8, 1, 2, 5, 6, 4 },
|
|
}
|
|
local ELEMENTS: { { number } } = {
|
|
{ 1 },
|
|
{ 2 },
|
|
{ 1, 2 },
|
|
{ 1, 2, 1 },
|
|
{ 1, 2, 2 },
|
|
{ 1, 2, 2, 1 },
|
|
{ 1, 2, 2, 1, 1 },
|
|
{ 1, 2, 2, 2, 1 },
|
|
}
|
|
Alac.TO_WAV = TO_WAV
|
|
|
|
function Alac.config(buf: buffer, offset: number?, length: number?): (Config?, Err.Err?)
|
|
local r = Bytes.reader(buf, offset, length)
|
|
-- a QuickTime 'wave' payload may hold frma + alac atoms ahead of the cookie
|
|
for _ = 1, 2 do
|
|
local typ = r:peek(4, 4)
|
|
if typ == "frma" then
|
|
r:skip(12)
|
|
elseif typ == "alac" then
|
|
r:skip(12)
|
|
end
|
|
end
|
|
if r:remaining() < 24 then
|
|
return nil, Err.new(Err.TRUNCATED, "ALAC configuration is shorter than 24 bytes")
|
|
end
|
|
local c: Config = {
|
|
frameLength = r:u32be(),
|
|
compatibleVersion = r:u8(),
|
|
bitDepth = r:u8(),
|
|
pb = r:u8(),
|
|
mb = r:u8(),
|
|
kb = r:u8(),
|
|
channels = r:u8(),
|
|
maxRun = r:u16be(),
|
|
maxFrameBytes = r:u32be(),
|
|
avgBitRate = r:u32be(),
|
|
sampleRate = r:u32be(),
|
|
}
|
|
if c.compatibleVersion ~= 0 then
|
|
return nil, Err.new(Err.UNSUPPORTED, "ALAC version " .. c.compatibleVersion)
|
|
end
|
|
if c.frameLength < 1 or c.frameLength > 65536 then
|
|
return nil, Err.new(Err.CORRUPT, "bad ALAC frame length " .. c.frameLength)
|
|
end
|
|
if c.bitDepth ~= 16 and c.bitDepth ~= 20 and c.bitDepth ~= 24 and c.bitDepth ~= 32 then
|
|
return nil, Err.new(Err.UNSUPPORTED, "ALAC bit depth " .. c.bitDepth)
|
|
end
|
|
if c.channels < 1 or c.channels > 8 then
|
|
return nil, Err.new(Err.UNSUPPORTED, "ALAC with " .. c.channels .. " channels")
|
|
end
|
|
if c.kb < 1 or c.kb > 24 then
|
|
return nil, Err.new(Err.CORRUPT, "bad ALAC rice limit " .. c.kb)
|
|
end
|
|
return c, nil
|
|
end
|
|
|
|
function Alac.cookie(c: Config): buffer
|
|
local w = Bytes.writer(24)
|
|
w:u32be(c.frameLength)
|
|
w:u8(0)
|
|
w:u8(c.bitDepth)
|
|
w:u8(c.pb)
|
|
w:u8(c.mb)
|
|
w:u8(c.kb)
|
|
w:u8(c.channels)
|
|
w:u16be(c.maxRun)
|
|
w:u32be(c.maxFrameBytes)
|
|
w:u32be(c.avgBitRate)
|
|
w:u32be(c.sampleRate)
|
|
return w:finish()
|
|
end
|
|
|
|
export type Decoder = {
|
|
config: Config,
|
|
pcm: { number },
|
|
decode: (self: Decoder, buf: buffer, offset: number?, length: number?) -> (number?, Err.Err?),
|
|
state: Decode.State,
|
|
}
|
|
|
|
local function decode(self: Decoder, buf: buffer, offset: number?, length: number?): (number?, Err.Err?)
|
|
local o = offset or 0
|
|
local n = length or (buffer.len(buf) - o)
|
|
if o < 0 or n < 0 or o + n > buffer.len(buf) then
|
|
return nil, Err.new(Err.ARG, "packet range out of the buffer")
|
|
end
|
|
local frames, msg = Decode.frame(self.state, buf, o, n)
|
|
if not frames then
|
|
return nil, Err.new(Err.CORRUPT, msg or "bad packet", o)
|
|
end
|
|
local ch = self.config.channels
|
|
local src, dst = self.state.out, self.pcm
|
|
if ch <= 2 then
|
|
for i = 1, frames * ch do
|
|
dst[i] = src[i]
|
|
end
|
|
else
|
|
local map = TO_WAV[ch]
|
|
for f = 0, frames - 1 do
|
|
local base = f * ch
|
|
for k = 1, ch do
|
|
dst[base + map[k]] = src[base + k]
|
|
end
|
|
end
|
|
end
|
|
return frames, nil
|
|
end
|
|
|
|
function Alac.decoder(cfg: Config): Decoder
|
|
return {
|
|
config = cfg,
|
|
pcm = table.create(cfg.frameLength * cfg.channels, 0),
|
|
decode = decode,
|
|
state = Decode.state(cfg),
|
|
}
|
|
end
|
|
|
|
export type Encoder = {
|
|
config: Config,
|
|
encode: (self: Encoder, samples: { number }, first: number, frames: number) -> (buffer?, Err.Err?),
|
|
state: Encode.State,
|
|
bytes: number,
|
|
frames: number,
|
|
}
|
|
|
|
local function encode(self: Encoder, samples: { number }, first: number, frames: number): (buffer?, Err.Err?)
|
|
local cfg = self.config
|
|
if type(samples) ~= "table" or frames < 1 or frames > cfg.frameLength or frames % 1 ~= 0 then
|
|
return nil, Err.new(Err.ARG, "encode: 1 .. " .. cfg.frameLength .. " frames of interleaved integer samples")
|
|
end
|
|
if first < 1 or first + frames * cfg.channels - 1 > #samples then
|
|
return nil, Err.new(Err.ARG, "encode: the samples end early")
|
|
end
|
|
local ch = cfg.channels
|
|
local pkt = Encode.frame(self.state, samples, first, frames, TO_WAV[ch], ELEMENTS[ch])
|
|
local n = buffer.len(pkt)
|
|
self.bytes += n
|
|
self.frames += frames
|
|
if n > cfg.maxFrameBytes then
|
|
cfg.maxFrameBytes = n
|
|
end
|
|
if self.frames > 0 then
|
|
cfg.avgBitRate = math.floor(self.bytes * 8 * cfg.sampleRate / self.frames)
|
|
end
|
|
return pkt, nil
|
|
end
|
|
|
|
export type EncoderOptions = {
|
|
sampleRate: number,
|
|
channels: number,
|
|
bitDepth: number?,
|
|
frameLength: number?,
|
|
}
|
|
|
|
function Alac.encoder(opts: EncoderOptions): (Encoder?, Err.Err?)
|
|
if type(opts) ~= "table" then
|
|
return nil, Err.new(Err.ARG, "encoder: expected options")
|
|
end
|
|
local depth = opts.bitDepth or 16
|
|
local ch = opts.channels
|
|
if depth ~= 16 and depth ~= 20 and depth ~= 24 then
|
|
return nil, Err.new(Err.ARG, "encoder: bitDepth 16, 20 or 24")
|
|
end
|
|
if type(ch) ~= "number" or ch < 1 or ch > 8 or ch % 1 ~= 0 then
|
|
return nil, Err.new(Err.ARG, "encoder: 1 .. 8 channels")
|
|
end
|
|
local sr = opts.sampleRate
|
|
if type(sr) ~= "number" or sr < 1 or sr % 1 ~= 0 then
|
|
return nil, Err.new(Err.ARG, "encoder: bad sampleRate")
|
|
end
|
|
local fl = opts.frameLength or 4096
|
|
if fl < 16 or fl > 16384 or fl % 1 ~= 0 then
|
|
return nil, Err.new(Err.ARG, "encoder: frameLength 16 .. 16384")
|
|
end
|
|
local cfg: Config = {
|
|
frameLength = fl,
|
|
compatibleVersion = 0,
|
|
bitDepth = depth,
|
|
pb = 40,
|
|
mb = 10,
|
|
kb = 14,
|
|
channels = ch,
|
|
maxRun = 255,
|
|
maxFrameBytes = 0,
|
|
avgBitRate = 0,
|
|
sampleRate = sr,
|
|
}
|
|
return {
|
|
config = cfg,
|
|
encode = encode,
|
|
state = Encode.state(cfg),
|
|
bytes = 0,
|
|
frames = 0,
|
|
}, nil
|
|
end
|
|
|
|
return Alac
|