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,155 @@
|
||||
--!strict
|
||||
-- audio/adpcm: headerless ADPCM streams (.adpcm / .ima / .vox-style dumps), over codecs/audio/Adpcm. New in
|
||||
-- rosetta. A raw stream has no header, so nothing identifies it: sniff is always 0, F.detect only finds it by
|
||||
-- extension, and the layout comes from the options (codec, sampleRate, channels, blockAlign). vgmstream plays such
|
||||
-- files only through a .txth description and ffmpeg not at all (its adpcm_* codecs need a container), so the codecs
|
||||
-- here are the layouts those containers use:
|
||||
-- "ima" IMA nibbles from predictor 0 / step index 0, channels interleaved by nibble, the high nibble first
|
||||
-- (DVI order, as KVAG / adpcm_ima_ssi); lowFirst = true for the other order
|
||||
-- "ima_wav" Microsoft IMA blocks (the WAV / AVI data chunk), blockAlign bytes each (default 1024)
|
||||
-- "ms" Microsoft ADPCM blocks with the 7 standard coefficient pairs (default blockAlign 1024)
|
||||
-- "ima_qt" Apple ima4 packets (34 bytes per channel, 64 frames), as in AIFF-C / CAF / QuickTime
|
||||
-- "psx" Sony PS-ADPCM frames (16 bytes, 28 samples), channels interleaved every `interleave` bytes (default 16)
|
||||
-- Encode: "ima", "ima_wav" and "ms" (from an Audio or a Decoder). Not supported: raw DSP / ADX (their coefficients
|
||||
-- live in a header: see audio/brstm and audio/adx).
|
||||
--
|
||||
-- local a = Adpcm.decode(Bytes.reader(buf), { codec = "ms", sampleRate = 22050, channels = 2, blockAlign = 512 })
|
||||
-- local raw = Adpcm.encode(a, { codec = "ima" })
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Pcm = require("@rosetta/codecs/audio/Pcm")
|
||||
local Adpcm = require("@rosetta/codecs/audio/Adpcm")
|
||||
local Decoder = require("@rosetta/codecs/audio/Adpcm/Decoder")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Raw = {
|
||||
name = "adpcm",
|
||||
category = "audio",
|
||||
extensions = { "adpcm", "ima" },
|
||||
mime = { "audio/x-adpcm" },
|
||||
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, headerless = true, codecs = { "ima", "ima_wav", "ms", "ima_qt", "psx" } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type Codec = "ima" | "ima_wav" | "ms" | "ima_qt" | "psx"
|
||||
export type Options = {
|
||||
codec: Codec?, -- default "ima"
|
||||
sampleRate: number?, -- default 22050 (decode)
|
||||
channels: number?, -- default 1 (decode)
|
||||
blockAlign: number?, -- ima_wav / ms: bytes per block (default 1024 when decoding; 256 * channels when encoding)
|
||||
lowFirst: boolean?, -- ima: the low nibble first
|
||||
interleave: number?, -- psx: bytes per channel before the next channel (default 16)
|
||||
maxFrames: number?, -- decode(): Err.LIMIT past this (default 2^28)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local CODECS = { ima = true, ima_wav = true, ms = true, ima_qt = true, psx = true }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Raw.sniff(_r: Bytes.Reader): number
|
||||
return 0 -- no header: found by extension or opts.format only
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Raw.decode(r: Bytes.Reader, opts: Options?): (Types.Audio?, Err.Err?)
|
||||
local dec, err = Raw.open(r, opts)
|
||||
if not dec then
|
||||
return nil, err
|
||||
end
|
||||
local limit = opts and opts.maxFrames or 2 ^ 28
|
||||
if dec.frames > limit or dec.frames * dec.channels > 2 ^ 28 then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d frames is over the limit", dec.frames))
|
||||
end
|
||||
return Pcm.drain(dec, { format = "adpcm", codec = (dec :: any).codec }, limit)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Raw.encode(value: any, opts: Options?): (buffer?, Err.Err?)
|
||||
local o: Options = opts or {}
|
||||
local codec = o.codec or "ima"
|
||||
if codec ~= "ima" and codec ~= "ima_wav" and codec ~= "ms" then
|
||||
return nil, Err.new(Err.ARG, "encode: codec must be ima, ima_wav or ms")
|
||||
end
|
||||
local src, serr = Pcm.source(value)
|
||||
if not src then
|
||||
return nil, serr
|
||||
end
|
||||
local a, cerr = Pcm.collect(src)
|
||||
if not a then
|
||||
return nil, cerr
|
||||
end
|
||||
local ch, n, x = a.channels, a.frames, a.samples
|
||||
if codec == "ima" then
|
||||
local out = buffer.create((n * ch + 1) // 2)
|
||||
local st = buffer.create(8 * ch)
|
||||
for c = 0, ch - 1 do
|
||||
Adpcm.imaEncode(x, c, ch, n, st, c * 8, out, 0, c, ch, not o.lowFirst)
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
local ba = o.blockAlign or 256 * ch
|
||||
local spb = Decoder.blockFrames(codec, ba, ch)
|
||||
if type(ba) ~= "number" or ba ~= math.floor(ba) or ba > 65535 or not spb or spb < 2 then
|
||||
return nil, Err.new(Err.ARG, "bad blockAlign " .. tostring(ba))
|
||||
end
|
||||
if codec == "ima_wav" and (ba % (4 * ch) ~= 0) then
|
||||
return nil, Err.new(Err.ARG, "ima_wav: blockAlign must be a multiple of 4 * channels")
|
||||
end
|
||||
local blocks = (n + spb - 1) // spb
|
||||
local out = buffer.create(blocks * ba)
|
||||
local st = buffer.create(8 * ch)
|
||||
for b = 0, blocks - 1 do
|
||||
local f0 = b * spb
|
||||
if codec == "ms" then
|
||||
Adpcm.msEncode(x, f0, n - f0, ch, spb, out, b * ba)
|
||||
else
|
||||
Adpcm.imaWavEncode(x, f0, n - f0, ch, spb, st, out, b * ba)
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
function Raw.open(r: Bytes.Reader, opts: Options?): (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 o: Options = opts or {}
|
||||
local codec = o.codec or "ima"
|
||||
local rate, ch = o.sampleRate or 22050, o.channels or 1
|
||||
if not CODECS[codec] then
|
||||
return nil, Err.new(Err.ARG, "unknown codec " .. tostring(codec))
|
||||
end
|
||||
if type(rate) ~= "number" or rate <= 0 or rate ~= rate or type(ch) ~= "number" or ch < 1 or ch > 255 or ch ~= math.floor(ch) then
|
||||
return nil, Err.new(Err.ARG, "bad sampleRate or channels")
|
||||
end
|
||||
local size = r.limit - r.base
|
||||
local dec: any = { sampleRate = rate, channels = ch, tags = {}, codec = codec, frames = 0, duration = 0 }
|
||||
local d, err
|
||||
if codec == "ima" then
|
||||
d, err = Decoder.nibbles(dec, { buf = r.buf, offset = r.base, size = size, hi = not o.lowFirst })
|
||||
elseif codec == "psx" then
|
||||
local il = o.interleave or 16
|
||||
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
|
||||
d, err = Decoder.interleaved(dec, { kind = "psx", buf = r.buf, offset = r.base, size = size, interleave = if ch == 1 then 16 else il })
|
||||
else
|
||||
local ba = o.blockAlign or 1024
|
||||
if type(ba) ~= "number" or ba < 1 or ba ~= math.floor(ba) then
|
||||
return nil, Err.new(Err.ARG, "bad blockAlign")
|
||||
end
|
||||
d, err = Decoder.blocks(dec, { kind = codec :: any, buf = r.buf, offset = r.base, size = size, blockAlign = ba })
|
||||
end
|
||||
if not d then
|
||||
return nil, err
|
||||
end
|
||||
d.bitrate = if d.duration > 0 then math.floor(size * 8 / d.duration) else 0
|
||||
return d, nil
|
||||
end
|
||||
|
||||
return Raw
|
||||
Reference in new issue
Block a user