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,15 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Flac/Encode: the FLAC encoder (RFC 9639). New code, written from the RFC; the presets mirror libFLAC's -0 .. -8
|
||||
-- (block size 1152 / 4096, max LPC order 0 / 6 / 8 / 12, Rice partition order 3 .. 6, independent / adaptive /
|
||||
-- exhaustive stereo decorrelation). Subframes: CONSTANT, VERBATIM, FIXED (order 0..4, chosen by the residual sum),
|
||||
-- LPC (Levinson-Durbin on the tukey(0.5)-windowed autocorrelation, error-feedback coefficient quantization with
|
||||
-- libFLAC's precision table, the order estimated from the prediction error or searched exhaustively at -8), wasted
|
||||
-- bits; Rice / Rice2 partitions with the partition order and parameters searched per subframe. Frames carry CRC-8 /
|
||||
-- CRC-16; STREAMINFO carries the sizes, total samples and the MD5 of the PCM; VORBIS_COMMENT from the tags; an optional
|
||||
-- SEEKTABLE. Streams: input blocks are pulled from a codecs/audio/Pcm Source, so a Decoder is encoded without decoding it
|
||||
-- whole. Never throws: bad options are rejected by audio/flac before calling in.
|
||||
-- codecs/audio/Flac/Encode: the FLAC encoder (RFC 9639), written from the RFC.
|
||||
-- The presets mirror libFLAC's -0 .. -8; QLP precision follows libFLAC's table.
|
||||
local Bits = require("@rosetta/core/Bits")
|
||||
local Md5 = require("@rosetta/core/Md5")
|
||||
local VorbisComment = require("@rosetta/codecs/audio/VorbisComment")
|
||||
@@ -25,8 +18,6 @@ for i = 0, 63 do
|
||||
POW[i + 1] = 2 ^ i
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ CRCs
|
||||
-- (the decoder's tables: codecs/audio/Flac/Frame; (b, offset, count) here)
|
||||
local function crc8(b: buffer, o: number, n: number): number
|
||||
return Frame.crc8(b, o, o + n)
|
||||
end
|
||||
@@ -35,7 +26,6 @@ local function crc16(b: buffer, o: number, n: number): number
|
||||
end
|
||||
Encode.crc8, Encode.crc16 = crc8, crc16
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bit writer (MSB first)
|
||||
type W = Bits.RawWriter
|
||||
|
||||
local function ensure(w: W, n: number)
|
||||
@@ -74,12 +64,11 @@ local function putUtf8(w: W, v: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ presets
|
||||
export type Config = {
|
||||
blockSize: number,
|
||||
maxLpcOrder: number,
|
||||
maxPartitionOrder: number,
|
||||
stereo: string, -- "independent" | "adaptive" | "exhaustive"
|
||||
stereo: string,
|
||||
exhaustiveOrder: boolean,
|
||||
}
|
||||
local PRESETS: { Config } = {
|
||||
@@ -95,22 +84,20 @@ local PRESETS: { Config } = {
|
||||
}
|
||||
Encode.PRESETS = PRESETS
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ analysis
|
||||
-- a subframe plan for one channel signal
|
||||
type Plan = {
|
||||
kind: number, -- 0 constant, 1 verbatim, 2 fixed, 3 lpc
|
||||
bits: number, -- estimated size in bits (with the subframe header)
|
||||
kind: number,
|
||||
bits: number,
|
||||
order: number,
|
||||
wasted: number,
|
||||
bps: number, -- sample size after removing wasted bits
|
||||
res: { number }, -- residual (fixed / lpc), indices 1..n
|
||||
bps: number,
|
||||
res: { number },
|
||||
porder: number,
|
||||
params: { number },
|
||||
method: number, -- 0 Rice (4-bit params), 1 Rice2 (5-bit)
|
||||
qlp: { number },
|
||||
shift: number,
|
||||
precision: number,
|
||||
x: { number }, -- the (shifted) samples
|
||||
x: { number },
|
||||
}
|
||||
|
||||
local function newPlan(n: number): Plan
|
||||
@@ -131,7 +118,6 @@ local function newPlan(n: number): Plan
|
||||
}
|
||||
end
|
||||
|
||||
-- scratch per encoder
|
||||
type Scratch = {
|
||||
sums: { number },
|
||||
counts: { number },
|
||||
@@ -147,9 +133,7 @@ type Scratch = {
|
||||
qlp: { number },
|
||||
}
|
||||
|
||||
-- rice(res, n, order, maxP, plan?) -> bits, porder, method (fills scratch.bestKs with the parameters)
|
||||
local function rice(sc: Scratch, res: { number }, n: number, order: number, maxP: number): (number, number, number)
|
||||
-- the finest usable partition order
|
||||
local p = maxP
|
||||
while p > 0 and (n % POW[p + 1] ~= 0 or n // POW[p + 1] <= order) do
|
||||
p -= 1
|
||||
@@ -157,7 +141,6 @@ local function rice(sc: Scratch, res: { number }, n: number, order: number, maxP
|
||||
local sums, counts = sc.sums, sc.counts
|
||||
local parts = POW[p + 1]
|
||||
local ps = n // parts
|
||||
-- sums of zig-zag values at the finest order
|
||||
for j = 0, parts - 1 do
|
||||
local s = 0
|
||||
local a = if j == 0 then order + 1 else j * ps + 1
|
||||
@@ -206,7 +189,6 @@ local function rice(sc: Scratch, res: { number }, n: number, order: number, maxP
|
||||
if q == 0 then
|
||||
break
|
||||
end
|
||||
-- merge pairs into the next coarser order
|
||||
for j = 1, np // 2 do
|
||||
sums[j] = sums[2 * j - 1] + sums[2 * j]
|
||||
counts[j] = counts[2 * j - 1] + counts[2 * j]
|
||||
@@ -224,7 +206,6 @@ local function keepRice(sc: Scratch, plan: Plan, porder: number, method: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- fixed predictor residuals
|
||||
local function fixedResidual(x: { number }, n: number, order: number, res: { number })
|
||||
for i = 1, order do
|
||||
res[i] = 0
|
||||
@@ -310,7 +291,6 @@ local function qlpPrecision(bps: number, n: number): number
|
||||
return 13
|
||||
end
|
||||
|
||||
-- quantize coefficients lp[1..order] to precision bits: qlp[1..order], shift | nil
|
||||
local function quantize(lp: { number }, order: number, precision: number, qlp: { number }): number?
|
||||
local cmax = 0
|
||||
for i = 1, order do
|
||||
@@ -372,7 +352,6 @@ local function lpcResidual(x: { number }, n: number, order: number, qlp: { numbe
|
||||
end
|
||||
end
|
||||
|
||||
-- Levinson-Durbin: sc.lpc[o][1..o] and sc.err[o] for o = 1 .. maxOrder; returns the highest usable order
|
||||
local function levinson(sc: Scratch, maxOrder: number): number
|
||||
local r = sc.autoc
|
||||
local e = r[1]
|
||||
@@ -407,10 +386,8 @@ local function levinson(sc: Scratch, maxOrder: number): number
|
||||
return maxOrder
|
||||
end
|
||||
|
||||
-- choose the subframe for signal x (n samples, bps bits) into plan (reusing its arrays)
|
||||
local function analyze(sc: Scratch, cfg: Config, src: { number }, n: number, bps: number, plan: Plan)
|
||||
local x = plan.x
|
||||
-- constant?
|
||||
local first = src[1]
|
||||
local constant = true
|
||||
for i = 1, n do
|
||||
@@ -424,7 +401,6 @@ local function analyze(sc: Scratch, cfg: Config, src: { number }, n: number, bps
|
||||
plan.res[1] = first
|
||||
return
|
||||
end
|
||||
-- wasted bits: the common trailing zeros
|
||||
local wasted = 0
|
||||
do
|
||||
local w = 0
|
||||
@@ -451,9 +427,7 @@ local function analyze(sc: Scratch, cfg: Config, src: { number }, n: number, bps
|
||||
local b = bps - wasted
|
||||
local hdr = 8 + wasted
|
||||
plan.wasted, plan.bps = wasted, b
|
||||
-- verbatim
|
||||
plan.kind, plan.bits = 1, hdr + n * b
|
||||
-- fixed
|
||||
local fo = bestFixedOrder(x, n)
|
||||
if n > fo then
|
||||
local tmp = sc.tmpRes
|
||||
@@ -466,10 +440,8 @@ local function analyze(sc: Scratch, cfg: Config, src: { number }, n: number, bps
|
||||
keepRice(sc, plan, po, me)
|
||||
end
|
||||
end
|
||||
-- lpc
|
||||
local maxOrder = math.min(cfg.maxLpcOrder, n - 1, 32)
|
||||
if maxOrder > 0 then
|
||||
-- windowed autocorrelation
|
||||
local win = sc.win
|
||||
if sc.winN ~= n then
|
||||
-- tukey(0.5)
|
||||
@@ -542,7 +514,6 @@ local function analyze(sc: Scratch, cfg: Config, src: { number }, n: number, bps
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ writing
|
||||
local function writeResidual(w: W, plan: Plan, n: number)
|
||||
local method = plan.method
|
||||
put(w, method, 2)
|
||||
@@ -649,7 +620,6 @@ local function blockCode(n: number): (number, number) -- code, extra bits (0, 8,
|
||||
return 7, 16
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encoder
|
||||
export type Source = {
|
||||
sampleRate: number,
|
||||
channels: number,
|
||||
@@ -662,18 +632,16 @@ export type Options = {
|
||||
level: number,
|
||||
bits: number,
|
||||
blockSize: number?,
|
||||
seekInterval: number, -- seconds between seek points (0 = no SEEKTABLE)
|
||||
padding: number, -- bytes of PADDING
|
||||
seekInterval: number,
|
||||
padding: number,
|
||||
vendor: string,
|
||||
}
|
||||
|
||||
-- encode(src, opts) -> buffer | nil, message
|
||||
function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
local cfg = PRESETS[opts.level + 1]
|
||||
local ch, rate, bps = src.channels, src.sampleRate, opts.bits
|
||||
local bs = opts.blockSize or cfg.blockSize
|
||||
local w: W = { buf = buffer.create(65536), pos = 0, acc = 0, cnt = 0 }
|
||||
-- metadata
|
||||
put(w, 0x664C6143, 32) -- "fLaC"
|
||||
put(w, 0, 1)
|
||||
put(w, 0, 7)
|
||||
@@ -682,7 +650,6 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
for _ = 1, 34 do
|
||||
put(w, 0, 8)
|
||||
end
|
||||
-- seek table placeholders (when the length is known)
|
||||
local seekPoints = 0
|
||||
local seekAt = 0
|
||||
local interval = floor(opts.seekInterval * rate)
|
||||
@@ -692,14 +659,12 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
seekPoints = 0
|
||||
end
|
||||
end
|
||||
-- vorbis comment
|
||||
local comments = VorbisComment.fields(src.tags)
|
||||
local vendor = opts.vendor
|
||||
local vcLen = 4 + #vendor + 4
|
||||
for _, c in comments do
|
||||
vcLen += 4 + #c
|
||||
end
|
||||
-- SEEKTABLE
|
||||
if seekPoints > 0 then
|
||||
put(w, 0, 1)
|
||||
put(w, 3, 7)
|
||||
@@ -747,7 +712,6 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
end
|
||||
end
|
||||
local audioStart = w.pos
|
||||
-- frames
|
||||
local md5 = Md5.new()
|
||||
local mbytes = (bps + 7) // 8
|
||||
local mbuf = buffer.create(bs * ch * mbytes)
|
||||
@@ -794,7 +758,6 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
local tmp: buffer? = nil
|
||||
local pendingBuf: buffer? = nil
|
||||
local pendingN, pendingOff = 0, 0
|
||||
-- read exactly bs frames (fewer only at the end)
|
||||
local function readBlock(): (buffer?, number)
|
||||
local out = buffer.create(bs * ch * 4)
|
||||
local got = 0
|
||||
@@ -841,7 +804,6 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
end
|
||||
end
|
||||
md5:update(mbuf, 0, mo)
|
||||
-- channel decorrelation
|
||||
local assign = ch - 1
|
||||
local chosen: { Plan } = {}
|
||||
if ch == 2 and cfg.stereo ~= "independent" then
|
||||
@@ -900,14 +862,12 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
chosen[c] = plans[4 + c]
|
||||
end
|
||||
end
|
||||
-- the frame
|
||||
local need = n * ch * 4 + 64
|
||||
for _, p in chosen do
|
||||
need += (p.bits // 8) + 8
|
||||
end
|
||||
ensure(w, need)
|
||||
local start = w.pos
|
||||
-- seek points: the first frame at or after each multiple of the interval
|
||||
while seekIdx < seekPoints and total >= seekNext do
|
||||
local at = seekAt + seekIdx * 18
|
||||
local sn, off = total, start - audioStart
|
||||
@@ -957,7 +917,6 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
if src.err then
|
||||
return nil, tostring(src.err)
|
||||
end
|
||||
-- unused seek points stay placeholders; STREAMINFO
|
||||
local out = buffer.create(w.pos)
|
||||
buffer.copy(out, 0, w.buf, 0, w.pos)
|
||||
local si = Encode.streaminfo(bs, if frameNo > 0 then minFrame else 0, maxFrame, rate, ch, bps, total, md5:digest())
|
||||
@@ -965,7 +924,7 @@ function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- streaminfo(...) -> the 34-byte STREAMINFO body
|
||||
-- STREAMINFO: 34 bytes
|
||||
function Encode.streaminfo(bs: number, minFrame: number, maxFrame: number, rate: number, ch: number, bps: number, total: number, md5: string): buffer
|
||||
local w: W = { buf = buffer.create(64), pos = 0, acc = 0, cnt = 0 }
|
||||
put(w, bs, 16)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
--!strict
|
||||
-- codecs/audio/Flac: FLAC frames without the container: frame header parsing, subframe decoding (constant, verbatim,
|
||||
-- fixed, LPC, Rice residuals, stereo decorrelation) with CRC-8 / CRC-16 (Frame), and the frame encoder (Encode).
|
||||
-- Absorbed from flac-luau 7083d0d (same author); the encoder is new. Native FLAC / Ogg FLAC streams, metadata and
|
||||
-- seeking are audio/flac.
|
||||
-- codecs/audio/Flac: FLAC frames without the container (frame header, subframes, CRC-8 / CRC-16) and the frame encoder.
|
||||
-- Absorbed from flac-luau 7083d0d; the encoder is new.
|
||||
local Flac = {
|
||||
Frame = require("@rosetta/codecs/audio/Flac/Frame"),
|
||||
Encode = require("@rosetta/codecs/audio/Flac/Encode"),
|
||||
|
||||
Reference in new issue
Block a user