codecs/audio: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
6dcce481bb
69 files changed
+523
-1604
No files matched your search
@@ -1,26 +1,6 @@
|
||||
--!strict
|
||||
-- codecs/audio/Adpcm/Decoder: the streaming Decoder (Types.Decoder) over an ADPCM stream cut into packets, for the
|
||||
-- formats whose state carries from packet to packet (ADX, VAG, BRSTM, raw IMA): codecs/audio/Stream.packets plus
|
||||
-- exact seeking. The codec state (one buffer the packet decoder advances) is snapshotted at every packet start it
|
||||
-- passes; a seek restores the snapshot, else the format's own state for that packet (BRSTM's ADPC history), else
|
||||
-- decodes forward from the nearest earlier snapshot. New in rosetta.
|
||||
--
|
||||
-- local dec, err = Decoder.packets(dec, { -- dec: the Decoder table (sampleRate, channels, frames, ...)
|
||||
-- count = n, packetFrames = 4096, -- packets of packetFrames frames (the last may be shorter)
|
||||
-- frames = total, -- output frames
|
||||
-- state = st, -- the codec state buffer decode advances
|
||||
-- decode = function(i, out) return frames end, -- packet i (1-based) as interleaved f32 into out from 0
|
||||
-- restore = function(i) return false end, -- optional: set st for packet i, true if it could
|
||||
-- })
|
||||
--
|
||||
-- The layouts the formats share, each returning the Decoder (dec gets read / seek / position) | nil, Err:
|
||||
-- Decoder.interleaved(dec, spec) psx / adx / dsp frames (or s16 / s8 PCM), channels interleaved every
|
||||
-- spec.interleave bytes (VAG, ADX, BRSTM / BCSTM / BFSTM, raw PS-ADPCM)
|
||||
-- Decoder.nibbles(dec, spec) an IMA nibble stream, channels interleaved by nibble (raw IMA, KVAG)
|
||||
-- Decoder.blocks(dec, spec) self-contained blocks: "ms" / "ima_wav" (blockAlign bytes), "ima_qt" (34 bytes
|
||||
-- per channel), raw or inside AIFF-C / CAF
|
||||
--
|
||||
-- Never throws (decode failures end the stream and land in dec.error, see codecs/audio/Stream).
|
||||
-- codecs/audio/Adpcm/Decoder: the streaming Types.Decoder over packetised ADPCM (ADX, VAG, BRSTM, raw IMA) with exact seeking.
|
||||
-- New in rosetta.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Stream = require("@rosetta/codecs/audio/Stream")
|
||||
local Adpcm = require("@rosetta/codecs/audio/Adpcm")
|
||||
@@ -58,7 +38,6 @@ function Decoder.packets(dec: any, spec: Spec): (any, Err.Err?)
|
||||
if snaps[i] then
|
||||
buffer.copy(st, 0, snaps[i])
|
||||
elseif restore and restore(i) then
|
||||
-- the format set the state (BRSTM's seek table)
|
||||
else
|
||||
local j = i - 1
|
||||
while j > 1 and not snaps[j] do
|
||||
@@ -95,11 +74,9 @@ function Decoder.packets(dec: any, spec: Spec): (any, Err.Err?)
|
||||
})
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ layouts
|
||||
local FRAME: { [string]: number } = { psx = 16, adx = 18, dsp = 8, s16be = 2, s16le = 2, s8 = 1 } -- bytes per frame
|
||||
local SPF: { [string]: number } = { psx = 28, adx = 32, dsp = 14, s16be = 1, s16le = 1, s8 = 1 } -- samples per frame
|
||||
|
||||
-- PCM samples of one channel into every ds-th float of dst from float d
|
||||
local function pcm(kind: string, src: buffer, p: number, n: number, dst: buffer, d: number, ds: number)
|
||||
if kind == "s8" then
|
||||
for k = 0, n - 1 do
|
||||
@@ -118,22 +95,21 @@ local function pcm(kind: string, src: buffer, p: number, n: number, dst: buffer,
|
||||
end
|
||||
|
||||
export type InterleavedSpec = {
|
||||
kind: "psx" | "adx" | "dsp" | "s16be" | "s16le" | "s8", -- ADPCM frames, or PCM samples laid out the same way
|
||||
kind: "psx" | "adx" | "dsp" | "s16be" | "s16le" | "s8",
|
||||
buf: buffer,
|
||||
offset: number, -- the first row
|
||||
size: number, -- data bytes (rows past it are cut; a truncated file decodes its whole frames)
|
||||
interleave: number, -- bytes per channel per row (a multiple of the frame size; mono: any multiple)
|
||||
offset: number,
|
||||
size: number,
|
||||
interleave: number,
|
||||
lastInterleave: number?, -- bytes per channel in the last row (BRSTM's padded last block), default interleave
|
||||
frames: number?, -- frames to output (default: what the data holds)
|
||||
c1: number?, -- adx: the predictor (Adpcm.adxCoefs)
|
||||
frames: number?,
|
||||
c1: number?,
|
||||
c2: number?,
|
||||
v3: boolean?, -- adx: version 3 rounding
|
||||
v3: boolean?,
|
||||
coefs: buffer?, -- dsp: 16 i32 per channel
|
||||
hist: { number }?, -- initial history, h1 / h2 per channel (default 0)
|
||||
hist: { number }?,
|
||||
restore: ((row: number, st: buffer) -> boolean)?, -- the exact history at the start of a row (BRSTM ADPC)
|
||||
}
|
||||
|
||||
-- interleaved(dec, spec) -> Decoder | nil, Err
|
||||
function Decoder.interleaved(dec: any, spec: InterleavedSpec): (any, Err.Err?)
|
||||
local ch = dec.channels
|
||||
local kind = spec.kind
|
||||
@@ -145,9 +121,8 @@ function Decoder.interleaved(dec: any, spec: InterleavedSpec): (any, Err.Err?)
|
||||
end
|
||||
local buf, base, size = spec.buf, spec.offset, spec.size
|
||||
local rowBytes = il * ch
|
||||
-- rows: the full ones, then the last (which may be shorter, and may be cut by a truncated file)
|
||||
local rows = math.max(1, math.ceil(size / rowBytes))
|
||||
local function rowFrames(r: number): number -- the frames row r (0-based) holds whole in every channel
|
||||
local function rowFrames(r: number): number
|
||||
local width = if r == rows - 1 then last else il
|
||||
-- channel c sits at c * width in the row: the last channel is the one a cut file shortens
|
||||
local per = math.clamp(size - r * rowBytes - (ch - 1) * width, 0, width)
|
||||
@@ -211,12 +186,12 @@ end
|
||||
export type NibbleSpec = {
|
||||
buf: buffer,
|
||||
offset: number,
|
||||
size: number, -- bytes
|
||||
size: number,
|
||||
hi: boolean?, -- the high nibble first (DVI / KVAG); default low first
|
||||
frames: number?, -- default: every whole frame of nibbles
|
||||
frames: number?,
|
||||
}
|
||||
|
||||
-- nibbles(dec, spec): an IMA stream starting from predictor 0, step index 0, channel c in nibbles c, c + ch, ...
|
||||
-- IMA from predictor 0, step index 0; channel c in nibbles c, c + ch, ...
|
||||
function Decoder.nibbles(dec: any, spec: NibbleSpec): (any, Err.Err?)
|
||||
local ch = dec.channels
|
||||
local buf, base = spec.buf, spec.offset
|
||||
@@ -246,12 +221,11 @@ export type BlockSpec = {
|
||||
buf: buffer,
|
||||
offset: number,
|
||||
size: number,
|
||||
blockAlign: number?, -- ms / ima_wav: bytes per block
|
||||
frames: number?, -- default: what the blocks hold
|
||||
blockAlign: number?,
|
||||
frames: number?,
|
||||
coefs: { number }?, -- ms: coefficient pairs, flat (default the 7 standard pairs)
|
||||
}
|
||||
|
||||
-- blockFrames(kind, blockAlign, ch) -> frames per block, or nil when the block cannot hold the headers
|
||||
function Decoder.blockFrames(kind: string, ba: number, ch: number): number?
|
||||
if kind == "ima_qt" then
|
||||
return 64
|
||||
@@ -261,7 +235,6 @@ function Decoder.blockFrames(kind: string, ba: number, ch: number): number?
|
||||
return if ba >= 4 * ch then (ba - 4 * ch) * 2 // ch + 1 else nil
|
||||
end
|
||||
|
||||
-- blocks(dec, spec): self-contained ADPCM blocks (no state across blocks); a truncated last block decodes what it has
|
||||
function Decoder.blocks(dec: any, spec: BlockSpec): (any, Err.Err?)
|
||||
local ch = dec.channels
|
||||
local kind = spec.kind
|
||||
@@ -278,7 +251,6 @@ function Decoder.blocks(dec: any, spec: BlockSpec): (any, Err.Err?)
|
||||
local rest = size - full * ba
|
||||
local total = full * spb
|
||||
if kind ~= "ima_qt" and rest > 0 then
|
||||
-- a partial last block: the frames it holds whole
|
||||
if kind == "ms" then
|
||||
total += if rest >= 7 * ch then math.min(spb, 2 + ((rest - 7 * ch) * 2) // ch) else 0
|
||||
elseif rest >= 4 * ch then
|
||||
|
||||
Reference in new issue
Block a user