Files
rosetta/src/Rosetta/codecs/audio/Tta/init.luau
T

116 lines
3.5 KiB
Luau

--!strict
-- codecs/audio/Tta: the TTA1 (True Audio) lossless codec.
-- From the TTA1 format description (22-byte header and seek table with CRC-32s, frames of 256 / 245 seconds); no libtta /
-- ffmpeg code (LGPL). Format 2 (password) is UNSUPPORTED.
local Err = require("@rosetta/core/Err")
local Crc32 = require("@rosetta/core/Crc32")
local Decode = require("@rosetta/codecs/audio/Tta/Decode")
local Tta = {}
export type Info = {
format: number,
channels: number,
bitsPerSample: number,
sampleRate: number,
samples: number,
frameLength: number, -- samples per frame (256 * rate / 245)
frames: number,
offsets: { number },
sizes: { number },
}
function Tta.info(buf: buffer, at: number, limit: number): (Info?, Err.Err?)
if at + 22 > limit then
return nil, Err.new(Err.TRUNCATED, "truncated header")
end
if buffer.readstring(buf, at, 4) ~= "TTA1" then
return nil, Err.new(Err.CORRUPT, "no TTA1 header")
end
if Crc32.of(buf, at, 18) ~= buffer.readu32(buf, at + 18) then
return nil, Err.new(Err.CHECKSUM, "header CRC mismatch", at)
end
local format = buffer.readu16(buf, at + 4)
local info: Info = {
format = format,
channels = buffer.readu16(buf, at + 6),
bitsPerSample = buffer.readu16(buf, at + 8),
sampleRate = buffer.readu32(buf, at + 10),
samples = buffer.readu32(buf, at + 14),
frameLength = 0,
frames = 0,
offsets = {},
sizes = {},
}
if format == 2 then
return nil, Err.new(Err.UNSUPPORTED, "password-protected TTA")
elseif format ~= 1 then
return nil, Err.new(Err.UNSUPPORTED, "TTA format " .. format)
end
local bits = info.bitsPerSample
if bits ~= 8 and bits ~= 16 and bits ~= 24 then
return nil, Err.new(Err.UNSUPPORTED, bits .. "-bit TTA")
end
if info.channels < 1 or info.sampleRate < 1 then
return nil, Err.new(Err.CORRUPT, "bad header")
end
info.frameLength = (256 * info.sampleRate) // 245
if info.frameLength < 1 then
return nil, Err.new(Err.CORRUPT, "bad sample rate")
end
local frames = (info.samples + info.frameLength - 1) // info.frameLength
info.frames = frames
local st = at + 22
if st + frames * 4 + 4 > limit then
return nil, Err.new(Err.TRUNCATED, "truncated seek table")
end
if Crc32.of(buf, st, frames * 4) ~= buffer.readu32(buf, st + frames * 4) then
return nil, Err.new(Err.CHECKSUM, "seek table CRC mismatch", st)
end
local o = st + frames * 4 + 4
for i = 1, frames do
local size = buffer.readu32(buf, st + (i - 1) * 4)
info.offsets[i] = o
info.sizes[i] = size
o += size
end
return info, nil
end
export type Decoder = {
info: Info,
pcm: { number },
limit: number,
frame: (self: Decoder, buf: buffer, i: number) -> (number?, Err.Err?),
state: Decode.State,
}
local function frame(self: Decoder, buf: buffer, i: number): (number?, Err.Err?)
local info = self.info
if i < 1 or i > info.frames then
return nil, Err.new(Err.ARG, "no frame " .. i)
end
local o, size = info.offsets[i], info.sizes[i]
if o + size > self.limit then
return nil, Err.new(Err.TRUNCATED, "frame " .. i .. " runs past the end", o)
end
local n = if i < info.frames then info.frameLength else info.samples - (info.frames - 1) * info.frameLength
local got, msg = Decode.frame(self.state, buf, o, size, n, self.pcm)
if not got then
return nil, Err.from(msg, o)
end
return got, nil
end
function Tta.decoder(info: Info, limit: number): Decoder
return {
info = info,
pcm = table.create(info.frameLength * info.channels, 0),
limit = limit,
frame = frame,
state = Decode.state(info.channels, info.bitsPerSample // 8),
}
end
return Tta