252 lines
7.6 KiB
Luau
252 lines
7.6 KiB
Luau
--!strict
|
|
-- codecs/Alac: Apple Lossless (ALAC) decoder and encoder, the codec inside m4a / mov / caf. A codec (layer 1).
|
|
-- Written from the ALAC format as Apple published it (ALACSpecificConfig, the element syntax, adaptive Golomb-Rice
|
|
-- residuals, the adaptive FIR predictor); no third-party code. 1 - 8 channels, 16 / 20 / 24 / 32-bit decode, 16 / 20 /
|
|
-- 24-bit encode. Channels come out (and go in) in WAV order (ALAC stores C L R ... for 3+ channels).
|
|
--
|
|
-- local Alac = require("@formats/codecs/Alac")
|
|
-- local cfg, err = Alac.config(cookie) -- the 24-byte ALACSpecificConfig (frma / alac atoms skipped)
|
|
-- local dec = Alac.decoder(cfg)
|
|
-- local frames, err = dec:decode(buf, offset, length) -- one packet -> dec.pcm (interleaved integers, WAV order)
|
|
-- local enc = Alac.encoder({ sampleRate = 44100, channels = 2, bitDepth = 16 })
|
|
-- local packet = enc:encode(samples, first, frames) -- interleaved integers -> one packet (frames <= 4096)
|
|
-- local cookie = Alac.cookie(enc.config) -- after encoding: maxFrameBytes / avgBitRate filled
|
|
--
|
|
-- Never throws: bad data returns nil, Err (CORRUPT / TRUNCATED / UNSUPPORTED), bad arguments Err.ARG.
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Decode = require("@formats/codecs/Alac/Decode")
|
|
local Encode = require("@formats/codecs/Alac/Encode")
|
|
|
|
local Alac = {}
|
|
|
|
export type Config = {
|
|
frameLength: number, -- samples per packet (4096)
|
|
compatibleVersion: number,
|
|
bitDepth: number,
|
|
pb: number, -- 40
|
|
mb: number, -- 10
|
|
kb: number, -- 14
|
|
channels: number,
|
|
maxRun: number, -- 255
|
|
maxFrameBytes: number,
|
|
avgBitRate: number,
|
|
sampleRate: number,
|
|
}
|
|
|
|
-- ALAC channel k (1-based) -> WAV channel (1-based), per channel count
|
|
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 },
|
|
}
|
|
-- the element layout (channels per element) the encoder writes
|
|
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
|
|
|
|
-- config(buf, offset?, length?) -> Config | nil, Err
|
|
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
|
|
|
|
-- cookie(cfg) -> the 24-byte ALACSpecificConfig
|
|
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
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decoder
|
|
export type Decoder = {
|
|
config: Config,
|
|
pcm: { number }, -- the last packet's samples, interleaved, WAV order (integers of config.bitDepth bits)
|
|
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
|
|
|
|
-- decoder(cfg) -> Decoder
|
|
function Alac.decoder(cfg: Config): Decoder
|
|
return {
|
|
config = cfg,
|
|
pcm = table.create(cfg.frameLength * cfg.channels, 0),
|
|
decode = decode,
|
|
state = Decode.state(cfg),
|
|
}
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encoder
|
|
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, -- 1 .. 8
|
|
bitDepth: number?, -- 16 (default), 20 or 24
|
|
frameLength: number?, -- default 4096
|
|
}
|
|
|
|
-- encoder(opts) -> Encoder | nil, Err
|
|
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
|