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,13 +1,9 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Flac/Frame: FLAC frames: header parsing (CRC-8), the sync search, subframes (constant, verbatim, fixed, LPC, wasted bits),
|
||||
-- residuals (Rice / Rice2 partitions, escapes), stereo decorrelation and the CRC-16 check.
|
||||
--
|
||||
-- The input buffer must have at least PAD zero bytes after the data end (`len`): the bit reader reads 32-bit
|
||||
-- words without bounds checks and instead compares its position with the data end once per sample. Nothing here
|
||||
-- throws: bad data gives nil, err.
|
||||
-- Absorbed from flac-luau 7083d0d (same author, no third-party code).
|
||||
-- codecs/audio/Flac/Frame: FLAC frames: header (CRC-8), sync search, subframes, residuals, decorrelation, CRC-16.
|
||||
-- Absorbed from flac-luau 7083d0d (no third-party code). The input needs PAD zero bytes after `len`: the bit reader
|
||||
-- reads 32-bit words unchecked and compares its position with the data end once per sample.
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
local band, bxor, lshift, rshift, countlz, byteswap = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift,
|
||||
bit32.countlz, bit32.byteswap
|
||||
@@ -62,7 +58,7 @@ local RATE = { [1] = 88200, [2] = 176400, [3] = 192000, [4] = 8000, [5] = 16000,
|
||||
local BITS = { [1] = 8, [2] = 12, [4] = 16, [5] = 20, [6] = 24, [7] = 32 }
|
||||
|
||||
export type Header = {
|
||||
offset: number, -- byte offset of the sync code
|
||||
offset: number,
|
||||
size: number, -- header bytes, CRC-8 included
|
||||
blockSize: number,
|
||||
sampleRate: number,
|
||||
@@ -73,8 +69,6 @@ export type Header = {
|
||||
number: number,
|
||||
}
|
||||
|
||||
-- Parse the frame header at pos (data ends at len; the buffer is padded). si gives the STREAMINFO defaults
|
||||
-- (sampleRate, bitsPerSample) for the "from STREAMINFO" codes. Returns nil when it is not a valid header.
|
||||
function Frame.header(b: buffer, pos: number, len: number, si: any): Header?
|
||||
if pos < 0 or pos + 6 > len then
|
||||
return nil
|
||||
@@ -155,7 +149,6 @@ function Frame.header(b: buffer, pos: number, len: number, si: any): Header?
|
||||
if not bs or not bps or bps < 4 or bps > 32 or p + 1 > len then
|
||||
return nil
|
||||
end
|
||||
-- CRC-8 over the header
|
||||
if crc8(b, pos, p) ~= readu8(b, p) then
|
||||
return nil
|
||||
end
|
||||
@@ -172,7 +165,6 @@ function Frame.header(b: buffer, pos: number, len: number, si: any): Header?
|
||||
}
|
||||
end
|
||||
|
||||
-- The first valid header at an offset in [pos, limit)
|
||||
function Frame.find(b: buffer, pos: number, limit: number, len: number, si: any): Header?
|
||||
local lim = math.min(limit, len - 6)
|
||||
if pos < 0 then
|
||||
@@ -193,7 +185,6 @@ function Frame.find(b: buffer, pos: number, limit: number, len: number, si: any)
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Bit reading for the header parts of subframes: n <= 32 bits at bit position p (unsigned)
|
||||
local function bits(b: buffer, p: number, n: number): number
|
||||
if n == 0 then
|
||||
return 0
|
||||
@@ -213,7 +204,6 @@ local function sbits(b: buffer, p: number, n: number): number
|
||||
return v
|
||||
end
|
||||
|
||||
-- Residual coding: fills s[order + 1 .. n] with residuals. Returns the new bit position, or -1.
|
||||
local function residual(b: buffer, p: number, lim: number, s: { number }, n: number, order: number): number
|
||||
local method = bits(b, p, 2)
|
||||
if method > 1 then
|
||||
@@ -258,7 +248,6 @@ local function residual(b: buffer, p: number, lim: number, s: { number }, n: num
|
||||
end
|
||||
end
|
||||
elseif k <= 24 then
|
||||
-- the hot loop: unary quotient + k-bit remainder, zigzag
|
||||
local pk = POW2[k + 1]
|
||||
local rk = 32 - k
|
||||
for j = i, stop do
|
||||
@@ -280,7 +269,6 @@ local function residual(b: buffer, p: number, lim: number, s: { number }, n: num
|
||||
p += k
|
||||
end
|
||||
else
|
||||
-- a long run of zeros
|
||||
q = 32 - sh
|
||||
p += q
|
||||
while true do
|
||||
@@ -448,7 +436,6 @@ local function lpc(s: { number }, n: number, cf: { number }, order: number, shif
|
||||
end
|
||||
end
|
||||
|
||||
-- One subframe into s[1..n]. Returns the new bit position, or -1.
|
||||
local function subframe(b: buffer, p: number, lim: number, s: { number }, n: number, bps: number, cf: { number }): number
|
||||
if p > lim then
|
||||
return -1
|
||||
@@ -558,8 +545,6 @@ local function subframe(b: buffer, p: number, lim: number, s: { number }, n: num
|
||||
return p
|
||||
end
|
||||
|
||||
-- Decode the frame whose header h was parsed (data ends at len). chans[c] are tables of at least h.blockSize
|
||||
-- numbers; they receive the samples. Returns the byte offset after the frame, or nil, err.
|
||||
function Frame.decode(b: buffer, len: number, h: Header, chans: { { number } }, cf: { number }, checkCrc: boolean): (number?, string?)
|
||||
local n = h.blockSize
|
||||
local lim = len * 8
|
||||
@@ -575,7 +560,7 @@ function Frame.decode(b: buffer, len: number, h: Header, chans: { { number } },
|
||||
return nil, "bad subframe"
|
||||
end
|
||||
end
|
||||
local e = rshift(p + 7, 3) -- byte align
|
||||
local e = rshift(p + 7, 3)
|
||||
if e + 2 > len then
|
||||
return nil, "truncated frame"
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user