rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,126 @@
|
||||
--!strict
|
||||
-- codecs/audio/Tta: the TTA1 (True Audio) lossless codec. A codec (layer 3). Written from the TTA1 format description
|
||||
-- (the 22-byte header with its CRC-32, the seek table of frame sizes with its CRC-32, frames of 256 / 245 seconds);
|
||||
-- no libtta / ffmpeg code (those are LGPL). The frame decoder is codecs/audio/Tta/Decode. Supports format 1 (plain
|
||||
-- PCM), 8 / 16 / 24 bits, any channel count, the header / seek table / frame CRCs. Not supported: format 2
|
||||
-- (password-protected: UNSUPPORTED), TTA2.
|
||||
--
|
||||
-- local info, err = Tta.info(buf, at, limit) -- the header at `at` (after any ID3v2 tag) + the frame table
|
||||
-- local dec = Tta.decoder(info)
|
||||
-- local n, err = dec:frame(buf, i) -- frame i (1-based) -> dec.pcm (interleaved integers)
|
||||
--
|
||||
-- Never throws: bad data returns nil, Err.
|
||||
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, -- per channel
|
||||
frameLength: number, -- samples per frame (256 * rate / 245)
|
||||
frames: number,
|
||||
offsets: { number }, -- each frame's byte offset
|
||||
sizes: { number },
|
||||
}
|
||||
|
||||
-- info(buf, at, limit) -> Info | nil, Err: the TTA1 header at byte `at`
|
||||
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
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decoder
|
||||
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
|
||||
|
||||
-- decoder(info, limit) -> Decoder (limit: the end of the file data)
|
||||
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
|
||||
Reference in new issue
Block a user