diff --git a/licenses/NOTICE.md b/licenses/NOTICE.md index 26dd01b..1dab632 100644 --- a/licenses/NOTICE.md +++ b/licenses/NOTICE.md @@ -60,6 +60,7 @@ | `src/Rosetta/codecs/image/Bcn/Decode.luau` (the BC1..BC7 block decoders and the BPTC tables; changed to follow the D3D11 rules) | Pillow 12 `src/libImaging/BcnDecode.c` | CC0 1.0: [pillow-bcn-CC0.txt](pillow-bcn-CC0.txt) | | `src/Rosetta/image/exr/Piz.luau`, `src/Rosetta/image/exr/Codec.luau` (the PIZ Huffman / wavelet / LUT steps and the ZIP / RLE byte predictor, re-implemented in Luau after the OpenEXR algorithms) | OpenEXR 3.x (ImfPizCompressor, ImfHuf, ImfWav, ImfZip, ImfRle) | BSD-3-Clause: [openexr-LICENSE.md](openexr-LICENSE.md) | | `src/Rosetta/model/godot/Primitives.luau` (BoxMesh, PlaneMesh, QuadMesh, SphereMesh, CylinderMesh generation, ported to Luau without UV2) | Godot Engine 4.4 `scene/resources/3d/primitive_meshes.cpp` | MIT: [godot-LICENSE.txt](godot-LICENSE.txt) | +| `src/Rosetta/codecs/audio/Aac/Sbr.luau`, `SbrHf.luau`, `Ps.luau`, `SbrTables.luau` (HE-AAC SBR / PS: the bitstream, band tables, HF generation and adjustment, the PS hybrid filterbank / decorrelator / mixing, ported to Luau; the QMF banks are new) | syom 0.8.1 `src/engine/sbr_*.rs`, `ps_*.rs` (Evgeny Khodzitsky; its tables are ISO/IEC 14496-3 numbers) | MIT: [syom-LICENSE.txt](syom-LICENSE.txt) | Ports of third-party code (minimp3, stb_vorbis, libopus, ...) add a row here and their upstream licence text in this folder (see docs/STYLE.md, "Absorbing an existing repo"). diff --git a/licenses/syom-LICENSE.txt b/licenses/syom-LICENSE.txt new file mode 100644 index 0000000..5eae24e --- /dev/null +++ b/licenses/syom-LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Evgeny Khodzitsky + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING WITHOUT LIMITATION THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/Rosetta/audio/aac/init.luau b/src/Rosetta/audio/aac/init.luau index 6b28f3a..515d11b 100644 --- a/src/Rosetta/audio/aac/init.luau +++ b/src/Rosetta/audio/aac/init.luau @@ -1,8 +1,9 @@ --!strict --!optimize 2 -- audio/aac: raw AAC in ADTS framing (ISO/IEC 13818-7 / 14496-3 1.A.3). Supports: AAC-LC via codecs/audio/Aac (1-8 --- channels, every rate), CRC-protected headers, a leading ID3v2 tag, resync, seeking by frame; HE-AAC plays its --- AAC-LC core. Not supported: writing, ADIF, LATM. +-- channels, every rate), HE-AAC / HE-AACv2 (SBR / PS found in the stream: ADTS cannot signal them; opts.sbr = false +-- keeps the AAC-LC core), CRC-protected headers, a leading ID3v2 tag, resync, seeking by frame. Not supported: writing, +-- ADIF, LATM. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") @@ -25,6 +26,7 @@ local AacFormat = { -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { maxFrames: number?, -- default 2^22 + sbr: boolean?, -- false: decode only the AAC-LC core of HE-AAC (half rate, faster); default true } -- ------------------------------------------------------------------------------------------------ constants @@ -150,14 +152,18 @@ function AacFormat.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, return nil, Err.new(Err.UNSUPPORTED, "ADTS with a program config element (channel configuration 0)") end local channels = ({ 1, 2, 3, 4, 5, 6, 8 })[h.channelConfig] :: number - local dec, derr = Aac.decoder({ sfIndex = h.sfIndex, channels = channels, channelConfig = h.channelConfig }) + local dec, derr = Aac.decoder({ sfIndex = h.sfIndex, channels = channels, channelConfig = h.channelConfig }, { sbr = o.sbr }) if not dec then return nil, derr end local d = dec :: Aac.Decoder - local sr = d.sampleRate + if Aac.probeFrames(d, r.buf, offs, sizes) then + d:reset() + end + local sr, fl = d.sampleRate, d.frameLength + channels = d.channels local nframes = #offs - local total = nframes * 1024 + local total = nframes * fl local bytes = 0 for _, s in sizes do bytes += s @@ -174,11 +180,11 @@ function AacFormat.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, tags = tags, codec = "aac", } - -- 1024 samples per ADTS frame; one frame of pre-roll for the overlap + -- 1024 (2048 with SBR) samples per ADTS frame; pre-roll for the overlap (and the SBR filterbanks / time deltas) return Stream.packets(out, { count = nframes, start = function(i: number): number - return (i - 1) * 1024 + return (i - 1) * fl end, decode = function(i: number): (number?, Err.Err?) return d:decode(buf, offs[i], sizes[i]) @@ -189,7 +195,7 @@ function AacFormat.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, reset = function() d:reset() end, - preroll = 1, + preroll = if d.sbrOutput then 3 else 1, }) end diff --git a/src/Rosetta/audio/m4a/init.luau b/src/Rosetta/audio/m4a/init.luau index 2105e8d..6f3ad64 100644 --- a/src/Rosetta/audio/m4a/init.luau +++ b/src/Rosetta/audio/m4a/init.luau @@ -1,8 +1,8 @@ --!strict --!optimize 2 --- audio/m4a: MPEG-4 audio (M4A / M4B / MP4 / MOV audio tracks) via containers/IsoBmff. Supports: AAC-LC (HE-AAC plays --- its core), ALAC, edit-list gapless trimming, seeking, iTunes tags and cover art; encode ALAC (16 / 20 / 24-bit, 1-8 --- channels). Not supported: writing AAC, other codecs in MP4 (UNSUPPORTED), encrypted files. +-- audio/m4a: MPEG-4 audio (M4A / M4B / MP4 / MOV audio tracks) via containers/IsoBmff. Supports: AAC-LC, HE-AAC / +-- HE-AACv2 (explicit or implicit SBR / PS; opts.sbr = false keeps the AAC-LC core), ALAC, edit-list gapless trimming, +-- seeking, iTunes tags and cover art; encode ALAC (16 / 20 / 24-bit, 1-8 channels). Not supported: writing AAC, other codecs in MP4 (UNSUPPORTED), encrypted files. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") @@ -29,6 +29,7 @@ export type DecodeOptions = { track: number?, -- the track id to play (default: the first AAC / ALAC track) gapless: boolean?, -- apply the edit list (default true) maxSamples: number?, -- default 2^25 + sbr: boolean?, -- false: decode only the AAC-LC core of HE-AAC (half rate, faster); default true } export type EncodeOptions = { bitDepth: number?, -- 16 (default), 20 or 24 @@ -63,9 +64,10 @@ type PacketDecoder = { pcm: { number }, decode: (buf: buffer, off: number, len: number) -> (number?, Err.Err?), reset: () -> (), + sbr: boolean?, } -local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?) +local function packetDecoder(t: IsoBmff.Track, buf: buffer, sbr: boolean?): (PacketDecoder?, Err.Err?) if not t.config then return nil, Err.new(Err.CORRUPT, "track " .. t.id .. " (" .. t.codec .. ") has no codec configuration") end @@ -97,11 +99,14 @@ local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?) } return pd, nil elseif t.codec == "aac" then - local dec, err = Aac.decoder(t.config :: buffer) + local dec, err = Aac.decoder(t.config :: buffer, { sbr = sbr }) if not dec then return nil, err end local d = dec :: Aac.Decoder + if Aac.probeFrames(d, buf, t.offsets, t.sizes) then + d:reset() + end local pd: PacketDecoder = { channels = d.channels, sampleRate = d.sampleRate, @@ -113,6 +118,7 @@ local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?) reset = function() d:reset() end, + sbr = d.sbrOutput, } return pd, nil end @@ -146,7 +152,7 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E return nil, Err.new(Err.CORRUPT, "no audio track") end local t = track :: IsoBmff.Track - local pd, perr = packetDecoder(t) + local pd, perr = packetDecoder(t, r.buf, o.sbr) if not pd then return nil, perr end @@ -171,9 +177,11 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E for w in string.gmatch(smpb, "%x+") do table.insert(f, tonumber(w, 16) :: number) end - if #f >= 4 and f[2] and f[4] and f[4] > 0 and f[2] + f[4] <= total then - skip = f[2] - total = f[2] + f[4] + -- in media ticks (the core rate of HE-AAC, as ffmpeg reads it) + local pre, len = math.floor((f[2] or 0) * tickScale + 0.5), math.floor((f[4] or 0) * tickScale + 0.5) + if #f >= 4 and len > 0 and pre + len <= total then + skip = pre + total = pre + len end end local frames = math.max(0, total - skip) @@ -210,7 +218,7 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E reset = p.reset, skip = skip, stop = total, - preroll = if t.codec == "aac" then 1 else 0, + preroll = if p.sbr then 3 elseif t.codec == "aac" then 1 else 0, cumulative = true, }) end diff --git a/src/Rosetta/codecs/audio/Aac/Decode.luau b/src/Rosetta/codecs/audio/Aac/Decode.luau index 2034e38..83efadd 100644 --- a/src/Rosetta/codecs/audio/Aac/Decode.luau +++ b/src/Rosetta/codecs/audio/Aac/Decode.luau @@ -5,6 +5,7 @@ -- Written from the standard; no third-party code. PNS uses FFmpeg's LCG so the noise matches it. local Mdct = require("@rosetta/codecs/audio/Mdct") local Tables = require("@rosetta/codecs/audio/Aac/Tables") +local Sbr = require("@rosetta/codecs/audio/Aac/Sbr") local readu32, byteswap, lshift, rshift, band = buffer.readu32, bit32.byteswap, bit32.lshift, bit32.rshift, bit32.band local floor, sqrt, abs = math.floor, math.sqrt, math.abs @@ -150,6 +151,14 @@ export type State = { extensions: number, sbr: boolean, map: { number }?, + -- SBR output (codecs/audio/Aac/Sbr): sbrRate = the output rate when on, psOut = a mono stream output as stereo + sbrOn: boolean, + sbrRate: number, + psOut: boolean, + sbrElems: { any }, + elemKind: { number }, + elemFirst: { number }, + core: { { number } }, } local function newChannel(): Channel @@ -730,6 +739,50 @@ local function filterbank(st: State, c: Channel, out: { number }, stride: number c.prevShape = shape end +local function sbrElement(st: State, i: number, nch: number): any + local e = st.sbrElems[i] + if not e or e.nch ~= nch then + e = Sbr.element(nch, st.sbrRate, st.psOut and nch == 1) + st.sbrElems[i] = e + end + return e +end + +local OFFS = { 0, 0 } +local CORE2 = { {}, {} } + +-- every element through SBR at the doubled rate (2048 samples per channel); the PS output of a mono element is stereo +local function sbrOutput(st: State, order: { Channel }, nElem: number): number + local n = #order + local ps = st.psOut and n == 1 + local stride = if ps then 2 else n + local map = st.map + local core = st.core + for i, c in order do + local t = core[i] + if not t then + t = table.create(1024, 0) + core[i] = t + end + filterbank(st, c, t, 1, 1) + end + for ei = 1, nElem do + local first = st.elemFirst[ei] + local nch = if st.elemKind[ei] == 1 then 2 else 1 + local e = sbrElement(st, ei, nch) + for j = 1, nch do + local idx = first + j - 1 + OFFS[j] = if map and map[idx] then map[idx] else idx + CORE2[j] = core[idx] + end + if ps then + OFFS[2] = 2 + end + Sbr.process(e, CORE2, st.pcm, stride, OFFS) + end + return stride +end + function Decode.frame(st: State, buf: buffer, off: number, len: number): (number?, string?) if buffer.len(B) < len + 16 then B = buffer.create(len + 256) @@ -741,6 +794,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number local nextCh = 1 local order: { Channel } = {} local lastElement = -1 + local nElem = 0 + local elemKind, elemFirst = st.elemKind, st.elemFirst while true do if BP + 3 > BL then break -- tolerate a missing END @@ -762,6 +817,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number end applyTns(st, c) table.insert(order, c) + nElem += 1 + elemKind[nElem], elemFirst[nElem] = id, #order nextCh += 1 lastElement = id elseif id == 1 then -- CPE @@ -818,6 +875,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number applyTns(st, r) table.insert(order, l) table.insert(order, r) + nElem += 1 + elemKind[nElem], elemFirst[nElem] = id, #order - 1 nextCh += 2 lastElement = id elseif id == 2 then @@ -845,6 +904,9 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number local kind = getBits(4) if kind == 13 or kind == 14 then -- EXT_SBR_DATA / EXT_SBR_DATA_CRC st.sbr = true + if st.sbrOn and nElem > 0 and lastElement ~= 3 then + Sbr.extension(sbrElement(st, nElem, if lastElement == 1 then 2 else 1), B, start + 4, count * 8 - 4, kind == 14) + end end st.extensions += 1 local hook = st.onExtension @@ -865,6 +927,9 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number if n == 0 then return 0, nil end + if st.sbrOn then + return sbrOutput(st, order, nElem), nil + end local pcm = st.pcm local map = st.map for i, c in order do @@ -880,7 +945,7 @@ function Decode.state(sfIndex: number): State channels = 0, frameLength = 1024, ch = {}, - pcm = table.create(1024 * 8, 0), + pcm = table.create(2048 * 8, 0), random = 0x1f2e3d4c, time = table.create(2048, 0), frame = table.create(2048, 0), @@ -889,6 +954,13 @@ function Decode.state(sfIndex: number): State extensions = 0, sbr = false, map = nil, + sbrOn = false, + sbrRate = 0, + psOut = false, + sbrElems = {}, + elemKind = {}, + elemFirst = {}, + core = {}, } end diff --git a/src/Rosetta/codecs/audio/Aac/Ps.luau b/src/Rosetta/codecs/audio/Aac/Ps.luau new file mode 100644 index 0000000..070b61d --- /dev/null +++ b/src/Rosetta/codecs/audio/Aac/Ps.luau @@ -0,0 +1,544 @@ +--!strict +--!native +--!optimize 2 +-- codecs/audio/Aac/Ps: baseline Parametric Stereo (ISO/IEC 14496-3 8.6.4, syntax 8.3): ps_data with IID / ICC (10, +-- 20 or 34 bands, mapped to 20), the 10/20-band hybrid analysis filterbank, the all-pass / delay decorrelator with +-- transient attenuation, and mixing procedures Ra / Rb. IPD / OPD (the ps_extension) are skipped, as baseline PS +-- allows. Ported from syom 0.8.1 src/engine/ps_*.rs (MIT, licenses/syom-LICENSE.txt). +local SbrTables = require("@rosetta/codecs/audio/Aac/SbrTables") +local SbrBits = require("@rosetta/codecs/audio/Aac/SbrBits") + +local cos, sin, sqrt, pi, acos, atan = math.cos, math.sin, math.sqrt, math.pi, math.acos, math.atan +local bits, huff, codebook = SbrBits.bits, SbrBits.huff, SbrBits.codebook + +local Ps = {} + +local IID_DF = codebook(SbrTables.IID_DF) +local IID_DT = codebook(SbrTables.IID_DT) +local IID_FINE_DF = codebook(SbrTables.IID_FINE_DF) +local IID_FINE_DT = codebook(SbrTables.IID_FINE_DT) +local ICC_DF = codebook(SbrTables.ICC_DF) +local ICC_DT = codebook(SbrTables.ICC_DT) + +local NR_PAR = { 10, 20, 34, 10, 20, 34 } +local NUM_ENV = { 0, 1, 2, 4, 1, 2, 3, 4 } + +-- Table 8.25 / 8.26: IID steps (dB) and ICC values +local IID_COARSE = { -25, -18, -14, -10, -7, -4, -2, 0, 2, 4, 7, 10, 14, 18, 25 } +local IID_FINE = { -50, -45, -40, -35, -30, -25, -22, -19, -16, -13, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 13, 16, 19, 22, + 25, 30, 35, 40, 45, 50 } +local ICC_RHO = { 1.0, 0.937, 0.84118, 0.60092, 0.36764, 0.0, -0.589, -1.0 } +for i, v in IID_COARSE do + IID_COARSE[i] = 10 ^ (v / 20) +end +for i, v in IID_FINE do + IID_FINE[i] = 10 ^ (v / 20) +end + +-- 34 -> 20 parameter bands (8.6.4.6.1): { i, j, wi, wj } weights over 34-band indexes (0-based) +local MAP34 = { + { 0, 1, 2, 1 }, { 1, 2, 1, 2 }, { 3, 4, 2, 1 }, { 4, 5, 1, 2 }, { 6, 7, 1, 1 }, { 8, 9, 1, 1 }, { 10, 10, 1, 0 }, + { 11, 11, 1, 0 }, { 12, 13, 1, 1 }, { 14, 15, 1, 1 }, { 16, 16, 1, 0 }, { 17, 17, 1, 0 }, { 18, 18, 1, 0 }, + { 19, 19, 1, 0 }, { 20, 21, 1, 1 }, { 22, 23, 1, 1 }, { 24, 25, 1, 1 }, { 26, 27, 1, 1 }, { 28, 29, 1, 1 }, + { 32, 33, 1, 1 }, +} + +-- Table 8.48: parameter band of each of the 71 hybrid bands (20-band configuration) +local B_K = { 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 17 } +for _ = 1, 12 do + table.insert(B_K, 18) +end +while #B_K < 71 do + table.insert(B_K, 19) +end + +-- 8.6.4.3 hybrid prototype filters (13 taps) +local G0 = { 0.00746082949812, 0.02270420949825, 0.04546865930473, 0.07266113929591, 0.09885108575264, 0.11793710567217, + 0.125, 0.11793710567217, 0.09885108575264, 0.07266113929591, 0.04546865930473, 0.02270420949825, 0.00746082949812 } +local G12 = { 0.0, 0.01899487526049, 0.0, -0.07293139167538, 0.0, 0.30596630545168, 0.5, 0.30596630545168, 0.0, + -0.07293139167538, 0.0, 0.01899487526049, 0.0 } +-- band 0, type A, Q = 8: H0[(q * 13 + m) * 2 + 1 / 2] = g[m] e^(i 2 pi / 8 (q + 1/2) (m - 6)) +local H0R, H0I = table.create(8 * 13, 0), table.create(8 * 13, 0) +for q = 0, 7 do + for m = 0, 12 do + local a = 2 * pi / 8 * (q + 0.5) * (m - 6) + H0R[q * 13 + m + 1] = G0[m + 1] * cos(a) + H0I[q * 13 + m + 1] = G0[m + 1] * sin(a) + end +end +-- the hybrid band receiving each band-0 filter output (s4 = q2 + q5, s5 = q3 + q4) +local Q0_TO = { 2, 3, 4, 5, 5, 4, 0, 1 } + +-- 8.6.4.5 decorrelator constants (20-band configuration) +local AP_A = { 0.65143905753106, 0.56471812200776, 0.48954165955695 } +local AP_D = { 3, 4, 5 } +local Q_FRACT = { 0.43, 0.75, 0.347 } +local F_CENTER = { -3 / 8, -1 / 8, 1 / 8, 3 / 8, 5 / 8, 7 / 8, 5 / 4, 7 / 4, 9 / 4, 11 / 4 } +local NR_AP = 30 +local PHI_R, PHI_I = table.create(NR_AP, 0), table.create(NR_AP, 0) +local QF_R, QF_I = table.create(NR_AP * 3, 0), table.create(NR_AP * 3, 0) +local G_DECAY = table.create(NR_AP, 0) +for k = 0, NR_AP - 1 do + local f = if k < 10 then F_CENTER[k + 1] else k + 0.5 - 7 + PHI_R[k + 1], PHI_I[k + 1] = cos(-pi * 0.39 * f), sin(-pi * 0.39 * f) + for m = 0, 2 do + QF_R[k * 3 + m + 1], QF_I[k * 3 + m + 1] = cos(-pi * Q_FRACT[m + 1] * f), sin(-pi * Q_FRACT[m + 1] * f) + end + G_DECAY[k + 1] = if k > 10 then math.max(0, 1 - 0.05 * (k - 10)) else 1 +end +local PEAK_DECAY, A_SMOOTH, GAMMA = 0.76592833836465, 0.25, 1.5 + +function Ps.state(): any + return { + haveConfig = false, + enableIid = false, + iidMode = 0, + enableIcc = false, + iccMode = 0, + enableExt = false, + iidPrev = table.create(34, 0), + iidPrevN = 0, + iccPrev = table.create(34, 0), + iccPrevN = 0, + hist = table.create(3 * 6 * 2, 0), -- hybrid input history: [(p * 6 + j) * 2 + 1 / 2] + apIn = table.create(NR_AP * 4, 0), -- z^-2 delay: in[n-1], in[n-2] (re, im) + apW = table.create(NR_AP * 3 * 5 * 2, 0), -- all-pass links: [((k * 3 + m) * 5 + pos) * 2 + 1 / 2] + apPos = table.create(NR_AP * 3, 0), + dl = table.create(41 * 14 * 2, 0), -- plain delays of hybrid bands 30 .. 70 + dlPos = table.create(41, 0), + peak = table.create(20, 0), + smooth = table.create(20, 0), + smoothDiff = table.create(20, 0), + hPrev = table.create(20 * 4, 0), + prevHadPs = false, + active = false, + -- frame parameters + nEnv = 0, + border = table.create(5, 0), + iid = table.create(4 * 20, 0), + icc = table.create(4 * 20, 0), + -- hybrid rows [(n * 71 + k) + 1], decorrelated rows + sr = table.create(32 * 71, 0), + si = table.create(32 * 71, 0), + dr = table.create(32 * 71, 0), + di = table.create(32 * 71, 0), + hs = table.create(32 * 20 * 4, 0), + } +end + +local TMP = table.create(34, 0) + +-- integer division truncating toward zero (the 8.6.4.6.1 mapping averages) +local function tdiv(a: number, b: number): number + local q = a / b + return if q >= 0 then math.floor(q) else -math.floor(-q) +end + +-- one parameter kind: nEnv rows into out[(e - 1) * 20 + b] (mapped to 20 bands) +local function readKind(st: any, nEnv: number, nr: number, fine: boolean, iid: boolean, out: { number }): boolean + local prev = if iid then st.iidPrev else st.iccPrev + local prevN = if iid then st.iidPrevN else st.iccPrevN + local lo, hi = 0, 7 + if iid then + lo = if fine then -15 else -7 + hi = -lo + end + for e = 1, nEnv do + local dt = bits(1) == 1 + local bk + if iid then + bk = if fine then (if dt then IID_FINE_DT else IID_FINE_DF) else (if dt then IID_DT else IID_DF) + else + bk = if dt then ICC_DT else ICC_DF + end + local acc = 0 + for b = 1, nr do + local d = huff(bk) + if not d then + return false + end + local v + if dt then + v = (if prevN == nr then prev[b] else 0) + d + else + acc += d + v = acc + end + if v < lo or v > hi then + return false + end + TMP[b] = v + end + for b = 1, nr do + prev[b] = TMP[b] + end + prevN = nr + local base = (e - 1) * 20 + if nr == 20 then + for b = 1, 20 do + out[base + b] = TMP[b] + end + elseif nr == 10 then + for b = 1, 20 do + out[base + b] = TMP[(b - 1) // 2 + 1] + end + else + for b = 1, 20 do + local mp = MAP34[b] + local v = 0 + if b == 19 then + v = tdiv(TMP[29] + TMP[30] + TMP[31] + TMP[32], 4) + else + v = tdiv(TMP[mp[1] + 1] * mp[3] + TMP[mp[2] + 1] * mp[4], mp[3] + mp[4]) + end + out[base + b] = v + end + end + end + if iid then + st.iidPrevN = prevN + else + st.iccPrevN = prevN + end + return true +end + +-- ps_data (8.3 Table 8.1) at bit position pos; false when undecodable +function Ps.parse(st: any, buf: buffer, pos: number, count: number): boolean + SbrBits.start(buf, pos, count) + if bits(1) == 1 then + st.enableIid = bits(1) == 1 + st.iidMode = if st.enableIid then bits(3) else 0 + st.enableIcc = bits(1) == 1 + st.iccMode = if st.enableIcc then bits(3) else 0 + st.enableExt = bits(1) == 1 + if st.iidMode > 5 or st.iccMode > 5 then + st.haveConfig = false + return false + end + st.haveConfig = true + end + if not st.haveConfig then + return false + end + local class = bits(1) + local nEnv = NUM_ENV[class * 4 + bits(2) + 1] + st.nEnv = nEnv + for e = 1, nEnv do + st.border[e] = if class == 1 then math.min(bits(5), 31) else (32 * e) // nEnv - 1 + end + local iid, icc = st.iid, st.icc + if st.enableIid then + if not readKind(st, nEnv, NR_PAR[st.iidMode + 1], st.iidMode >= 3, true, iid) then + return false + end + else + st.iidPrevN = 0 + for i = 1, 4 * 20 do + iid[i] = 0 + end + end + if st.enableIcc then + if not readKind(st, nEnv, NR_PAR[st.iccMode + 1], false, false, icc) then + return false + end + else + st.iccPrevN = 0 + for i = 1, 4 * 20 do + icc[i] = 0 + end + end + if st.enableExt then + local cnt = bits(4) + if cnt == 15 then + cnt += bits(8) + end + SbrBits.skip(cnt * 8) + end + return not SbrBits.over() +end + +-- 8.6.4.6.2 mixing matrix for one envelope's parameters (Ra, or Rb for icc modes 3 .. 5) +local function mixMatrix(st: any, e: number, out: { number }) + local fine = st.iidMode >= 3 + local rb = st.iccMode >= 3 + local base = (e - 1) * 20 + for b = 1, 20 do + local ii = st.iid[base + b] + local c = if fine then IID_FINE[ii + 16] else IID_COARSE[ii + 8] + local rho = ICC_RHO[st.icc[base + b] + 1] + local h11, h12, h21, h22 + if rb then + rho = math.max(rho, 0.05) + local alpha + if math.abs(c - 1) < 1e-12 then + alpha = pi / 4 + else + alpha = 0.5 * atan(2 * c * rho / (c * c - 1)) + end + if alpha < 0 then + alpha += pi / 2 + end + local cc = c + 1 / c + local mu = 1 + (4 * rho * rho - 4) / (cc * cc) + local gamma = atan(sqrt(math.max((1 - mu) / (1 + mu), 0))) + local s2 = sqrt(2) + h11 = s2 * cos(alpha) * cos(gamma) + h12 = s2 * sin(alpha) * cos(gamma) + h21 = -s2 * sin(alpha) * sin(gamma) + h22 = s2 * cos(alpha) * sin(gamma) + else + local den = sqrt(1 + c * c) + local c1 = sqrt(2) / den + local c2 = sqrt(2) * c / den + local alpha = 0.5 * acos(math.clamp(rho, -1, 1)) + local beta = alpha * (c1 - c2) / sqrt(2) + h11 = cos(alpha + beta) * c2 + h12 = cos(beta - alpha) * c1 + h21 = sin(alpha + beta) * c2 + h22 = sin(beta - alpha) * c1 + end + local o = (b - 1) * 4 + out[o + 1], out[o + 2], out[o + 3], out[o + 4] = h11, h12, h21, h22 + end +end + +local H_TO = table.create(80, 0) +local H_FROM = table.create(80, 0) + +-- per-slot matrices hs[(n * 20 + b) * 4 + c] interpolated between envelope borders +local function fillMatrices(st: any) + local hs, hPrev = st.hs, st.hPrev + local nEnv = st.nEnv + for i = 1, 80 do + H_FROM[i] = hPrev[i] + end + local nFrom = -1 + for e = 1, nEnv do + local ne = st.border[e] + mixMatrix(st, e, H_TO) + local den = math.max(ne - nFrom, 1) + for n = nFrom + 1, math.min(ne, 31) do + local t = (n - nFrom) / den + local o = n * 80 + for i = 1, 80 do + local f = H_FROM[i] + hs[o + i] = f + (H_TO[i] - f) * t + end + end + for i = 1, 80 do + H_FROM[i] = H_TO[i] + end + if ne > nFrom then + nFrom = ne + end + end + for n = nFrom + 1, 31 do + local o = n * 80 + for i = 1, 80 do + hs[o + i] = H_FROM[i] + end + end + for i = 1, 80 do + hPrev[i] = H_FROM[i] + end +end + +-- 8.6.4.3 hybrid analysis: QMF bands 0 .. 2 of slots 0 .. 37 (6 look-ahead) -> hybrid bands 0 .. 9; bands 3 .. 63 +-- pass through as hybrid bands 10 .. 70 +local BR = table.create(44, 0) +local BI = table.create(44, 0) +local function hybrid(st: any, xr: { number }, xi: { number }) + local sr, si, hist = st.sr, st.si, st.hist + for n = 0, 31 do + local o, x = n * 71, n * 64 + for k = 0, 9 do + sr[o + k + 1], si[o + k + 1] = 0, 0 + end + for k = 3, 63 do + sr[o + k + 8], si[o + k + 8] = xr[x + k + 1], xi[x + k + 1] + end + end + for p = 0, 2 do + for j = 0, 5 do + BR[j + 1], BI[j + 1] = hist[(p * 6 + j) * 2 + 1], hist[(p * 6 + j) * 2 + 2] + end + for j = 0, 37 do + BR[j + 7], BI[j + 7] = xr[j * 64 + p + 1], xi[j * 64 + p + 1] + end + if p == 0 then + for q = 0, 7 do + local to = Q0_TO[q + 1] + 1 + local hb = q * 13 + for n = 0, 31 do + local ar, ai = 0, 0 + for m = 0, 12 do + local cr, ci = H0R[hb + m + 1], H0I[hb + m + 1] + local vr, vi = BR[n + 13 - m], BI[n + 13 - m] + ar += cr * vr - ci * vi + ai += cr * vi + ci * vr + end + local o = n * 71 + to + sr[o] += ar + si[o] += ai + end + end + else + -- type B, Q = 2: q0 = g * x (low pass), q1 = g * (-1)^(m - 6) x (high pass) + local lowTo = if p == 1 then 8 else 9 + local highTo = if p == 1 then 7 else 10 + for n = 0, 31 do + local er, ei = BR[n + 7] * 0.5, BI[n + 7] * 0.5 + local orr, oi = 0, 0 + for m = 1, 11, 2 do + local g = G12[m + 1] + orr += g * BR[n + 13 - m] + oi += g * BI[n + 13 - m] + end + local o = n * 71 + sr[o + lowTo], si[o + lowTo] = er + orr, ei + oi + sr[o + highTo], si[o + highTo] = er - orr, ei - oi + end + end + for j = 0, 5 do + hist[(p * 6 + j) * 2 + 1], hist[(p * 6 + j) * 2 + 2] = xr[(26 + j) * 64 + p + 1], xi[(26 + j) * 64 + p + 1] + end + end +end + +local PW = table.create(20, 0) +local GR = table.create(20, 0) + +-- 8.6.4.5 decorrelation of the hybrid rows +local function decorrelate(st: any) + local sr, si, dr, di = st.sr, st.si, st.dr, st.di + local peak, smooth, sdiff = st.peak, st.smooth, st.smoothDiff + local apIn, apW, apPos, dl, dlPos = st.apIn, st.apW, st.apPos, st.dl, st.dlPos + for n = 0, 31 do + local o = n * 71 + for i = 1, 20 do + PW[i] = 0 + end + for k = 1, 71 do + local r, im = sr[o + k], si[o + k] + local b = B_K[k] + 1 + PW[b] += r * r + im * im + end + for i = 1, 20 do + local p = PW[i] + local pk = PEAK_DECAY * peak[i] + if pk < p then + pk = p + end + peak[i] = pk + smooth[i] += A_SMOOTH * (p - smooth[i]) + sdiff[i] += A_SMOOTH * (pk - p - sdiff[i]) + GR[i] = if GAMMA * sdiff[i] > smooth[i] then smooth[i] / (GAMMA * sdiff[i]) else 1 + end + for k = 0, NR_AP - 1 do + local a4 = k * 4 + local ur, ui = apIn[a4 + 3], apIn[a4 + 4] + apIn[a4 + 3], apIn[a4 + 4] = apIn[a4 + 1], apIn[a4 + 2] + apIn[a4 + 1], apIn[a4 + 2] = sr[o + k + 1], si[o + k + 1] + local pr, pi2 = PHI_R[k + 1], PHI_I[k + 1] + ur, ui = pr * ur - pi2 * ui, pr * ui + pi2 * ur + local g = G_DECAY[k + 1] + for m = 0, 2 do + local coef = AP_A[m + 1] * g + local qi = k * 3 + m + 1 + local qr, qim = QF_R[qi], QF_I[qi] + local pos = apPos[qi] + local w = ((k * 3 + m) * 5 + pos) * 2 + local wr, wi = apW[w + 1], apW[w + 2] + local tr, ti = qr * wr - qim * wi, qr * wi + qim * wr + local nr, ni = ur + tr * coef, ui + ti * coef + ur, ui = tr - nr * coef, ti - ni * coef + apW[w + 1], apW[w + 2] = nr, ni + pos += 1 + if pos >= AP_D[m + 1] then + pos = 0 + end + apPos[qi] = pos + end + local gr = GR[B_K[k + 1] + 1] + dr[o + k + 1], di[o + k + 1] = ur * gr, ui * gr + end + for k = NR_AP, 70 do + local i = k - NR_AP + local len = if k < 42 then 14 else 1 + local pos = dlPos[i + 1] + local w = (i * 14 + pos) * 2 + local vr, vi = dl[w + 1], dl[w + 2] + dl[w + 1], dl[w + 2] = sr[o + k + 1], si[o + k + 1] + pos += 1 + if pos >= len then + pos = 0 + end + dlPos[i + 1] = pos + local gr = GR[B_K[k + 1] + 1] + dr[o + k + 1], di[o + k + 1] = vr * gr, vi * gr + end + end +end + +local function clear(t: { number }) + for i = 1, #t do + t[i] = 0 + end +end + +-- one frame: X (38 slots x 64 bands, [n * 64 + k + 1]) -> L / R QMF slots (32 x 64) in lr / li / rr / ri +function Ps.process(st: any, had: boolean, xr: { number }, xi: { number }, lr: { number }, li: { number }, rr: { number }, ri: { number }) + if not st.prevHadPs then + clear(st.apIn) + clear(st.apW) + clear(st.apPos) + clear(st.dl) + clear(st.dlPos) + end + if not had then + st.nEnv = 0 + end + fillMatrices(st) + hybrid(st, xr, xi) + decorrelate(st) + local sr, si, dr, di, hs = st.sr, st.si, st.dr, st.di, st.hs + for n = 0, 31 do + local o, x, hb = n * 71, n * 64, n * 80 + local l0r, l0i, r0r, r0i = 0, 0, 0, 0 + local l1r, l1i, r1r, r1i = 0, 0, 0, 0 + local l2r, l2i, r2r, r2i = 0, 0, 0, 0 + for k = 1, 71 do + local h = hb + B_K[k] * 4 + local h11, h12, h21, h22 = hs[h + 1], hs[h + 2], hs[h + 3], hs[h + 4] + local s_r, s_i, d_r, d_i = sr[o + k], si[o + k], dr[o + k], di[o + k] + local Lr, Li = h11 * s_r + h21 * d_r, h11 * s_i + h21 * d_i + local Rr, Ri = h12 * s_r + h22 * d_r, h12 * s_i + h22 * d_i + if k <= 6 then + l0r += Lr + l0i += Li + r0r += Rr + r0i += Ri + elseif k <= 8 then + l1r += Lr + l1i += Li + r1r += Rr + r1i += Ri + elseif k <= 10 then + l2r += Lr + l2i += Li + r2r += Rr + r2i += Ri + else + lr[x + k - 7], li[x + k - 7] = Lr, Li + rr[x + k - 7], ri[x + k - 7] = Rr, Ri + end + end + lr[x + 1], li[x + 1], rr[x + 1], ri[x + 1] = l0r, l0i, r0r, r0i + lr[x + 2], li[x + 2], rr[x + 2], ri[x + 2] = l1r, l1i, r1r, r1i + lr[x + 3], li[x + 3], rr[x + 3], ri[x + 3] = l2r, l2i, r2r, r2i + end + st.prevHadPs = had +end + +return Ps diff --git a/src/Rosetta/codecs/audio/Aac/Sbr.luau b/src/Rosetta/codecs/audio/Aac/Sbr.luau new file mode 100644 index 0000000..c173ebf --- /dev/null +++ b/src/Rosetta/codecs/audio/Aac/Sbr.luau @@ -0,0 +1,1069 @@ +--!strict +--!native +--!optimize 2 +-- codecs/audio/Aac/Sbr: Spectral Band Replication bitstream and control (ISO/IEC 14496-3 4.6.18, syntax 4.4.2.8). +-- Ported from syom 0.8.1 src/engine/sbr_*.rs (MIT, licenses/syom-LICENSE.txt). Supports: sbr_header, sbr_data for +-- SCE / CPE (coupled and independent), envelope / noise delta decoding and dequantisation, frequency band tables, +-- patches and limiter bands; ps_data is handed to codecs/audio/Aac/Ps. Not supported: down-sampled SBR, 960 frames. +local SbrTables = require("@rosetta/codecs/audio/Aac/SbrTables") + +local SbrBits = require("@rosetta/codecs/audio/Aac/SbrBits") +local SbrQmf = require("@rosetta/codecs/audio/Aac/SbrQmf") +local SbrHf = require("@rosetta/codecs/audio/Aac/SbrHf") +local Ps = require("@rosetta/codecs/audio/Aac/Ps") + +local floor, log = math.floor, math.log +local bits, huff, codebook = SbrBits.bits, SbrBits.huff, SbrBits.codebook + +local Sbr = {} + +local T_ENV15 = codebook(SbrTables.T_ENV15) +local F_ENV15 = codebook(SbrTables.F_ENV15) +local T_BAL15 = codebook(SbrTables.T_BAL15) +local F_BAL15 = codebook(SbrTables.F_BAL15) +local T_ENV30 = codebook(SbrTables.T_ENV30) +local F_ENV30 = codebook(SbrTables.F_ENV30) +local T_BAL30 = codebook(SbrTables.T_BAL30) +local F_BAL30 = codebook(SbrTables.F_BAL30) +local T_NOISE30 = codebook(SbrTables.T_NOISE30) +local T_NOISEBAL30 = codebook(SbrTables.T_NOISEBAL30) + +-- ------------------------------------------------------------------------------------------------ band tables +local function nint(x: number): number + return floor(x + 0.5) +end + +-- 4.6.18.3.2 Table 4.82 startMin offsets by the SBR rate +local OFFSETS = { + { -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 }, + { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13 }, + { -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 }, + { -6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 }, + { -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20 }, + { -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24 }, +} + +local function sortInts(t: { number }, n: number) + for i = 2, n do + local v = t[i] + local j = i - 1 + while j >= 1 and t[j] > v do + t[j + 1] = t[j] + j -= 1 + end + t[j + 1] = v + end +end + +-- vDk(i) = NINT(lo * r^((i + 1) / n)) - NINT(lo * r^(i / n)), sorted +local function warpSteps(lo: number, hi: number, n: number): { number } + local r = hi / lo + local d = table.create(n, 0) + for i = 0, n - 1 do + d[i + 1] = nint(lo * r ^ ((i + 1) / n)) - nint(lo * r ^ (i / n)) + end + sortInts(d, n) + return d +end + +-- 4.6.18.3.2: fMaster from k0, k2, bs_freq_scale, bs_alter_scale (nil when invalid) +local function masterTable(k0: number, k2: number, freqScale: number, alterScale: boolean): { number }? + if k2 <= k0 then + return nil + end + local f = { k0 } + if freqScale == 0 then + local dk = if alterScale then 2 else 1 + local nb = if alterScale then 2 * nint((k2 - k0) / 4) else 2 * ((k2 - k0) // 2) + if nb <= 0 then + return nil + end + local d = table.create(nb, dk) + local diff = k2 - (k0 + nb * dk) + local k = if diff < 0 then 1 else nb + local incr = if diff < 0 then 1 else -1 + local guard = 0 + while diff ~= 0 do + if k < 1 or k > nb or guard > 64 then + return nil + end + d[k] -= incr + k += incr + diff += incr + guard += 1 + end + for i = 1, nb do + if d[i] <= 0 then + return nil + end + f[i + 1] = f[i] + d[i] + end + return f + end + local bandsPerOct = ({ 12, 10, 8 })[freqScale] or 8 + local warp = if alterScale then 1.3 else 1 + local twoRegions = k2 / k0 > 2.2449 + local k1 = if twoRegions then 2 * k0 else k2 + local nb0 = 2 * nint(bandsPerOct * log(k1 / k0) / (2 * log(2))) + if nb0 <= 0 or nb0 > 64 then + return nil + end + local d0 = warpSteps(k0, k1, nb0) + local maxD0 = 0 + for i = 1, nb0 do + if d0[i] <= 0 then + return nil + end + maxD0 = math.max(maxD0, d0[i]) + f[i + 1] = f[i] + d0[i] + end + if not twoRegions then + return f + end + local nb1 = 2 * nint(bandsPerOct * log(k2 / k1) / (2 * log(2) * warp)) + if nb1 <= 0 or nb1 > 64 then + return nil + end + local d1 = warpSteps(k1, k2, nb1) + if d1[1] < maxD0 then + local change = math.min(maxD0 - d1[1], (d1[nb1] - d1[1]) // 2) + d1[1] += change + d1[nb1] -= change + sortInts(d1, nb1) + end + local base = #f + for i = 1, nb1 do + if d1[i] <= 0 then + return nil + end + f[base + i] = f[base + i - 1] + d1[i] + end + return f +end + +-- 4.6.18.6.3 patch construction: patchNum / patchStart per patch (nil when invalid) +local function patches(e: any, k0: number): boolean + local master, kx, m = e.fMaster, e.kx, e.M + local nMaster = #master - 1 + local msb, usb = k0, kx + local goal = nint(2.048e6 / e.fs) + local k = nMaster + if goal < kx + m then + k = 0 + local i = 0 + while master[i + 1] < goal do + i += 1 + k = i + end + end + local num, start = {}, {} + local sb = 0 + local guard = 0 + repeat + guard += 1 + if guard > 64 then + return false + end + local j = k + 1 + local odd = 0 + repeat + j -= 1 + if j < 0 then + return false + end + sb = master[j + 1] + odd = (sb - 2 + k0) % 2 + until sb <= k0 - 1 + msb - odd + local n = math.max(sb - usb, 0) + if n > 0 then + local s = k0 - odd - n + if s < 0 or #num >= 5 or s + n > 32 then + return false + end + table.insert(num, n) + table.insert(start, s) + usb, msb = sb, sb + else + msb = kx + end + if master[k + 1] - sb < 3 then + k = nMaster + end + until sb == kx + m + if #num > 1 and num[#num] < 3 then + table.remove(num) + table.remove(start) + end + if #num == 0 then + return false + end + e.patchNum, e.patchStart = num, start + return true +end + +local LIM_BANDS = { 1.2, 2, 3 } +local function limiterTable(e: any, limBands: number) + local low = e.fLow + local n = #low + if limBands == 0 then + e.fLim = { low[1], low[n] } + return + end + local per = LIM_BANDS[limBands] + local borders = { e.kx } + for i, p in e.patchNum do + borders[i + 1] = borders[i] + p + end + local function isBorder(v: number): boolean + for _, b in borders do + if b == v then + return true + end + end + return false + end + local t = table.clone(low) + for i = 2, #borders - 1 do + table.insert(t, borders[i]) + end + sortInts(t, #t) + local k = 2 + while k <= #t do + local a, b = t[k - 1], t[k] + if a < 1 then + k += 1 + elseif log(b / a) / log(2) * per < 0.49 then + if b == a or not isBorder(b) then + table.remove(t, k) + elseif not isBorder(a) then + table.remove(t, k - 1) + else + k += 1 + end + else + k += 1 + end + end + e.fLim = t +end + +local function rateRow(fs: number): { number } + if fs < 22050 then + return OFFSETS[1] + elseif fs < 24000 then + return OFFSETS[2] + elseif fs < 32000 then + return OFFSETS[3] + elseif fs < 44100 then + return OFFSETS[4] + elseif fs <= 64000 then + return OFFSETS[5] + end + return OFFSETS[6] +end + +-- band index of QMF subband k in table f (0-based), or -1 +local function bandOf(f: { number }, k: number): number + for i = 1, #f - 1 do + if f[i] <= k and k < f[i + 1] then + return i - 1 + end + end + return -1 +end + +-- 4.6.18.3.2 - 4.6.18.3.6: every table derived from the header; false when the header is unusable +local function deriveTables(e: any, h: any): boolean + local fs = e.fs + local cMin = if fs < 32000 then 3000 elseif fs < 64000 then 4000 else 5000 + local sMin = if fs < 32000 then 6000 elseif fs < 64000 then 8000 else 10000 + local startMin = nint(cMin * 128 / fs) + local stopMin = nint(sMin * 128 / fs) + local k0 = startMin + rateRow(fs)[h.startFreq + 1] + local k2: number + if h.stopFreq == 14 then + k2 = math.min(2 * k0, 64) + elseif h.stopFreq == 15 then + k2 = math.min(3 * k0, 64) + else + local d = table.create(13, 0) + local r = 64 / stopMin + for p = 0, 12 do + d[p + 1] = nint(stopMin * r ^ ((p + 1) / 13)) - nint(stopMin * r ^ (p / 13)) + end + sortInts(d, 13) + k2 = stopMin + for p = 1, h.stopFreq do + k2 += d[p] + end + k2 = math.min(k2, 64) + end + if k0 < 1 or k0 >= 32 or k2 <= k0 or k2 - k0 > 48 then + return false + end + local master = masterTable(k0, k2, h.freqScale, h.alterScale) + if not master then + return false + end + local nMaster = #master - 1 + if h.xover >= nMaster then + return false + end + local high = {} + for i = h.xover, nMaster do + table.insert(high, master[i + 1]) + end + local nHigh = #high - 1 + local kx = high[1] + local m = high[nHigh + 1] - kx + if kx > 32 or kx + m > 64 or m <= 0 or nHigh > 48 then + return false + end + local nLow = nHigh // 2 + nHigh % 2 + local low = { high[1] } + local parity = nHigh % 2 + for k = 1, nLow do + low[k + 1] = high[2 * k - parity + 1] + end + local nQ = 1 + if h.noiseBands > 0 then + nQ = math.max(1, nint(h.noiseBands * log(low[nLow + 1] / kx) / log(2))) + end + if nQ > 5 then + return false + end + local noise = { low[1] } + local ip = 0 + for k = 1, nQ do + ip += (nLow - ip) // (nQ + 1 - k) + local v = low[ip + 1] + if not v then + return false + end + noise[k + 1] = v + end + e.fMaster, e.fHigh, e.fLow, e.fNoise = master, high, low, noise + e.kx, e.M, e.nHigh, e.nLow, e.nQ = kx, m, nHigh, nLow, nQ + if not patches(e, k0) then + return false + end + limiterTable(e, h.limiterBands) + -- per QMF subband k = kx + m: envelope / noise / limiter band (0-based) + local mapH, mapL, mapQ = table.create(m, 0), table.create(m, 0), table.create(m, 0) + for i = 0, m - 1 do + mapH[i + 1] = bandOf(high, kx + i) + mapL[i + 1] = bandOf(low, kx + i) + mapQ[i + 1] = bandOf(noise, kx + i) + if mapH[i + 1] < 0 or mapL[i + 1] < 0 or mapQ[i + 1] < 0 then + return false + end + end + e.mapH, e.mapL, e.mapQ = mapH, mapL, mapQ + -- the noise band of each patched target subband, for the chirp factor + return true +end + +-- ------------------------------------------------------------------------------------------------ sbr_data +local function grid(c: any): boolean + local class = bits(2) + local nEnv = 1 + local fr = c.freqRes + local rel0, rel1 = c.rel0, c.rel1 + local nRel0, nRel1 = 0, 0 + local vb0, vb1, ptr = 0, 0, 0 + c.ampOverride = false + if class == 0 then + nEnv = 2 ^ bits(2) + if nEnv > 4 then + return false + end + local r = bits(1) + for i = 1, nEnv do + fr[i] = r + end + if nEnv == 1 then + c.ampOverride = true + end + nRel0 = nEnv - 1 + elseif class == 1 then + vb1 = bits(2) + nRel1 = bits(2) + nEnv = nRel1 + 1 + for i = 1, nRel1 do + rel1[i] = 2 * bits(2) + 2 + end + ptr = bits(if nEnv + 1 <= 2 then 1 elseif nEnv + 1 <= 4 then 2 else 3) + for i = nEnv, 1, -1 do + fr[i] = bits(1) + end + elseif class == 2 then + vb0 = bits(2) + nRel0 = bits(2) + nEnv = nRel0 + 1 + for i = 1, nRel0 do + rel0[i] = 2 * bits(2) + 2 + end + ptr = bits(if nEnv + 1 <= 2 then 1 elseif nEnv + 1 <= 4 then 2 else 3) + for i = 1, nEnv do + fr[i] = bits(1) + end + else + vb0 = bits(2) + vb1 = bits(2) + nRel0 = bits(2) + nRel1 = bits(2) + nEnv = nRel0 + nRel1 + 1 + if nEnv > 5 then + return false + end + for i = 1, nRel0 do + rel0[i] = 2 * bits(2) + 2 + end + for i = 1, nRel1 do + rel1[i] = 2 * bits(2) + 2 + end + ptr = bits(if nEnv + 1 <= 2 then 1 elseif nEnv + 1 <= 4 then 2 elseif nEnv + 1 <= 8 then 3 else 3) + for i = 1, nEnv do + fr[i] = bits(1) + end + end + c.class, c.nEnv, c.nNoise, c.pointer = class, nEnv, if nEnv > 1 then 2 else 1, ptr + -- 4.6.18.3.3 time borders (in time slots) + local tE, tQ = c.tE, c.tQ + local lead = if class >= 2 then vb0 else 0 + local trail = if class == 1 or class == 3 then vb1 + 16 else 16 + tE[1] = lead + tE[nEnv + 1] = trail + if class == 0 then + local step = (2 * 16 + nEnv) // (2 * nEnv) + for l = 1, nEnv - 1 do + tE[l + 1] = lead + l * step + end + else + local b = lead + for l = 1, nRel0 do + b += rel0[l] + tE[l + 1] = b + end + b = trail + for l = 1, nRel1 do + b -= rel1[l] + tE[nEnv + 1 - l] = b + end + end + for l = 1, nEnv do + if tE[l + 1] <= tE[l] then + return false + end + end + if nEnv == 1 then + tQ[1], tQ[2] = tE[1], tE[2] + else + local mid + if class == 0 then + mid = nEnv // 2 + elseif class == 2 then + mid = if ptr == 0 then 1 elseif ptr == 1 then nEnv - 1 else ptr - 1 + else + mid = if ptr <= 1 then nEnv - 1 else nEnv + 1 - ptr + end + if mid <= 0 or mid >= nEnv then + return false + end + tQ[1], tQ[2], tQ[3] = tE[1], tE[mid + 1], tE[nEnv + 1] + end + local lA = -1 + if class == 1 or class == 3 then + if ptr > 0 then + lA = nEnv + 1 - ptr + end + elseif class == 2 then + if ptr > 1 then + lA = ptr - 1 + end + end + c.lA = lA + return true +end + +local function dtdf(c: any) + for i = 1, c.nEnv do + c.dfEnv[i] = bits(1) + end + for i = 1, c.nNoise do + c.dfNoise[i] = bits(1) + end +end + +local function invf(c: any, nQ: number) + for i = 1, nQ do + c.invf[i] = bits(2) + end +end + +-- sbr_envelope: raw deltas into c.envRaw[(l - 1) * 64 + i] +local function envelope(e: any, c: any, bal: boolean): boolean + local amp = e.ampRes == 1 and not c.ampOverride + c.amp = amp + local tb, fb + if bal then + tb, fb = if amp then T_BAL30 else T_BAL15, if amp then F_BAL30 else F_BAL15 + else + tb, fb = if amp then T_ENV30 else T_ENV15, if amp then F_ENV30 else F_ENV15 + end + local startBits = if bal then (if amp then 5 else 6) else (if amp then 6 else 7) + local raw = c.envRaw + for l = 1, c.nEnv do + local n = if c.freqRes[l] == 1 then e.nHigh else e.nLow + local base = (l - 1) * 64 + if c.dfEnv[l] == 0 then + raw[base + 1] = bits(startBits) + for i = 2, n do + local v = huff(fb) + if not v then + return false + end + raw[base + i] = v + end + else + for i = 1, n do + local v = huff(tb) + if not v then + return false + end + raw[base + i] = v + end + end + end + return true +end + +local function noise(e: any, c: any, bal: boolean): boolean + local tb = if bal then T_NOISEBAL30 else T_NOISE30 + local fb = if bal then F_BAL30 else F_ENV30 + local raw = c.noiseRaw + for l = 1, c.nNoise do + local base = (l - 1) * 8 + if c.dfNoise[l] == 0 then + raw[base + 1] = bits(5) + for i = 2, e.nQ do + local v = huff(fb) + if not v then + return false + end + raw[base + i] = v + end + else + for i = 1, e.nQ do + local v = huff(tb) + if not v then + return false + end + raw[base + i] = v + end + end + end + return true +end + +local function harmonics(e: any, c: any) + local h = c.harm + if bits(1) == 1 then + c.hasHarm = true + for i = 1, e.nHigh do + h[i] = bits(1) + end + else + c.hasHarm = false + end +end + +-- sbr_extension: ps_data (bs_extension_id 2) is recorded for the PS tool +local function extended(e: any) + e.psPos, e.psBits = -1, 0 + if bits(1) == 0 then + return + end + local cnt = bits(4) + if cnt == 15 then + cnt += bits(8) + end + local left = cnt * 8 + while left > 7 do + local id = bits(2) + left -= 2 + if id == 2 and e.psPos < 0 then + e.psPos, e.psBits = SbrBits.pos(), left + end + SbrBits.skip(left) + left = 0 + end + SbrBits.skip(left) +end + +local function newChannel(): any + return { + freqRes = table.create(6, 0), + rel0 = table.create(4, 0), + rel1 = table.create(4, 0), + tE = table.create(6, 0), + tQ = table.create(3, 0), + dfEnv = table.create(5, 0), + dfNoise = table.create(2, 0), + invf = table.create(5, 0), + envRaw = table.create(5 * 64, 0), + noiseRaw = table.create(16, 0), + harm = table.create(64, 0), + hasHarm = false, + class = 0, + nEnv = 0, + nNoise = 0, + pointer = 0, + lA = -1, + amp = false, + ampOverride = false, + -- decoded (persist across frames for the time-direction deltas) + env = table.create(5 * 64, 0), + envLast = table.create(64, 0), + envLastRes = -1, + noiseQ = table.create(16, 0), + noiseLast = table.create(8, 0), + noiseLastValid = false, + eOrig = table.create(5 * 64, 0), + qOrig = table.create(16, 0), + } +end +Sbr.newChannel = newChannel + +local HEADER_KEYS = { "startFreq", "stopFreq", "xover", "freqScale", "alterScale", "noiseBands", "limiterBands" } + +-- sbr_extension_data (4.4.2.8 Table 4.62) at bit position pos (after the extension_type nibble); true when parsed +function Sbr.parse(e: any, buf: buffer, pos: number, count: number, crc: boolean): boolean + SbrBits.start(buf, pos, count) + if crc then + bits(10) + end + local h = e.hdrNext + if bits(1) == 1 then + e.ampRes = bits(1) + h.startFreq = bits(4) + h.stopFreq = bits(4) + h.xover = bits(3) + bits(2) + local x1, x2 = bits(1), bits(1) + if x1 == 1 then + h.freqScale = bits(2) + h.alterScale = bits(1) == 1 + h.noiseBands = bits(2) + else + h.freqScale, h.alterScale, h.noiseBands = 2, true, 2 + end + if x2 == 1 then + h.limiterBands = bits(2) + h.limiterGains = bits(2) + h.interpolFreq = bits(1) == 1 + h.smoothingMode = bits(1) == 1 + else + h.limiterBands, h.limiterGains, h.interpolFreq, h.smoothingMode = 2, 2, true, true + end + if SbrBits.over() then + return false + end + local hc = e.hdr + local changed = not e.tablesOk + for _, k in HEADER_KEYS do + if hc[k] ~= h[k] then + changed = true + hc[k] = h[k] + end + end + hc.limiterGains, hc.interpolFreq, hc.smoothingMode = h.limiterGains, h.interpolFreq, h.smoothingMode + if changed then + e.tablesOk = deriveTables(e, hc) + e.reset = true + end + end + if not e.tablesOk then + return false + end + local nQ = e.nQ + if e.nch == 1 then + local c = e.ch[1] + if bits(1) == 1 then + bits(4) + end + if not grid(c) then + return false + end + dtdf(c) + invf(c, nQ) + if not envelope(e, c, false) or not noise(e, c, false) then + return false + end + harmonics(e, c) + e.coupling = false + else + local a, b = e.ch[1], e.ch[2] + if bits(1) == 1 then + bits(8) + end + local coupling = bits(1) == 1 + e.coupling = coupling + if coupling then + if not grid(a) then + return false + end + for _, k in { "class", "nEnv", "nNoise", "pointer", "lA", "ampOverride" } do + b[k] = a[k] + end + for i = 1, 6 do + b.freqRes[i], b.tE[i] = a.freqRes[i], a.tE[i] + end + for i = 1, 3 do + b.tQ[i] = a.tQ[i] + end + dtdf(a) + dtdf(b) + invf(a, nQ) + for i = 1, nQ do + b.invf[i] = a.invf[i] + end + if not envelope(e, a, false) or not noise(e, a, false) then + return false + end + if not envelope(e, b, true) or not noise(e, b, true) then + return false + end + else + if not grid(a) or not grid(b) then + return false + end + dtdf(a) + dtdf(b) + invf(a, nQ) + invf(b, nQ) + if not envelope(e, a, false) or not envelope(e, b, false) then + return false + end + if not noise(e, a, false) or not noise(e, b, false) then + return false + end + end + harmonics(e, a) + harmonics(e, b) + end + extended(e) + return not SbrBits.over() +end + +-- ------------------------------------------------------------------------------------------------ decoding +-- 4.6.18.3.5 delta decoding of one channel's envelopes and noise floors +local function decodeChannel(e: any, c: any, bal: boolean) + local delta = if bal then 2 else 1 + local raw, env, last = c.envRaw, c.env, c.envLast + local fHigh, fLow = e.fHigh, e.fLow + local lastRes = c.envLastRes + for l = 1, c.nEnv do + local res = c.freqRes[l] + local n = if res == 1 then e.nHigh else e.nLow + local base = (l - 1) * 64 + if c.dfEnv[l] == 0 then + local acc = 0 + for i = 1, n do + acc += raw[base + i] * delta + env[base + i] = acc + end + else + local prevBase, prev = (l - 2) * 64, env + local prevRes = if l > 1 then c.freqRes[l - 1] else lastRes + if l == 1 then + prevBase, prev = 0, last + end + for i = 1, n do + local g = 0 + if prevRes == res then + g = prev[prevBase + i] or 0 + elseif prevRes >= 0 then + local idx + if res == 1 then + -- high band -> the low band containing it + local target = fHigh[i] + idx = 1 + while fLow[idx + 1] and fLow[idx + 1] <= target do + idx += 1 + end + if idx > e.nLow then + idx = e.nLow + end + else + local target = fLow[i] + idx = 1 + for j = 1, e.nHigh + 1 do + if fHigh[j] == target then + idx = j + break + end + end + end + g = prev[prevBase + idx] or 0 + end + env[base + i] = g + raw[base + i] * delta + end + end + end + local lb = (c.nEnv - 1) * 64 + for i = 1, 64 do + last[i] = env[lb + i] + end + c.envLastRes = c.freqRes[c.nEnv] + local nq = e.nQ + local nr, q, ql = c.noiseRaw, c.noiseQ, c.noiseLast + for l = 1, c.nNoise do + local base = (l - 1) * 8 + if c.dfNoise[l] == 0 then + local acc = 0 + for i = 1, nq do + acc += nr[base + i] * delta + q[base + i] = acc + end + else + for i = 1, nq do + local g = if l > 1 then q[base - 8 + i] elseif c.noiseLastValid then ql[i] else 0 + q[base + i] = g + nr[base + i] * delta + end + end + end + local qb = (c.nNoise - 1) * 8 + for i = 1, 8 do + ql[i] = q[qb + i] + end + c.noiseLastValid = true +end + +-- 4.6.18.3.5 dequantisation (E_orig, Q_orig), single or coupled +local function dequant(e: any) + local a, b = e.ch[1], e.ch[2] + if e.coupling and b then + local div = if a.amp then 1 else 2 + local pan = if a.amp then 12 else 24 + for l = 1, a.nEnv do + local n = if a.freqRes[l] == 1 then e.nHigh else e.nLow + local base = (l - 1) * 64 + for i = 1, n do + local avg2 = 64 * 2 ^ (a.env[base + i] / div + 1) + local ratio = 2 ^ ((pan - b.env[base + i]) / div) + a.eOrig[base + i] = avg2 / (1 + ratio) + b.eOrig[base + i] = avg2 / (1 + 1 / ratio) + end + end + for l = 1, a.nNoise do + local base = (l - 1) * 8 + for i = 1, e.nQ do + local avg2 = 2 ^ (6 - a.noiseQ[base + i] + 1) + local ratio = 2 ^ (12 - b.noiseQ[base + i]) + a.qOrig[base + i] = avg2 / (1 + ratio) + b.qOrig[base + i] = avg2 / (1 + 1 / ratio) + end + end + return + end + for _, c in e.ch do + local div = if c.amp then 1 else 2 + for l = 1, c.nEnv do + local n = if c.freqRes[l] == 1 then e.nHigh else e.nLow + local base = (l - 1) * 64 + for i = 1, n do + c.eOrig[base + i] = 64 * 2 ^ (c.env[base + i] / div) + end + end + for l = 1, c.nNoise do + local base = (l - 1) * 8 + for i = 1, e.nQ do + c.qOrig[base + i] = 2 ^ (6 - c.noiseQ[base + i]) + end + end + end +end + +-- after a successful parse: deltas, dequantisation +function Sbr.decode(e: any) + if e.reset then + for _, c in e.ch do + c.envLastRes = -1 + c.noiseLastValid = false + end + end + for i, c in e.ch do + decodeChannel(e, c, e.coupling and i == 2) + end + dequant(e) +end + +-- an SBR element: nch 1 (SCE / LFE) or 2 (CPE), fs = the SBR (output) rate; ps: a mono element output as stereo +function Sbr.element(nch: number, fs: number, ps: boolean?): any + local ch = {} + for i = 1, nch do + local c = newChannel() + c.q = SbrQmf.state() + c.h = SbrHf.state() + ch[i] = c + end + return { + nch = nch, + fs = fs, + ch = ch, + hdr = {}, + hdrNext = {}, + ampRes = 0, + tablesOk = false, + reset = true, + coupling = false, + present = false, + psPos = -1, + psBits = 0, + psOut = nch == 1 and ps == true, + ps = if nch == 1 and ps then Ps.state() else nil, + psq = if nch == 1 and ps then SbrQmf.state() else nil, + psHad = false, + kx = 32, + M = 0, + nHigh = 0, + nLow = 0, + nQ = 0, + } +end + +-- a seek: filterbank, HF and delta-coding state restart; the header and its tables are kept +function Sbr.restart(e: any) + for _, c in e.ch do + SbrQmf.reset(c.q) + c.h = SbrHf.state() + c.envLastRes = -1 + c.noiseLastValid = false + end + if e.ps then + local old = e.ps + local ps = Ps.state() + for _, k in { "haveConfig", "enableIid", "iidMode", "enableIcc", "iccMode", "enableExt" } do + ps[k] = old[k] + end + e.ps = ps + SbrQmf.reset(e.psq) + end + e.reset = true + e.present = false +end + +-- an EXT_SBR_DATA(_CRC) payload for this element (bit position after the extension_type nibble) +function Sbr.extension(e: any, buf: buffer, pos: number, count: number, crc: boolean) + e.present = Sbr.parse(e, buf, pos, count, crc) + e.psHad = false + if e.present and e.ps and e.psPos >= 0 then + local stop = math.min(e.psPos + e.psBits, pos + count) + e.psHad = Ps.parse(e.ps, buf, e.psPos, stop - e.psPos) + end +end + +local SR = table.create(64, 0) +local SI = table.create(64, 0) +local XR = table.create(38 * 64, 0) +local XI = table.create(38 * 64, 0) +local LR = table.create(32 * 64, 0) +local LI = table.create(32 * 64, 0) +local RR = table.create(32 * 64, 0) +local RI = table.create(32 * 64, 0) + +-- 4.6.18.5 the QMF matrix of slot l (0 .. 31): X_low below kx, Y from kx to kx + M (the previous frame's Y up to lTemp) +local function slot(e: any, c: any, ok: boolean, lTemp: number, l: number, xr: { number }, xi: { number }, xo: number) + local q, h = c.q, c.h + local XLr, XLi = q.XLr, q.XLi + local kx, m = 32, 0 + local yr, yi, col = h.Yr, h.Yi, l + 2 + if l < lTemp then + kx, m, yr, yi, col = h.kxPrev, h.mPrev, h.Pr, h.Pi, l + 34 + elseif ok then + kx, m = e.kx, e.M + end + local lo = (l + 2) * 32 + for k = 1, kx do + xr[xo + k], xi[xo + k] = XLr[lo + k], XLi[lo + k] + end + local yo = col * 64 + for k = kx + 1, kx + m do + xr[xo + k], xi[xo + k] = yr[yo + k], yi[yo + k] + end + for k = kx + m + 1, 64 do + xr[xo + k], xi[xo + k] = 0, 0 + end +end + +-- one frame of an element: core[ci] = 1024 core samples -> 2048 samples per output channel at out[offs[i] + j * stride] +function Sbr.process(e: any, core: { { number } }, out: { number }, stride: number, offs: { number }) + local ok = e.present + if ok then + Sbr.decode(e) + end + for ci = 1, e.nch do + local c = e.ch[ci] + local q, h = c.q, c.h + SbrQmf.analysis(q, core[ci], 1, 1) + if ok then + SbrHf.generate(e, c, h, q.XLr, q.XLi) + SbrHf.adjust(e, c, e.hdr, h) + end + local lTemp = math.max(2 * h.tEPrev - 32, 0) + local o = offs[ci] + if e.psOut and e.ps then + for l = 0, 31 do + slot(e, c, ok, lTemp, l, XR, XI, l * 64) + end + local XLr, XLi = q.XLr, q.XLi + for l = 32, 37 do + local lo, xo = (l + 2) * 32, l * 64 + for k = 1, 3 do + XR[xo + k], XI[xo + k] = XLr[lo + k], XLi[lo + k] + end + end + local ps = e.ps + if ps.haveConfig then + Ps.process(ps, ok and e.psHad, XR, XI, LR, LI, RR, RI) + local o2 = offs[2] + for l = 0, 31 do + SbrQmf.synthesis(q, l, LR, LI, l * 64, out, o + l * 64 * stride, stride) + SbrQmf.synthesis(e.psq, l, RR, RI, l * 64, out, o2 + l * 64 * stride, stride) + end + SbrQmf.endFrame(e.psq) + else + local o2 = offs[2] + for l = 0, 31 do + SbrQmf.synthesis(q, l, XR, XI, l * 64, out, o + l * 64 * stride, stride) + local p, p2 = o + l * 64 * stride, o2 + l * 64 * stride + for _ = 1, 64 do + out[p2] = out[p] + p += stride + p2 += stride + end + end + end + else + for l = 0, 31 do + slot(e, c, ok, lTemp, l, SR, SI, 0) + SbrQmf.synthesis(q, l, SR, SI, 0, out, o + l * 64 * stride, stride) + end + end + SbrQmf.endFrame(q) + h.Yr, h.Pr = h.Pr, h.Yr + h.Yi, h.Pi = h.Pi, h.Yi + if ok then + h.tEPrev, h.kxPrev, h.mPrev = c.tE[c.nEnv + 1], e.kx, e.M + else + h.tEPrev, h.kxPrev, h.mPrev = 16, 32, 0 + end + end + if ok then + e.reset = false + end + e.present = false +end + +return Sbr diff --git a/src/Rosetta/codecs/audio/Aac/SbrBits.luau b/src/Rosetta/codecs/audio/Aac/SbrBits.luau new file mode 100644 index 0000000..727aafe --- /dev/null +++ b/src/Rosetta/codecs/audio/Aac/SbrBits.luau @@ -0,0 +1,117 @@ +--!strict +--!native +--!optimize 2 +-- codecs/audio/Aac/SbrBits: the MSB-first bit cursor and Huffman decoding shared by the SBR and PS payload parsers +-- (ISO/IEC 14496-3 4.6.18 / 8.6.4; codebooks of codecs/audio/Aac/SbrTables). One payload at a time, over the core +-- decoder's zero-padded frame copy. +local readu32, byteswap, lshift, rshift, band = buffer.readu32, bit32.byteswap, bit32.lshift, bit32.rshift, bit32.band + +local SbrBits = {} + +local RB: buffer = buffer.create(16) +local RP = 0 +local RE = 0 +local ROVER = false + +-- the buffer must hold 4 readable bytes past (pos + count) / 8 +function SbrBits.start(buf: buffer, pos: number, count: number) + local n = buffer.len(buf) + if pos < 0 or count < 0 or (pos + count + 7) // 8 + 4 > n then + RB, RP, RE, ROVER = buf, 0, 0, true + return + end + RB, RP, RE, ROVER = buf, pos, pos + count, false +end + +-- n = 1 .. 25 +local function bits(n: number): number + local p = RP + RP = p + n + if RP > RE then + ROVER = true + RP = RE + return 0 + end + return rshift(lshift(byteswap(readu32(RB, rshift(p, 3))), band(p, 7)), 32 - n) +end +SbrBits.bits = bits + +function SbrBits.skip(n: number) + RP += n + if RP > RE then + ROVER = true + RP = RE + end +end + +function SbrBits.pos(): number + return RP +end + +function SbrBits.over(): boolean + return ROVER +end + +-- codebook from { lav, lengths, codes }: { lav, peek[2^8] = sym * 32 + len (0: longer), long[len * 2^20 + code] = +-- sym, maxLen } (sym 1-based; value = sym - 1 - lav) +local PEEK = 8 +function SbrBits.codebook(t: { any }): { any } + local lav, lens, codes = t[1], t[2], t[3] + local peek = table.create(2 ^ PEEK, 0) + local long: { [number]: number } = {} + local maxLen = 0 + for s = 1, #lens do + local l, c = lens[s], codes[s] + if l > maxLen then + maxLen = l + end + if l <= PEEK then + local span = 2 ^ (PEEK - l) + for k = 0, span - 1 do + peek[c * span + k + 1] = s * 32 + l + end + else + long[l * 1048576 + c] = s + end + end + return { lav, peek, long, maxLen } +end + +-- the decoded value, or nil on an invalid code / the end of the payload +function SbrBits.huff(bk: { any }): number? + local p = RP + local left = RE - p + if left <= 0 then + ROVER = true + return nil + end + local w + if left >= PEEK then + w = rshift(lshift(byteswap(readu32(RB, rshift(p, 3))), band(p, 7)), 32 - PEEK) + else + w = lshift(rshift(lshift(byteswap(readu32(RB, rshift(p, 3))), band(p, 7)), 32 - left), PEEK - left) + end + local e = bk[2][w + 1] + if e > 0 then + local l = band(e, 31) + if l > left then + ROVER = true + RP = RE + return nil + end + RP = p + l + return rshift(e, 5) - 1 - bk[1] + end + local long = bk[3] + local code = bits(PEEK) + for l = PEEK + 1, bk[4] do + code = code * 2 + bits(1) + local s = long[l * 1048576 + code] + if s then + return s - 1 - bk[1] + end + end + return nil +end + +return SbrBits diff --git a/src/Rosetta/codecs/audio/Aac/SbrHf.luau b/src/Rosetta/codecs/audio/Aac/SbrHf.luau new file mode 100644 index 0000000..dc2f0a9 --- /dev/null +++ b/src/Rosetta/codecs/audio/Aac/SbrHf.luau @@ -0,0 +1,453 @@ +--!strict +--!native +--!optimize 2 +-- codecs/audio/Aac/SbrHf: SBR HF generation (4.6.18.6: chirp factors, covariance prediction, patching) and HF +-- adjustment (4.6.18.7: mapping, current energies, gains, limiter, boost, noise floor, sinusoids, smoothing). +-- Ported from syom 0.8.1 src/engine/sbr_hf_gen.rs, sbr_env_adjust.rs (MIT, licenses/syom-LICENSE.txt). +local SbrTables = require("@rosetta/codecs/audio/Aac/SbrTables") + +local sqrt, min = math.sqrt, math.min + +local SbrHf = {} + +local NOISE = SbrTables.NOISE +local LIM_GAIN = { 0.70795, 1.0, 1.41254, 1e10 } +local H_SMOOTH = { 0.33333333333333, 0.30150283239582, 0.21816949906249, 0.11516383427084, 0.03183050093751 } +local EPS0 = 1e-12 +local MAX_BOOST = 1.584893192 +-- 4.6.18.6.2 Table 4.183: newBw from (previous, current) bs_invf_mode +local NEW_BW = { 0, 0.6, 0.9, 0.98, 0.6, 0.75, 0.9, 0.98, 0, 0.75, 0.9, 0.98, 0, 0.75, 0.9, 0.98 } + +-- per-channel HF state +function SbrHf.state(): any + return { + Yr = table.create(40 * 64, 0), -- X_high / Y[col * 64 + k + 1], col = slot + tHFAdj + Yi = table.create(40 * 64, 0), + Pr = table.create(40 * 64, 0), -- the previous frame's Y + Pi = table.create(40 * 64, 0), + bw = table.create(5, 0), + prevBw = table.create(5, 0), + prevInvf = table.create(5, 0), + tEPrev = 16, + kxPrev = 32, + mPrev = 0, + -- envelope adjuster + gTail = table.create(4 * 64, 0), + qTail = table.create(4 * 64, 0), + tailM = -1, + indexNoise = 0, + indexSine = 0, + sPrev = table.create(64, 0), + sPrevKx = 0, + lAPrev = -1, + lEPrev = 0, + started = false, + } +end + +local alpha0r = table.create(32, 0) +local alpha0i = table.create(32, 0) +local alpha1r = table.create(32, 0) +local alpha1i = table.create(32, 0) +local alphaDone = table.create(32, 0) +local alphaFrame = 0 + +-- 4.6.18.6.2 covariance method for source subband p (0-based), cached per frame +local function prediction(xr: { number }, xi: { number }, p: number) + local i = p + 1 + if alphaDone[i] == alphaFrame then + return + end + alphaDone[i] = alphaFrame + local p01r, p01i, p02r, p02i, p11, p12r, p12i, p22 = 0, 0, 0, 0, 0, 0, 0, 0 + local x2r, x2i = xr[i], xi[i] + local x1r, x1i = xr[32 + i], xi[32 + i] + for col = 2, 39 do + local q = col * 32 + i + local x0r, x0i = xr[q], xi[q] + p01r += x0r * x1r + x0i * x1i + p01i += x0i * x1r - x0r * x1i + p02r += x0r * x2r + x0i * x2i + p02i += x0i * x2r - x0r * x2i + p11 += x1r * x1r + x1i * x1i + p12r += x1r * x2r + x1i * x2i + p12i += x1i * x2r - x1r * x2i + p22 += x2r * x2r + x2i * x2i + x2r, x2i = x1r, x1i + x1r, x1i = x0r, x0i + end + local a1r, a1i, a0r, a0i = 0, 0, 0, 0 + local d = p22 * p11 - (p12r * p12r + p12i * p12i) / (1 + 1e-6) + if d ~= 0 then + -- (phi01 * phi12 - phi02 * phi11) / d + a1r = (p01r * p12r - p01i * p12i - p02r * p11) / d + a1i = (p01r * p12i + p01i * p12r - p02i * p11) / d + end + if p11 ~= 0 then + -- -(phi01 + alpha1 * conj(phi12)) / phi11 + a0r = -(p01r + a1r * p12r + a1i * p12i) / p11 + a0i = -(p01i + a1i * p12r - a1r * p12i) / p11 + end + if a0r * a0r + a0i * a0i >= 16 or a1r * a1r + a1i * a1i >= 16 then + a0r, a0i, a1r, a1i = 0, 0, 0, 0 + end + alpha0r[i], alpha0i[i], alpha1r[i], alpha1i[i] = a0r, a0i, a1r, a1i +end + +-- 4.6.18.6: X_high for the frame's envelope span; chirp factors from the invf modes +function SbrHf.generate(e: any, c: any, h: any, xr: { number }, xi: { number }) + local yr, yi = h.Yr, h.Yi + for i = 1, 40 * 64 do + yr[i], yi[i] = 0, 0 + end + local nQ = e.nQ + local bw, prevBw, prevInvf, invf = h.bw, h.prevBw, h.prevInvf, c.invf + for i = 1, nQ do + local nb = NEW_BW[(prevInvf[i] or 0) * 4 + (invf[i] or 0) + 1] or 0 + local t = if nb < prevBw[i] then 0.75 * nb + 0.25 * prevBw[i] else 0.90625 * nb + 0.09375 * prevBw[i] + bw[i] = if t < 0.015625 then 0 else t + end + alphaFrame += 1 + local c0 = 2 * c.tE[1] + 2 + local c1 = 2 * c.tE[c.nEnv + 1] + 2 - 1 + local kx, mapQ = e.kx, e.mapQ + local k = kx + for pi, num in e.patchNum do + local start = e.patchStart[pi] + for x = 0, num - 1 do + local p = start + x + prediction(xr, xi, p) + local i = p + 1 + local b = bw[(mapQ[k - kx + 1] or 0) + 1] or 0 + local b2 = b * b + local a0r, a0i = alpha0r[i] * b, alpha0i[i] * b + local a1r, a1i = alpha1r[i] * b2, alpha1i[i] * b2 + local q = c0 * 32 + i + local o = c0 * 64 + k + 1 + local m1r, m1i = xr[q - 32], xi[q - 32] + local m2r, m2i = xr[q - 64], xi[q - 64] + for _ = c0, c1 do + local x0r, x0i = xr[q], xi[q] + yr[o] = x0r + a0r * m1r - a0i * m1i + a1r * m2r - a1i * m2i + yi[o] = x0i + a0r * m1i + a0i * m1r + a1r * m2i + a1i * m2r + m2r, m2i = m1r, m1i + m1r, m1i = x0r, x0i + q += 32 + o += 64 + end + k += 1 + end + end + for i = 1, nQ do + prevBw[i] = bw[i] + prevInvf[i] = invf[i] + end +end + +-- per-envelope scratch: [ (l - 1) * 64 + m ] +local E_MAP = table.create(5 * 64, 0) +local Q_MAP = table.create(5 * 64, 0) +local S_IDX = table.create(5 * 64, 0) +local S_MAP = table.create(5 * 64, 0) +local E_CURR = table.create(5 * 64, 0) +local G_B = table.create(5 * 64, 0) +local Q_B = table.create(5 * 64, 0) +local S_B = table.create(5 * 64, 0) +local QM = table.create(64, 0) +local SM = table.create(64, 0) +local GG = table.create(64, 0) +local GMAX = table.create(64, 0) +local QLIM = table.create(64, 0) +local GLIM = table.create(64, 0) +local G_T = table.create(44 * 64, 0) +local Q_T = table.create(44 * 64, 0) +local ENV_OF = table.create(40, 0) + +-- 4.6.18.7.2 mapping of E_orig / Q_orig / sinusoids onto the subbands of each envelope +local function mapping(e: any, c: any, h: any): number + local kx, M = e.kx, e.M + local nEnv = c.nEnv + local mapH, mapL, mapQ = e.mapH, e.mapL, e.mapQ + local eOrig, qOrig, tE, tQ = c.eOrig, c.qOrig, c.tE, c.tQ + local fHigh = e.fHigh + for l = 1, nEnv do + local base = (l - 1) * 64 + local map = if c.freqRes[l] == 1 then mapH else mapL + local nq = if c.nNoise > 1 and tE[l] >= tQ[2] then 1 else 0 + for m = 1, M do + E_MAP[base + m] = eOrig[base + map[m] + 1] + Q_MAP[base + m] = qOrig[nq * 8 + mapQ[m] + 1] + S_IDX[base + m] = 0 + S_MAP[base + m] = 0 + end + if c.hasHarm then + for i = 1, e.nHigh do + if c.harm[i] == 1 then + local mid = (fHigh[i + 1] + fHigh[i]) // 2 + local mr = mid - kx + if mr >= 0 and mr < M then + local pr = mid - h.sPrevKx + local prevOn = pr >= 0 and pr < 64 and h.sPrev[pr + 1] == 1 + if l - 1 >= c.lA or prevOn then + S_IDX[base + mr + 1] = 1 + end + end + end + end + local f = if c.freqRes[l] == 1 then fHigh else e.fLow + for i = 1, #f - 1 do + local lo, hi = f[i] - kx, f[i + 1] - kx + local any = false + for j = lo, hi - 1 do + if S_IDX[base + j + 1] == 1 then + any = true + end + end + if any then + for j = lo, hi - 1 do + S_MAP[base + j + 1] = 1 + end + end + end + end + end + return nEnv +end + +-- 4.6.18.7.3 current envelope energies E_curr +local function energies(e: any, c: any, hdr: any, yr: { number }, yi: { number }) + local kx, M = e.kx, e.M + for l = 1, c.nEnv do + local base = (l - 1) * 64 + local lo = 2 * c.tE[l] + 2 + local hi = 2 * c.tE[l + 1] + 2 + local inv = 1 / (hi - lo) + if hdr.interpolFreq then + for m = 1, M do + local s = 0 + local o = lo * 64 + kx + m + for _ = lo, hi - 1 do + local r, i = yr[o], yi[o] + s += r * r + i * i + o += 64 + end + E_CURR[base + m] = s * inv + end + else + local f = if c.freqRes[l] == 1 then e.fHigh else e.fLow + for p = 1, #f - 1 do + local kl, kh = f[p], f[p + 1] - 1 + local s = 0 + for j = kl, kh do + local o = lo * 64 + j + 1 + for _ = lo, hi - 1 do + local r, i = yr[o], yi[o] + s += r * r + i * i + o += 64 + end + end + local avg = s * inv / (kh - kl + 1) + for j = kl, kh do + E_CURR[base + j - kx + 1] = avg + end + end + end + end +end + +-- 4.6.18.7.4 / 4.6.18.7.5 gains, limiter and boost per envelope +local function gains(e: any, c: any, hdr: any, lAPrev: number) + local kx, M = e.kx, e.M + local fLim = e.fLim + local limGain = LIM_GAIN[hdr.limiterGains + 1] or 1 + for l = 1, c.nEnv do + local base = (l - 1) * 64 + local li = l - 1 + local trans = li == c.lA or li == lAPrev + local deltaL = if trans then 0 else 1 + for m = 1, M do + local i = base + m + local eo, q = E_MAP[i], Q_MAP[i] + local ec = E_CURR[i] + QM[m] = sqrt(eo * q / (1 + q)) + SM[m] = if S_IDX[i] == 1 then sqrt(eo / (1 + q)) else 0 + if S_MAP[i] == 1 then + GG[m] = sqrt(eo / (1 + ec) * (q / (1 + q))) + else + GG[m] = sqrt(eo / ((1 + ec) * (1 + deltaL * q))) + end + end + for b = 1, #fLim - 1 do + local lo = math.max(fLim[b] - kx, 0) + local hi = min(math.max(fLim[b + 1] - kx, 0), M) + local num, den = EPS0, EPS0 + for m = lo + 1, hi do + num += E_MAP[base + m] + den += E_CURR[base + m] + end + local gmax = min(sqrt(num / den) * limGain, 1e5) + for m = lo + 1, hi do + GMAX[m] = gmax + end + end + for m = 1, M do + local g, gm, qm = GG[m], GMAX[m], QM[m] + QLIM[m] = if g > 0 then min(qm, qm * gm / g) else qm + GLIM[m] = min(g, gm) + end + for b = 1, #fLim - 1 do + local lo = math.max(fLim[b] - kx, 0) + local hi = min(math.max(fLim[b + 1] - kx, 0), M) + local num, den = EPS0, EPS0 + for m = lo + 1, hi do + num += E_MAP[base + m] + local sm, gl = SM[m], GLIM[m] + den += E_CURR[base + m] * gl * gl + sm * sm + if not (sm ~= 0 or trans) then + den += QLIM[m] * QLIM[m] + end + end + local boost = min(sqrt(num / den), MAX_BOOST) + for m = lo + 1, hi do + G_B[base + m] = GLIM[m] * boost + Q_B[base + m] = QLIM[m] * boost + S_B[base + m] = SM[m] * boost + end + end + end +end + +-- 4.6.18.7.6 assembly of Y over the frame's envelope span +local function assemble(e: any, c: any, hdr: any, h: any, lAPrev: number, yr: { number }, yi: { number }) + local kx, M = e.kx, e.M + local nEnv = c.nEnv + local tE = c.tE + local i0, iEnd = 2 * tE[1], 2 * tE[nEnv + 1] + local nCols = iEnd - i0 + local hSl = if hdr.smoothingMode then 0 else 4 + for col = 0, nCols - 1 do + local t = i0 + col + local l = nEnv + for x = 1, nEnv do + if t >= 2 * tE[x] and t < 2 * tE[x + 1] then + l = x + break + end + end + ENV_OF[col + 1] = l + end + if hSl > 0 then + local tail = h.tailM == M + for j = 0, hSl - 1 do + local d = j * 64 + for m = 1, M do + G_T[d + m] = if tail then h.gTail[d + m] else G_B[m] + Q_T[d + m] = if tail then h.qTail[d + m] else Q_B[m] + end + end + for col = 0, nCols - 1 do + local d = (col + hSl) * 64 + local s = (ENV_OF[col + 1] - 1) * 64 + for m = 1, M do + G_T[d + m] = G_B[s + m] + Q_T[d + m] = Q_B[s + m] + end + end + end + local indexNoise, indexSine = h.indexNoise, h.indexSine + local fNoise, fSine = indexNoise, indexSine + for col = 0, nCols - 1 do + local l = ENV_OF[col + 1] + local li = l - 1 + local base = (l - 1) * 64 + local trans = li == c.lA or li == lAPrev + local smooth = hSl ~= 0 and not trans + fSine = (indexSine + col) % 4 + local o = (i0 + col + 2) * 64 + kx + local nb = indexNoise + col * M + local sinRe = if fSine == 0 then 1 elseif fSine == 2 then -1 else 0 + local sinIm = if fSine == 1 then 1 elseif fSine == 3 then -1 else 0 + local d = (col + hSl) * 64 + for m = 1, M do + local gf, qf + if smooth then + gf = 0 + qf = 0 + for j = 0, hSl do + gf += G_T[d - j * 64 + m] * H_SMOOTH[j + 1] + qf += Q_T[d - j * 64 + m] * H_SMOOTH[j + 1] + end + elseif hSl > 0 then + gf, qf = G_T[d + m], Q_T[d + m] + else + gf, qf = G_B[base + m], Q_B[base + m] + end + local sb = S_B[base + m] + if trans or sb ~= 0 then + qf = 0 + end + local ni = (nb + m) % 512 + local r = yr[o + m] * gf + qf * NOISE[2 * ni + 1] + local im = yi[o + m] * gf + qf * NOISE[2 * ni + 2] + if S_IDX[base + m] == 1 then + r += sb * sinRe + if (m - 1 + kx) % 2 == 1 then + im -= sb * sinIm + else + im += sb * sinIm + end + end + yr[o + m] = r + yi[o + m] = im + end + fNoise = (indexNoise + (col + 1) * M) % 512 + end + if nCols > 0 then + h.indexNoise = fNoise + h.indexSine = (fSine + 1) % 4 + end + if hSl > 0 then + for j = 0, hSl - 1 do + local s = (nCols + j) * 64 + local d = j * 64 + for m = 1, M do + h.gTail[d + m] = G_T[s + m] + h.qTail[d + m] = Q_T[s + m] + end + end + h.tailM = M + else + h.tailM = -1 + end +end + +-- 4.6.18.7: adjust X_high (h.Yr / h.Yi) in place into Y +function SbrHf.adjust(e: any, c: any, hdr: any, h: any) + if e.reset or not h.started then + h.indexNoise, h.indexSine = 0, 0 + h.tailM = -1 + h.lAPrev, h.lEPrev = -1, 0 + h.sPrevKx = 0 + table.clear(h.sPrev) + for i = 1, 64 do + h.sPrev[i] = 0 + end + h.started = true + end + local lAPrev = if h.lAPrev == h.lEPrev then 0 else -1 + mapping(e, c, h) + local yr, yi = h.Yr, h.Yi + energies(e, c, hdr, yr, yi) + gains(e, c, hdr, lAPrev) + assemble(e, c, hdr, h, lAPrev, yr, yi) + local lb = (c.nEnv - 1) * 64 + local sp = h.sPrev + for m = 1, 64 do + sp[m] = if m <= e.M then S_IDX[lb + m] else 0 + end + h.sPrevKx = e.kx + h.lAPrev = c.lA + h.lEPrev = c.nEnv +end + +return SbrHf diff --git a/src/Rosetta/codecs/audio/Aac/SbrQmf.luau b/src/Rosetta/codecs/audio/Aac/SbrQmf.luau new file mode 100644 index 0000000..0f1709c --- /dev/null +++ b/src/Rosetta/codecs/audio/Aac/SbrQmf.luau @@ -0,0 +1,136 @@ +--!strict +--!native +--!optimize 2 +-- codecs/audio/Aac/SbrQmf: the SBR 32-band complex analysis and 64-band synthesis QMF banks (ISO/IEC 14496-3 +-- 4.6.18.4.1 / 4.6.18.4.2), through DCT-IV factorisations of the modulation (codecs/audio/Mdct). Written from the +-- standard; the window is codecs/audio/Aac/SbrTables.QMF_WINDOW. +-- analysis: W(k) = 2 e^(-i 3 pi (k + 1/2) / 128) (DCT4_32(u(m) - u(63 - m)) + i DST4_32(u(m) + u(63 - m)))(k) +-- synthesis: v(n) = -(DCT4_64(Re X) - DST4_64(Im X))(n) / 64, v(127 - n) = (DCT4_64(Re X) + DST4_64(Im X))(n) / 64 +local Mdct = require("@rosetta/codecs/audio/Mdct") +local SbrTables = require("@rosetta/codecs/audio/Aac/SbrTables") + +local cos, sin, pi = math.cos, math.sin, math.pi + +local SbrQmf = {} + +local C = SbrTables.QMF_WINDOW +local DCT32 = Mdct.new(64) :: Mdct.Plan +local DCT64 = Mdct.new(128) :: Mdct.Plan +local dct4 = Mdct.dct4 + +-- the core decoder's samples are scaled by 1 / 32768; the SBR energies assume 16-bit sample values +local IN_SCALE = 32768 +local OUT_SCALE = 1 / (64 * 32768) + +-- analysis taps: CA[j * 64 + n + 1] = c[2n + 128j] * IN_SCALE +local CA = table.create(320, 0) +for j = 0, 4 do + for n = 0, 63 do + CA[j * 64 + n + 1] = C[2 * n + 128 * j + 1] * IN_SCALE + end +end +-- e^(-i beta_k) * 2 with beta_k = 0.75 pi (k + 1/2) / 32, the DST sign (-1)^k folded into SS +local AC, AS, SC, SS = table.create(32, 0), table.create(32, 0), table.create(32, 0), table.create(32, 0) +for k = 0, 31 do + local b = 0.75 * pi * (k + 0.5) / 32 + local sg = if k % 2 == 0 then 1 else -1 + AC[k + 1], AS[k + 1] = 2 * cos(b), 2 * sin(b) + SC[k + 1], SS[k + 1] = sg * 2 * cos(b), sg * 2 * sin(b) +end + +local U = table.create(64, 0) +local A = table.create(32, 0) +local B = table.create(32, 0) +local CR = table.create(64, 0) +local CI = table.create(64, 0) + +-- per-channel filterbank state +function SbrQmf.state(): any + return { + qa = table.create(320 + 1024, 0), -- analysis input: 320 history samples, then the frame + qs = table.create(41 * 128, 0), -- synthesis v blocks: 9 history blocks, then 32 for the frame + XLr = table.create(40 * 32, 0), -- X_low[col * 32 + k + 1], col 0..7 = the previous frame's 32..39 + XLi = table.create(40 * 32, 0), + } +end + +-- 32 analysis slots of one frame: 1024 core samples from inp[io + i * is] -> X_low columns 8 .. 39 +function SbrQmf.analysis(s: any, inp: { number }, io: number, is: number) + local x: { number } = s.qa + local p = io + for i = 321, 1344 do + x[i] = inp[p] + p += is + end + local xr: { number }, xi: { number } = s.XLr, s.XLi + table.move(xr, 1025, 1280, 1) + table.move(xi, 1025, 1280, 1) + local u, a, b, cr, ci = U, A, B, CR, CI + for l = 0, 31 do + local t = 352 + 32 * l + for n = 1, 64 do + local q = t - n + 1 + u[n] = x[q] * CA[n] + x[q - 64] * CA[64 + n] + x[q - 128] * CA[128 + n] + x[q - 192] * CA[192 + n] + + x[q - 256] * CA[256 + n] + end + for m = 1, 32 do + local u0, u1 = u[m], u[65 - m] + a[m] = u0 - u1 + b[m] = u0 + u1 + end + dct4(DCT32, a, 1, 1, cr, 1, 1, 1) + dct4(DCT32, b, 32, -1, ci, 1, 1, 1) + local o = (8 + l) * 32 + for k = 1, 32 do + local c0, s0 = cr[k], ci[k] + xr[o + k] = c0 * AC[k] + s0 * SS[k] + xi[o + k] = s0 * SC[k] - c0 * AS[k] + end + end + table.move(x, 1025, 1344, 1) +end + +-- one synthesis slot: 64 complex subbands (xr / xi at [xo + k], k = 1 .. 64) -> 64 samples at out[o + j * os] +function SbrQmf.synthesis(s: any, slot: number, xr: { number }, xi: { number }, xo: number, out: { number }, o: number, os: number) + local v: { number } = s.qs + local cr, ci = CR, CI + dct4(DCT64, xr, xo + 1, 1, cr, 1, 1, OUT_SCALE) + dct4(DCT64, xi, xo + 64, -1, ci, 1, 1, OUT_SCALE) + local base = (9 + slot) * 128 + for n = 1, 64 do + local c0 = cr[n] + local s0 = if n % 2 == 1 then ci[n] else -ci[n] + v[base + n] = s0 - c0 + v[base + 129 - n] = c0 + s0 + end + local b0, b1 = base, base - 128 + local p = o + for k = 1, 64 do + local acc = v[b0 + k] * C[k] + v[b1 + 64 + k] * C[64 + k] + acc += v[b0 - 256 + k] * C[128 + k] + v[b1 - 256 + 64 + k] * C[192 + k] + acc += v[b0 - 512 + k] * C[256 + k] + v[b1 - 512 + 64 + k] * C[320 + k] + acc += v[b0 - 768 + k] * C[384 + k] + v[b1 - 768 + 64 + k] * C[448 + k] + acc += v[b0 - 1024 + k] * C[512 + k] + v[b1 - 1024 + 64 + k] * C[576 + k] + out[p] = acc + p += os + end +end + +function SbrQmf.endFrame(s: any) + local v: { number } = s.qs + table.move(v, 32 * 128 + 1, 41 * 128, 1) +end + +function SbrQmf.reset(s: any) + for i = 1, 1344 do + s.qa[i] = 0 + end + for i = 1, 41 * 128 do + s.qs[i] = 0 + end + for i = 1, 1280 do + s.XLr[i], s.XLi[i] = 0, 0 + end +end + +return SbrQmf diff --git a/src/Rosetta/codecs/audio/Aac/SbrTables.luau b/src/Rosetta/codecs/audio/Aac/SbrTables.luau new file mode 100644 index 0000000..5d9e0ca --- /dev/null +++ b/src/Rosetta/codecs/audio/Aac/SbrTables.luau @@ -0,0 +1,411 @@ +--!strict +--!optimize 2 +-- codecs/audio/Aac/SbrTables: SBR / PS constant tables (ISO/IEC 14496-3 4.A.6.1, 4.A.6.4, 8.A). +-- Numbers from syom 0.8.1 src/engine (MIT, licenses/syom-LICENSE.txt), transcribed there from the standard. +local SbrTables = {} + +-- Table 4.A.89: c[i], the 640-tap QMF prototype window +SbrTables.QMF_WINDOW = { + 0.0000000000, -0.0005525286, -0.0005617692, -0.0004947518, -0.0004875227, -0.0004893791, -0.0005040714, -0.0005226564, + -0.0005466565, -0.0005677802, -0.0005870930, -0.0006132747, -0.0006312493, -0.0006540333, -0.0006777690, -0.0006941614, + -0.0007157736, -0.0007255043, -0.0007440941, -0.0007490598, -0.0007681371, -0.0007724848, -0.0007834332, -0.0007779869, + -0.0007803664, -0.0007801449, -0.0007757977, -0.0007630793, -0.0007530001, -0.0007319357, -0.0007215391, -0.0006917937, + -0.0006650415, -0.0006341594, -0.0005946118, -0.0005564576, -0.0005145572, -0.0004606325, -0.0004095121, -0.0003501175, + -0.0002896981, -0.0002098337, -0.0001446380, -0.0000617334, 0.0000134949, 0.0001094383, 0.0002043017, 0.0002949531, + 0.0004026540, 0.0005107388, 0.0006239376, 0.0007458025, 0.0008608443, 0.0009885988, 0.0011250155, 0.0012577884, + 0.0013902494, 0.0015443219, 0.0016868083, 0.0018348265, 0.0019841140, 0.0021461583, 0.0023017254, 0.0024625616, + 0.0026201758, 0.0027870464, 0.0029469447, 0.0031125420, 0.0032739613, 0.0034418874, 0.0036008268, 0.0037603922, + 0.0039207432, 0.0040819753, 0.0042264269, 0.0043730719, 0.0045209852, 0.0046606460, 0.0047932560, 0.0049137603, + 0.0050393022, 0.0051407353, 0.0052461166, 0.0053471681, 0.0054196775, 0.0054876040, 0.0055475714, 0.0055938023, + 0.0056220643, 0.0056455196, 0.0056389199, 0.0056266114, 0.0055917128, 0.0055404363, 0.0054753783, 0.0053838975, + 0.0052715758, 0.0051382275, 0.0049839687, 0.0048109469, 0.0046039530, 0.0043801861, 0.0041251642, 0.0038456408, + 0.0035401246, 0.0032091885, 0.0028446757, 0.0024508540, 0.0020274176, 0.0015784682, 0.0010902329, 0.0005832264, + 0.0000276045, -0.0005464280, -0.0011568135, -0.0018039472, -0.0024826723, -0.0031933778, -0.0039401124, -0.0047222596, + -0.0055337211, -0.0063792293, -0.0072615816, -0.0081798233, -0.0091325329, -0.0101150215, -0.0111315548, -0.0121849995, + 0.0132718220, 0.0143904666, 0.0155405553, 0.0167324712, 0.0179433381, 0.0191872431, 0.0204531793, 0.0217467550, + 0.0230680169, 0.0244160992, 0.0257875847, 0.0271859429, 0.0286072173, 0.0300502657, 0.0315017608, 0.0329754081, + 0.0344620948, 0.0359697560, 0.0374812850, 0.0390053679, 0.0405349170, 0.0420649094, 0.0436097542, 0.0451488405, + 0.0466843027, 0.0482165720, 0.0497385755, 0.0512556155, 0.0527630746, 0.0542452768, 0.0557173648, 0.0571616450, + 0.0585915683, 0.0599837480, 0.0613455171, 0.0626857808, 0.0639715898, 0.0652247106, 0.0664367512, 0.0676075985, + 0.0687043828, 0.0697630244, 0.0707628710, 0.0717002673, 0.0725682583, 0.0733620255, 0.0741003642, 0.0747452558, + 0.0753137336, 0.0758008358, 0.0761992479, 0.0764992170, 0.0767093490, 0.0768173975, 0.0768230011, 0.0767204924, + 0.0765050718, 0.0761748321, 0.0757305756, 0.0751576255, 0.0744664394, 0.0736406005, 0.0726774642, 0.0715826364, + 0.0703533073, 0.0689664013, 0.0674525021, 0.0657690668, 0.0639444805, 0.0619602779, 0.0598166570, 0.0575152691, + 0.0550460034, 0.0524093821, 0.0495978676, 0.0466303305, 0.0434768782, 0.0401458278, 0.0366418116, 0.0329583930, + 0.0290824006, 0.0250307561, 0.0207997072, 0.0163701258, 0.0117623832, 0.0069636862, 0.0019765601, -0.0032086896, + -0.0085711749, -0.0141288827, -0.0198834129, -0.0258227288, -0.0319531274, -0.0382776572, -0.0447806821, -0.0514804176, + -0.0583705326, -0.0654409853, -0.0726943300, -0.0801372934, -0.0877547536, -0.0955533352, -0.1035329531, -0.1116826931, + -0.1200077984, -0.1285002850, -0.1371551761, -0.1459766491, -0.1549607071, -0.1640958855, -0.1733808172, -0.1828172548, + -0.1923966745, -0.2021250176, -0.2119735853, -0.2219652696, -0.2320690870, -0.2423016884, -0.2526480309, -0.2631053299, + -0.2736634040, -0.2843214189, -0.2950716717, -0.3059098575, -0.3168278913, -0.3278113727, -0.3388722693, -0.3499914122, + 0.3611589903, 0.3723795546, 0.3836350013, 0.3949211761, 0.4062317676, 0.4175696896, 0.4289119920, 0.4402553754, + 0.4515996535, 0.4629308085, 0.4742453214, 0.4855253091, 0.4967708254, 0.5079817500, 0.5191234970, 0.5302240895, + 0.5412553448, 0.5522051258, 0.5630789140, 0.5738524131, 0.5845403235, 0.5951123086, 0.6055783538, 0.6159109932, + 0.6261242695, 0.6361980107, 0.6461269695, 0.6559016302, 0.6655139880, 0.6749663190, 0.6842353293, 0.6933282376, + 0.7022388719, 0.7109410426, 0.7194462634, 0.7277448900, 0.7358211758, 0.7436827863, 0.7513137456, 0.7587080760, + 0.7658674865, 0.7727780881, 0.7794287519, 0.7858353120, 0.7919735841, 0.7978466413, 0.8034485751, 0.8087695004, + 0.8138191270, 0.8185776004, 0.8230419890, 0.8272275347, 0.8311038457, 0.8346937361, 0.8379717337, 0.8409541392, + 0.8436238281, 0.8459818469, 0.8480315777, 0.8497805198, 0.8511971524, 0.8523047035, 0.8531020949, 0.8535720573, + 0.8537385600, 0.8535720573, 0.8531020949, 0.8523047035, 0.8511971524, 0.8497805198, 0.8480315777, 0.8459818469, + 0.8436238281, 0.8409541392, 0.8379717337, 0.8346937361, 0.8311038457, 0.8272275347, 0.8230419890, 0.8185776004, + 0.8138191270, 0.8087695004, 0.8034485751, 0.7978466413, 0.7919735841, 0.7858353120, 0.7794287519, 0.7727780881, + 0.7658674865, 0.7587080760, 0.7513137456, 0.7436827863, 0.7358211758, 0.7277448900, 0.7194462634, 0.7109410426, + 0.7022388719, 0.6933282376, 0.6842353293, 0.6749663190, 0.6655139880, 0.6559016302, 0.6461269695, 0.6361980107, + 0.6261242695, 0.6159109932, 0.6055783538, 0.5951123086, 0.5845403235, 0.5738524131, 0.5630789140, 0.5522051258, + 0.5412553448, 0.5302240895, 0.5191234970, 0.5079817500, 0.4967708254, 0.4855253091, 0.4742453214, 0.4629308085, + 0.4515996535, 0.4402553754, 0.4289119920, 0.4175696896, 0.4062317676, 0.3949211761, 0.3836350013, 0.3723795546, + -0.3611589903, -0.3499914122, -0.3388722693, -0.3278113727, -0.3168278913, -0.3059098575, -0.2950716717, -0.2843214189, + -0.2736634040, -0.2631053299, -0.2526480309, -0.2423016884, -0.2320690870, -0.2219652696, -0.2119735853, -0.2021250176, + -0.1923966745, -0.1828172548, -0.1733808172, -0.1640958855, -0.1549607071, -0.1459766491, -0.1371551761, -0.1285002850, + -0.1200077984, -0.1116826931, -0.1035329531, -0.0955533352, -0.0877547536, -0.0801372934, -0.0726943300, -0.0654409853, + -0.0583705326, -0.0514804176, -0.0447806821, -0.0382776572, -0.0319531274, -0.0258227288, -0.0198834129, -0.0141288827, + -0.0085711749, -0.0032086896, 0.0019765601, 0.0069636862, 0.0117623832, 0.0163701258, 0.0207997072, 0.0250307561, + 0.0290824006, 0.0329583930, 0.0366418116, 0.0401458278, 0.0434768782, 0.0466303305, 0.0495978676, 0.0524093821, + 0.0550460034, 0.0575152691, 0.0598166570, 0.0619602779, 0.0639444805, 0.0657690668, 0.0674525021, 0.0689664013, + 0.0703533073, 0.0715826364, 0.0726774642, 0.0736406005, 0.0744664394, 0.0751576255, 0.0757305756, 0.0761748321, + 0.0765050718, 0.0767204924, 0.0768230011, 0.0768173975, 0.0767093490, 0.0764992170, 0.0761992479, 0.0758008358, + 0.0753137336, 0.0747452558, 0.0741003642, 0.0733620255, 0.0725682583, 0.0717002673, 0.0707628710, 0.0697630244, + 0.0687043828, 0.0676075985, 0.0664367512, 0.0652247106, 0.0639715898, 0.0626857808, 0.0613455171, 0.0599837480, + 0.0585915683, 0.0571616450, 0.0557173648, 0.0542452768, 0.0527630746, 0.0512556155, 0.0497385755, 0.0482165720, + 0.0466843027, 0.0451488405, 0.0436097542, 0.0420649094, 0.0405349170, 0.0390053679, 0.0374812850, 0.0359697560, + 0.0344620948, 0.0329754081, 0.0315017608, 0.0300502657, 0.0286072173, 0.0271859429, 0.0257875847, 0.0244160992, + 0.0230680169, 0.0217467550, 0.0204531793, 0.0191872431, 0.0179433381, 0.0167324712, 0.0155405553, 0.0143904666, + -0.0132718220, -0.0121849995, -0.0111315548, -0.0101150215, -0.0091325329, -0.0081798233, -0.0072615816, -0.0063792293, + -0.0055337211, -0.0047222596, -0.0039401124, -0.0031933778, -0.0024826723, -0.0018039472, -0.0011568135, -0.0005464280, + 0.0000276045, 0.0005832264, 0.0010902329, 0.0015784682, 0.0020274176, 0.0024508540, 0.0028446757, 0.0032091885, + 0.0035401246, 0.0038456408, 0.0041251642, 0.0043801861, 0.0046039530, 0.0048109469, 0.0049839687, 0.0051382275, + 0.0052715758, 0.0053838975, 0.0054753783, 0.0055404363, 0.0055917128, 0.0056266114, 0.0056389199, 0.0056455196, + 0.0056220643, 0.0055938023, 0.0055475714, 0.0054876040, 0.0054196775, 0.0053471681, 0.0052461166, 0.0051407353, + 0.0050393022, 0.0049137603, 0.0047932560, 0.0046606460, 0.0045209852, 0.0043730719, 0.0042264269, 0.0040819753, + 0.0039207432, 0.0037603922, 0.0036008268, 0.0034418874, 0.0032739613, 0.0031125420, 0.0029469447, 0.0027870464, + 0.0026201758, 0.0024625616, 0.0023017254, 0.0021461583, 0.0019841140, 0.0018348265, 0.0016868083, 0.0015443219, + 0.0013902494, 0.0012577884, 0.0011250155, 0.0009885988, 0.0008608443, 0.0007458025, 0.0006239376, 0.0005107388, + 0.0004026540, 0.0002949531, 0.0002043017, 0.0001094383, 0.0000134949, -0.0000617334, -0.0001446380, -0.0002098337, + -0.0002896981, -0.0003501175, -0.0004095121, -0.0004606325, -0.0005145572, -0.0005564576, -0.0005946118, -0.0006341594, + -0.0006650415, -0.0006917937, -0.0007215391, -0.0007319357, -0.0007530001, -0.0007630793, -0.0007757977, -0.0007801449, + -0.0007803664, -0.0007779869, -0.0007834332, -0.0007724848, -0.0007681371, -0.0007490598, -0.0007440941, -0.0007255043, + -0.0007157736, -0.0006941614, -0.0006777690, -0.0006540333, -0.0006312493, -0.0006132747, -0.0005870930, -0.0005677802, + -0.0005466565, -0.0005226564, -0.0005040714, -0.0004893791, -0.0004875227, -0.0004947518, -0.0005617692, -0.000552528, +} + +-- Table 4.A.88: V[k], the SBR noise table (re, im interleaved) +SbrTables.NOISE = { + -0.99948153278296, -0.59483417516607, 0.97113454393991, -0.67528515225647, 0.14130051758487, -0.95090983575689, -0.47005496701697, -0.37340549728647, + 0.80705063769351, 0.29653668284408, -0.38981478896926, 0.89572605717087, -0.01053049862020, -0.66959058036166, -0.91266367957293, -0.11522938140034, + 0.54840422910309, 0.75221367176302, 0.40009252867955, -0.98929400334421, -0.99867974711855, -0.88147068645358, -0.95531076805040, 0.90908757154593, + -0.45725933317144, -0.56716323646760, -0.72929675029275, -0.98008272727324, 0.75622801399036, 0.20950329995549, 0.07069442601050, -0.78247898470706, + 0.74496252926055, -0.91169004445807, -0.96440182703856, -0.94739918296622, 0.30424629369539, -0.49438267012479, 0.66565033746925, 0.64652935542491, + 0.91697008020594, 0.17514097332009, -0.70774918760427, 0.52548653416543, -0.70051415345560, -0.45340028808763, -0.99496513054797, -0.90071908066973, + 0.98164490790123, -0.77463155528697, -0.54671580548181, -0.02570928536004, -0.01689629065389, 0.00287506445732, -0.86110349531986, 0.42548583726477, + -0.98892980586032, -0.87881132267556, 0.51756627678691, 0.66926784710139, -0.99635026409640, -0.58107730574765, -0.99969370862163, 0.98369989360250, + 0.55266258627194, 0.59449057465591, 0.34581177741673, 0.94879421061866, 0.62664209577999, -0.74402970906471, -0.77149701404973, -0.33883658042801, + -0.91592244254432, 0.03687901376713, -0.76285492357887, -0.91371867919124, 0.79788337195331, -0.93180971199849, 0.54473080610200, -0.11919206037186, + -0.85639281671058, 0.42429854760451, -0.92882402971423, 0.27871809078609, -0.11708371046774, -0.99800843444966, 0.21356749817493, -0.90716295627033, + -0.76191692573909, 0.99768118356265, 0.98111043100884, -0.95854459734407, -0.85913269895572, 0.95766566168880, -0.93307242253692, 0.49431757696466, + 0.30485754879632, -0.70540034357529, 0.85289650925190, 0.46766131791044, 0.91328082618125, -0.99839597361769, -0.05890199924154, 0.70741827819497, + 0.28398686150148, 0.34633555702188, 0.95258164539612, -0.54893416026939, -0.78566324168507, -0.75568541079691, -0.95789495447877, -0.20423194696966, + 0.82411158711197, 0.96654618432562, -0.65185446735885, -0.88734990773289, -0.93643603134666, 0.99870790442385, 0.91427159529618, -0.98290505544444, + -0.70395684036886, 0.58796798221039, 0.00563771969365, 0.61768196727244, 0.89065051931895, 0.52783352697585, -0.68683707712762, 0.80806944710339, + 0.72165342518718, -0.69259857349564, -0.62928247730667, 0.13627037407335, 0.29938434065514, -0.46051329682246, -0.91781958879280, -0.74012716684186, + 0.99298717043688, 0.40816610075661, 0.82368298622748, -0.74036047190173, -0.98512833386833, -0.99972330709594, -0.95915368242257, -0.99237800466040, + -0.21411126572790, -0.93424819052545, -0.68821476106884, -0.26892306315457, 0.91851997982317, 0.09358228901785, -0.96062769559127, 0.36099095133739, + 0.51646184922287, -0.71373332873917, 0.61130721139669, 0.46950141175917, 0.47336129371299, -0.27333178296162, 0.90998308703519, 0.96715662938132, + 0.44844799194357, 0.99211574628306, 0.66614891079092, 0.96590176169121, 0.74922239129237, -0.89879858826087, -0.99571588506485, 0.52785521494349, + 0.97401082477563, -0.16855870075190, 0.72683747733879, -0.48060774432251, 0.95432193457128, 0.68849603408441, -0.72962208425191, -0.76608443420917, + -0.85359479233537, 0.88738125901579, -0.81412430338535, -0.97480768049637, -0.87930772356786, 0.74748307690436, -0.71573331064977, -0.98570608178923, + 0.83524300028228, 0.83702537075163, -0.48086065601423, -0.98848504923531, 0.97139128574778, 0.80093621198236, 0.51992825347895, 0.80247631400510, + -0.00848591195325, -0.76670128000486, -0.70294374303036, 0.55359910445577, -0.95894428168140, -0.43265504344783, 0.97079252950321, 0.09325857238682, + -0.92404293670797, 0.85507704027855, -0.69506469500450, 0.98633412625459, 0.26559203620024, 0.73314307966524, 0.28038443336943, 0.14537913654427, + -0.74138124825523, 0.99310339807762, -0.01752795995444, -0.82616635284178, -0.55126773094930, -0.98898543862153, 0.97960898850996, -0.94021446752851, + -0.99196309146936, 0.67019017358456, -0.67684928085260, 0.12631491649378, 0.09140039465500, -0.20537731453108, -0.71658965751996, -0.97788200391224, + 0.81014640078925, 0.53722648362443, 0.40616991671205, -0.26469008598449, -0.67680188682972, 0.94502052337695, 0.86849774348749, -0.18333598647899, + -0.99500381284851, -0.02634122068550, 0.84329189340667, 0.10406957462213, -0.09215968531446, 0.69540012101253, 0.99956173327206, -0.12358542001404, + -0.79732779473535, -0.91582524736159, 0.96349973642406, 0.96640458041000, -0.79942778496547, 0.64323902822857, -0.11566039853896, 0.28587846253726, + -0.39922954514662, 0.94129601616966, 0.99089197565987, -0.92062625581587, 0.28631285179909, -0.91035047143603, -0.83302725605608, -0.67330410892084, + 0.95404443402072, 0.49162765398743, -0.06449863579434, 0.03250560813135, -0.99575054486311, 0.42389784469507, -0.65501142790847, 0.82546114655624, + -0.81254441908887, -0.51627234660629, -0.99646369485481, 0.84490533520752, 0.00287840603348, 0.64768261158166, 0.70176989408455, -0.20453028573322, + 0.96361882270190, 0.40706967140989, -0.68883758192426, 0.91338958840772, -0.34875585502238, 0.71472290693300, 0.91980081243087, 0.66507455644919, + -0.99009048343881, 0.85868021604848, 0.68865791458395, 0.55660316809678, -0.99484402129368, -0.20052559254934, 0.94214511408023, -0.99696425367461, + -0.67414626793544, 0.49548221180078, -0.47339353684664, -0.85904328834047, 0.14323651387360, -0.94145598222488, -0.29268293575672, 0.05759224927952, + 0.43793861458754, -0.78904969892724, -0.36345126374441, 0.64874435357162, -0.08750604656825, 0.97686944362527, -0.96495267812511, -0.53960305946511, + 0.55526940659947, 0.78891523734774, 0.73538215752630, 0.96452072373404, -0.30889773919437, -0.80664389776860, 0.03574995626194, -0.97325616900959, + 0.98720684660488, 0.48409133691962, -0.81689296271203, -0.90827703628298, 0.67866860118215, 0.81284503870856, -0.15808569732583, 0.85279555024382, + 0.80723395114371, -0.24717418514605, 0.47788757329038, -0.46333147839295, 0.96367554763201, 0.38486749303242, -0.99143875716818, -0.24945277239809, + 0.83081876925833, -0.94780851414763, -0.58753191905341, 0.01290772389163, 0.95538108220960, -0.85557052096538, -0.96490920476211, -0.64020970923102, + -0.97327101028521, 0.12378128133110, 0.91400366022124, 0.57972471346930, -0.99925837363824, 0.71084847864067, -0.86875903507313, -0.20291699203564, + -0.26240034795124, -0.68264554369108, -0.24664412953388, -0.87642273115183, 0.02416275806869, 0.27192914288905, 0.82068619590515, -0.85087787994476, + 0.88547373760759, -0.89636802901469, -0.18173078152226, -0.26152145156800, 0.09355476558534, 0.54845123045604, -0.54668414224090, 0.95980774020221, + 0.37050990604091, -0.59910140383171, -0.70373594262891, 0.91227665827081, -0.34600785879594, -0.99441426144200, -0.68774481731008, -0.30238837956299, + -0.26843291251234, 0.83115668004362, 0.49072334613242, -0.45359708737775, 0.38975993093975, 0.95515358099121, -0.97757125224150, 0.05305894580606, + -0.17325552859616, -0.92770672250494, 0.99948035025744, 0.58285545563426, -0.64946246527458, 0.68645507104960, -0.12016920576437, -0.57147322153312, + -0.58947456517751, -0.34847132454388, -0.41815140454465, 0.16276422358861, 0.99885650204884, 0.11136095490444, -0.56649614128386, -0.90494866361587, + 0.94138021032330, 0.35281916733018, -0.75725076534641, 0.53650549640587, 0.20541973692630, -0.94435144369918, 0.99980371023351, 0.79835913565599, + 0.29078277605775, 0.35393777921520, -0.62858772103030, 0.38765693387102, 0.43440904467688, -0.98546330463232, -0.98298583762390, 0.21021524625209, + 0.19513029146934, -0.94239832251867, -0.95476662400101, 0.98364554179143, 0.93379635304810, -0.70881994583682, -0.85235410573336, -0.08342347966410, + -0.86425093011245, -0.45795025029466, 0.38879779059045, 0.97274429344593, 0.92045124735495, -0.62433652524220, 0.89162532251878, 0.54950955570563, + -0.36834336949252, 0.96458298020975, 0.93891760988045, -0.89968353740388, 0.99267657565094, -0.03757034316958, -0.94063471614176, 0.41332338538963, + 0.99740224117019, -0.16830494996370, -0.35899413170555, -0.46633226649613, 0.05237237274947, -0.25640361602661, 0.36703583957424, -0.38653265641875, + 0.91653180367913, -0.30587628726597, 0.69000803499316, 0.90952171386132, -0.38658751133527, 0.99501571208985, -0.29250814029851, 0.37444994344615, + -0.60182204677608, 0.86779651036123, -0.97418588163217, 0.96468523666475, 0.88461574003963, 0.57508405276414, 0.05198933055162, 0.21269661669964, + -0.53499621979720, 0.97241553731237, -0.49429560226497, 0.98183865291903, -0.98935142339139, -0.40249159006933, -0.98081380091130, -0.72856895534041, + -0.27338148835532, 0.99950922447209, 0.06310802338302, -0.54539587529618, -0.20461677199539, -0.14209977628489, 0.66223843141647, 0.72528579940326, + -0.84764345483665, 0.02372316801261, -0.89039863483811, 0.88866581484602, 0.95903308477986, 0.76744927173873, 0.73504123909879, -0.03747203173192, + -0.31744434966056, -0.36834111883652, -0.34110827591623, 0.40211222807691, 0.47803883714199, -0.39423219786288, 0.98299195879514, 0.01989791390047, + -0.30963073129751, -0.18076720599336, 0.99992588229018, -0.26281872094289, -0.93149731080767, -0.98313162570490, 0.99923472302773, -0.80142993767554, + -0.26024169633417, -0.75999759855752, -0.35712514743563, 0.19298963768574, -0.99899084509530, 0.74645156992493, 0.86557171579452, 0.55593866696299, + 0.33408042438752, 0.86185953874709, 0.99010736374716, 0.04602397576623, -0.66694269691195, -0.91643611810148, 0.64016792079480, 0.15649530836856, + 0.99570534804836, 0.45844586038111, -0.63431466947340, 0.21079116459234, -0.07706847005931, -0.89581437101329, 0.98590090577724, 0.88241721133981, + 0.80099335254678, -0.36851896710853, 0.78368131392666, 0.45506999802597, 0.08707806671691, 0.80938994918745, -0.86811883080712, 0.39347308654705, + -0.39466529740375, -0.66809432114456, 0.97875325649683, -0.72467840967746, -0.95038560288864, 0.89563219587625, 0.17005239424212, 0.54683053962658, + -0.76910792026848, -0.96226617549298, 0.99743281016846, 0.42697157037567, 0.95437383549973, 0.97002324109952, 0.99578905365569, -0.54106826257356, + 0.28058259829990, -0.85361420634036, 0.85256524470573, -0.64567607735589, -0.50608540105128, -0.65846015480300, -0.97210735183243, -0.23095213067791, + 0.95424048234441, -0.99240147091219, -0.96926570524023, 0.73775654896574, 0.30872163214726, 0.41514960556126, -0.24523839572639, 0.63206633394807, + -0.33813265086024, -0.38661779441897, -0.05826828420146, -0.06940774188029, -0.22898461455054, 0.97054853316316, -0.18509915019881, 0.47565762892084, + -0.10488238045009, -0.87769947402394, -0.71886586182037, 0.78030982480538, 0.99793873738654, 0.90041310491497, 0.57563307626120, -0.91034337352097, + 0.28909646383717, 0.96307783970534, 0.42188998312520, 0.48148651230437, 0.93335049681047, -0.43537023883588, -0.97087374418267, 0.86636445711364, + 0.36722871286923, 0.65291654172961, -0.81093025665696, 0.08778370229363, -0.26240603062237, -0.92774095379098, 0.83996497984604, 0.55839849139647, + -0.99909615720225, -0.96024605713970, 0.74649464155061, 0.12144893606462, -0.74774595569805, -0.26898062008959, 0.95781667469567, -0.79047927052628, + 0.95472308713099, -0.08588776019550, 0.48708332746299, 0.99999041579432, 0.46332038247497, 0.10964126185063, -0.76497004940162, 0.89210929242238, + 0.57397389364339, 0.35289703373760, 0.75374316974495, 0.96705214651335, -0.59174397685714, -0.89405370422752, 0.75087906691890, -0.29612672982396, + -0.98607857336230, 0.25034911730023, -0.40761056640505, -0.90045573444695, 0.66929266740477, 0.98629493401748, -0.97463695257310, -0.00190223301301, + 0.90145509409859, 0.99781390365446, -0.87259289048043, 0.99233587353666, -0.91529461447692, -0.15698707534206, -0.03305738840705, -0.37205262859764, + 0.07223051368337, -0.88805001733626, 0.99498012188353, 0.97094358113387, -0.74904939500519, 0.99985483641521, 0.04585228574211, 0.99812337444082, + -0.89054954257993, -0.31791913188064, -0.83782144651251, 0.97637632547466, 0.33454804933804, -0.86231516800408, -0.99707579362824, 0.93237990079441, + -0.22827527843994, 0.18874759397997, 0.67248046289143, -0.03646211390569, -0.05146538187944, -0.92599700120679, 0.99947295749905, 0.93625229707912, + 0.66951124390363, 0.98905825623893, -0.99602956559179, -0.44654715757688, 0.82104905483590, 0.99540741724928, 0.99186510988782, 0.72023001312947, + -0.65284592392918, 0.52186723253637, 0.93885443798188, -0.74895312615259, 0.96735248738388, 0.90891816978629, -0.22225968841114, 0.57124029781228, + -0.44132783753414, -0.92688840659280, -0.85694974219574, 0.88844532719844, 0.91783042091762, -0.46356892383970, 0.72556974415690, -0.99899555770747, + -0.99711581834508, 0.58211560180426, 0.77638976371966, 0.94321834873819, 0.07717324253925, 0.58638399856595, -0.56049829194163, 0.82522301569036, + 0.98398893639988, 0.39467440420569, 0.47546946844938, 0.68613044836811, 0.65675089314631, 0.18331637134880, 0.03273375457980, -0.74933109564108, + -0.38684144784738, 0.51337349030406, -0.97346267944545, -0.96549364384098, -0.53282156061942, -0.91423265091354, 0.99817310731176, 0.61133572482148, + -0.50254500772635, -0.88829338134294, 0.01995873238855, 0.85223515096765, 0.99930381973804, 0.94578896296649, 0.82907767600783, -0.06323442598128, + -0.58660709669728, 0.96840773806582, -0.17573736667267, -0.48166920859485, 0.83434292401346, -0.13023450646997, 0.05946491307025, 0.20511047074866, + 0.81505484574602, -0.94685947861369, -0.44976380954860, 0.40894572671545, -0.89746474625671, 0.99846578838537, 0.39677256130792, -0.74854668609359, + -0.07588948563079, 0.74096214084170, 0.76343198951445, 0.41746629422634, -0.74490104699626, 0.94725911744610, 0.64880119792759, 0.41336660830571, + 0.62319537462542, -0.93098313552599, 0.42215817594807, -0.07712787385208, 0.02704554141885, -0.05417518053666, 0.80001773566818, 0.91542195141039, + -0.79351832348816, -0.36208897989136, 0.63872359151636, 0.08128252493444, 0.52890520960295, 0.60048872455592, 0.74238552914587, 0.04491915291044, + 0.99096131449250, -0.19451182854402, -0.80412329643109, -0.88513818199457, -0.64612616129736, 0.72198674804544, 0.11657770663191, -0.83662833815041, + -0.95053182488101, -0.96939905138082, -0.62228872928622, 0.82767262846661, 0.03004475787316, -0.99738896333384, -0.97987214341034, 0.36526129686425, + -0.99986980746200, -0.36021610299715, 0.89110648599879, -0.97894250343044, 0.10407960510582, 0.77357793811619, 0.95964737821728, -0.35435818285502, + 0.50843233159162, 0.96107691266205, 0.17006334670615, -0.76854025314829, 0.25872675063360, 0.99893303933816, -0.01115998681937, 0.98496019742444, + -0.79598702973261, 0.97138411318894, -0.99264708948101, -0.99542822402536, -0.99829663752818, 0.01877138824311, -0.70801016548184, 0.33680685948117, + -0.70467057786826, 0.93272777501857, 0.99846021905254, -0.98725746254433, -0.63364968534650, -0.16473594423746, -0.16258217500792, -0.95939125400802, + -0.43645594360633, -0.94805030113284, -0.99848471702976, 0.96245166923809, -0.16796458968998, -0.98987511890470, -0.87979225745213, -0.71725725041680, + 0.44183099021786, -0.93568974498761, 0.93310180125532, -0.99913308068246, -0.93941931782002, -0.56409379640356, -0.88590003188677, 0.47624600491382, + 0.99971463703691, -0.83889954253462, -0.75376385639978, 0.00814643438625, 0.93887685615875, -0.11284528204636, 0.85126435782309, 0.52349251543547, + 0.39701421446381, 0.81779634174316, -0.37024464187437, -0.87071656222959, -0.36024828242896, 0.34655735648287, -0.93388812549209, -0.84476541096429, + -0.65298804552119, -0.18439575450921, 0.11960319006843, 0.99899346780168, 0.94292565553160, 0.83163906518293, 0.75081145286948, -0.35533223142265, + 0.56721979748394, -0.24076836414499, 0.46857766746029, -0.30140233457198, 0.97312313923635, -0.99548191630031, -0.38299976567017, 0.98516909715427, + 0.41025800019463, 0.02116736935734, 0.09638062008048, 0.04411984381457, -0.85283249275397, 0.91475563922421, 0.88866808958124, -0.99735267083226, + -0.48202429536989, -0.96805608884164, 0.27572582416567, 0.58634753335832, -0.65889129659168, 0.58835634138583, 0.98838086953732, 0.99994349600236, + -0.20651349620689, 0.54593044066355, -0.62126416356920, -0.59893681700392, 0.20320105410437, -0.86879180355289, -0.97790548600584, 0.96290806999242, + 0.11112534735126, 0.21484763313301, -0.41368337314182, 0.28216837680365, 0.24133038992960, 0.51294362630238, -0.66393410674885, -0.08249679629081, + -0.53697829178752, -0.97649903936228, -0.97224737889348, 0.22081333579837, 0.87392477144549, -0.12796173740361, 0.19050361015753, 0.01602615387195, + -0.46353441212724, -0.95249041539006, -0.07064096339021, -0.94479803205886, -0.92444085484466, -0.10457590187436, -0.83822593578728, -0.01695043208885, + 0.75214681811150, -0.99955681042665, -0.42102998829339, 0.99720941999394, -0.72094786237696, -0.35008961934255, 0.78843311019251, 0.52851398958271, + 0.97394027897442, -0.26695944086561, 0.99206463477946, -0.57010120849429, 0.76789609461795, -0.76519356730966, -0.82002421836409, -0.73530179553767, + 0.81924990025724, 0.99698425250579, -0.26719850873357, 0.68903369776193, -0.43311260380975, 0.85321815947490, 0.99194979673836, 0.91876249766422, + -0.80692001248487, -0.32627540663214, 0.43080003649976, -0.21919095636638, 0.67709491937357, -0.95478075822906, 0.56151770568316, -0.70693811747778, + 0.10831862810749, -0.08628837174592, 0.91229417540436, -0.65987351408410, -0.48972893932274, 0.56289246362686, -0.89033658689697, -0.71656563987082, + 0.65269447475094, 0.65916004833932, 0.67439478141121, -0.81684380846796, -0.47770832416973, -0.16789556203025, -0.99715979260878, -0.93565784007648, + -0.90889593602546, 0.62034397054380, -0.06618622548177, -0.23812217221359, 0.99430266919728, 0.18812555317553, 0.97686402381843, -0.28664534366620, + 0.94813650221268, -0.97506640027128, -0.95434497492853, -0.79607978501983, -0.49104783137150, 0.32895214359663, 0.99881175120751, 0.88993983831354, + 0.50449166760303, -0.85995072408434, 0.47162891065108, -0.18680204049569, -0.62081581361840, 0.75000676218956, -0.43867015250812, 0.99998069244322, + 0.98630563232075, -0.53578899600662, -0.61510362277374, -0.89515019899997, -0.03841517601843, -0.69888815681179, -0.30102157304644, -0.07667808922205, + 0.41881284182683, 0.02188098922282, -0.86135454941237, 0.98947480909359, 0.67226861393788, -0.13494389011014, -0.70737398842068, -0.76547349325992, + 0.94044946687963, 0.09026201157416, -0.82386352534327, 0.08924768823676, -0.32070666698656, 0.50143421908753, 0.57593163224487, -0.98966422921509, + -0.36326018419965, 0.07440243123228, 0.99979044674350, -0.14130287347405, -0.92366023326932, -0.97979298068180, -0.44607178518598, -0.54233252016394, + 0.44226800932956, 0.71326756742752, 0.03671907158312, 0.63606389366675, 0.52175424682195, -0.85396826735705, -0.94701139690956, -0.01826348194255, + -0.98759606946049, 0.82288714303073, 0.87434794743625, 0.89399495655433, -0.93412041758744, 0.41374052024363, 0.96063943315511, 0.93116709541280, + 0.97534253457837, 0.86150930812689, 0.99642466504163, 0.70190043427512, -0.94705089665984, -0.29580042814306, 0.91599807087376, -0.98147830385781, +} + +-- Huffman codebooks (4.A.6.1 / 8.A.1): { lav, lengths, codes }; symbol index - lav = value +SbrTables.T_ENV15 = { + 60, + { 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 17, 18, 16, 17, 18, 17, 16, 16, 16, 16, 15, 14, 14, 13, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 14, 15, 16, 17, 16, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 }, + { + 262102, 262103, 262104, 262105, 262106, 262107, 524216, 524217, 524218, 524219, + 524220, 524221, 524222, 524223, 524224, 524225, 524226, 524227, 524228, 524229, + 524230, 524231, 524232, 524233, 524234, 524235, 524236, 524237, 524238, 524239, + 524240, 524241, 524242, 524243, 131046, 262100, 65520, 131049, 262101, 131047, + 65521, 65516, 65517, 65518, 32756, 16377, 16375, 8186, 8185, 4091, + 2044, 1020, 509, 253, 125, 61, 29, 13, 5, 1, + 0, 4, 12, 28, 60, 124, 252, 508, 1021, 4090, + 8184, 16374, 16376, 32757, 65519, 131048, 65522, 524244, 524245, 524246, + 524247, 524248, 524249, 524250, 524251, 524252, 524253, 524254, 524255, 524256, + 524257, 524258, 524259, 524260, 524261, 524262, 524263, 524264, 524265, 524266, + 524267, 524268, 524269, 524270, 524271, 524272, 524273, 524274, 524275, 524276, + 524277, 524278, 524279, 524280, 524281, 524282, 524283, 524284, 524285, 524286, + 524287, + }, +} +SbrTables.F_ENV15 = { + 60, + { 19, 19, 20, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 20, 19, 20, 19, 19, 20, 18, 20, 20, 20, 19, 20, 20, 20, 19, 20, 19, 18, 19, 18, 18, 17, 18, 17, 17, 17, 16, 16, 16, 15, 15, 14, 13, 13, 12, 12, 11, 10, 9, 9, 8, 7, 6, 5, 4, 3, 2, 2, 3, 4, 5, 6, 8, 8, 9, 10, 11, 11, 11, 12, 12, 13, 13, 14, 14, 16, 16, 17, 17, 18, 18, 18, 18, 18, 18, 18, 20, 19, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 20, 19, 20, 18, 20, 20, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 }, + { + 524263, 524264, 1048530, 1048531, 1048532, 1048533, 1048534, 1048535, 1048536, 524250, + 1048537, 1048538, 1048539, 1048540, 524251, 1048541, 524252, 524253, 1048542, 262116, + 1048543, 1048544, 1048545, 524254, 1048546, 1048547, 1048548, 524255, 1048549, 524256, + 262120, 524257, 262112, 262121, 131055, 262117, 131052, 131053, 131054, 65524, + 65523, 65520, 32759, 32758, 16378, 8186, 8185, 4090, 4088, 2041, + 1019, 508, 506, 251, 124, 60, 28, 12, 5, 1, + 0, 4, 13, 29, 61, 250, 252, 507, 1018, 2040, + 2042, 2043, 4089, 4091, 8184, 8187, 16376, 16377, 65521, 65522, + 131050, 131051, 262113, 262114, 262122, 262115, 262118, 262119, 262123, 1048550, + 524258, 1048551, 1048552, 1048553, 1048554, 1048555, 1048556, 524259, 1048557, 1048558, + 1048559, 1048560, 524260, 1048561, 262124, 1048562, 1048563, 524261, 524262, 1048564, + 1048565, 1048566, 1048567, 1048568, 1048569, 1048570, 1048571, 1048572, 1048573, 1048574, + 1048575, + }, +} +SbrTables.T_BAL15 = { + 24, + { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 12, 11, 9, 7, 5, 3, 1, 2, 4, 6, 8, 11, 12, 15, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 }, + { + 65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517, + 65518, 65519, 65520, 65521, 65522, 65523, 65524, 65506, 4092, 2044, + 510, 126, 30, 6, 0, 2, 14, 62, 254, 2045, + 4093, 32752, 65507, 65525, 65526, 65527, 65528, 65529, 65530, 131062, + 131063, 131064, 131065, 131066, 131067, 131068, 131069, 131070, 131071, + }, +} +SbrTables.F_BAL15 = { + 24, + { 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 16, 17, 14, 11, 11, 8, 7, 4, 2, 1, 3, 5, 6, 9, 11, 12, 15, 16, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19 }, + { + 262114, 262115, 262116, 262117, 262118, 262119, 262120, 262121, 262122, 262123, + 262124, 262125, 262126, 262127, 262128, 65527, 131056, 16380, 2046, 2044, + 254, 126, 14, 2, 0, 6, 30, 62, 510, 2045, + 4094, 32762, 65526, 262129, 262130, 262131, 262132, 262133, 262134, 262135, + 262136, 262137, 262138, 262139, 262140, 262141, 262142, 524286, 524287, + }, +} +SbrTables.T_ENV30 = { + 31, + { 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 17, 16, 16, 16, 14, 14, 14, 13, 12, 11, 8, 6, 4, 2, 1, 3, 5, 7, 9, 11, 13, 14, 14, 15, 16, 17, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 }, + { + 262125, 262126, 524254, 524255, 524256, 524257, 524258, 524259, 524260, 524261, + 524262, 524263, 524264, 524265, 524266, 524267, 524268, 131060, 65527, 65529, + 65528, 16379, 16378, 16376, 8186, 4092, 2044, 254, 62, 14, + 2, 0, 6, 30, 126, 510, 2045, 8187, 16377, 16380, + 32762, 65526, 131061, 262124, 524269, 524270, 524271, 524272, 524273, 524274, + 524275, 524276, 524277, 524278, 524279, 524280, 524281, 524282, 524283, 524284, + 524285, 524286, 524287, + }, +} +SbrTables.F_ENV30 = { + 31, + { 20, 20, 20, 20, 20, 20, 20, 18, 19, 19, 19, 19, 18, 18, 20, 19, 17, 18, 17, 16, 16, 15, 14, 12, 11, 10, 9, 8, 6, 4, 2, 1, 3, 5, 8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18, 18, 20, 19, 19, 19, 20, 19, 19, 20, 20, 20, 20, 20, 20 }, + { + 1048560, 1048561, 1048562, 1048563, 1048564, 1048565, 1048566, 262131, 524277, 524270, + 524271, 524278, 262132, 262130, 1048567, 524272, 131061, 262128, 131060, 65527, + 65526, 32760, 16379, 4093, 2045, 1021, 509, 253, 62, 14, + 2, 0, 6, 30, 252, 508, 1020, 2044, 4092, 8188, + 16378, 32761, 32762, 65528, 65529, 131062, 131063, 262133, 262134, 262129, + 1048568, 524273, 524274, 524275, 1048569, 524279, 524276, 1048570, 1048571, 1048572, + 1048573, 1048574, 1048575, + }, +} +SbrTables.T_BAL30 = { + 12, + { 13, 13, 13, 13, 13, 13, 13, 12, 8, 7, 4, 3, 1, 2, 5, 6, 9, 13, 13, 13, 13, 13, 13, 14, 14 }, + { + 8178, 8179, 8180, 8181, 8182, 8183, 8184, 4088, 254, 126, + 14, 6, 0, 2, 30, 62, 510, 8185, 8186, 8187, + 8188, 8189, 8190, 16382, 16383, + }, +} +SbrTables.F_BAL30 = { + 12, + { 13, 13, 13, 13, 13, 14, 14, 11, 8, 7, 4, 2, 1, 3, 5, 6, 9, 12, 13, 14, 14, 14, 14, 14, 14 }, + { + 8183, 8184, 8185, 8186, 8187, 16376, 16377, 2044, 254, 126, + 14, 2, 0, 6, 30, 62, 510, 4090, 8182, 16378, + 16379, 16380, 16381, 16382, 16383, + }, +} +SbrTables.T_NOISE30 = { + 31, + { 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 11, 8, 6, 4, 3, 1, 2, 5, 8, 10, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14 }, + { + 8142, 8143, 8144, 8145, 8146, 8147, 8148, 8149, 8150, 8151, + 8152, 8153, 8154, 8155, 8156, 8157, 8158, 8159, 8160, 8161, + 8162, 8163, 8164, 8165, 8166, 8167, 2034, 253, 62, 14, + 6, 0, 2, 30, 252, 1016, 8140, 8168, 8169, 8170, + 8171, 8172, 8141, 8173, 8174, 8175, 8176, 8177, 8178, 8179, + 8180, 8181, 8182, 8183, 8184, 8185, 8186, 8187, 8188, 8189, + 8190, 16382, 16383, + }, +} +SbrTables.T_NOISEBAL30 = { + 12, + { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 5, 2, 1, 3, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }, + { + 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, + 28, 2, 0, 6, 58, 246, 247, 248, 249, 250, + 251, 252, 253, 254, 255, + }, +} +SbrTables.IID_DF = { + 14, + { 17, 17, 17, 17, 16, 15, 13, 10, 9, 7, 6, 5, 4, 3, 1, 3, 4, 5, 6, 6, 8, 11, 13, 14, 14, 15, 17, 18, 18 }, + { + 131067, 131068, 131069, 131066, 65532, 32764, 8189, 1022, 510, 126, + 60, 29, 13, 5, 0, 4, 12, 28, 61, 62, + 254, 2046, 8188, 16380, 16381, 32765, 131070, 262142, 262143, + }, +} +SbrTables.IID_DT = { + 14, + { 19, 19, 19, 20, 20, 20, 17, 15, 12, 10, 8, 6, 4, 2, 1, 3, 5, 7, 9, 11, 13, 14, 17, 19, 20, 20, 20, 20, 20 }, + { + 524281, 524282, 524283, 1048568, 1048569, 1048570, 131069, 32766, 4094, 1022, + 254, 62, 14, 2, 0, 6, 30, 126, 510, 2046, + 8190, 16382, 131068, 524280, 1048571, 1048572, 1048573, 1048574, 1048575, + }, +} +SbrTables.IID_FINE_DF = { + 30, + { 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 16, 16, 15, 14, 14, 13, 12, 12, 11, 10, 10, 8, 7, 6, 5, 4, 3, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 16, 17, 17, 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18 }, + { + 130740, 130741, 130422, 130423, 130420, 130421, 130698, 130699, 130696, 65152, + 130742, 65154, 65208, 32578, 32686, 16303, 8145, 8169, 4073, 2026, + 2043, 1019, 507, 511, 124, 60, 28, 12, 0, 1, + 1, 2, 1, 13, 29, 61, 125, 252, 508, 1020, + 1012, 2027, 4074, 8170, 8150, 16336, 32687, 32579, 65209, 65155, + 130743, 65153, 130697, 130702, 130703, 130700, 130701, 130738, 130739, 130736, + 130737, + }, +} +SbrTables.IID_FINE_DT = { + 30, + { 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, 14, 13, 13, 13, 12, 12, 11, 10, 9, 9, 7, 6, 5, 3, 1, 2, 5, 6, 7, 8, 9, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 }, + { + 20180, 20181, 20174, 20175, 20172, 20182, 20184, 20294, 20320, 10008, + 10009, 10084, 10085, 10093, 10161, 5047, 5078, 2503, 2537, 2541, + 1262, 1271, 632, 313, 154, 159, 32, 17, 10, 3, + 1, 0, 11, 18, 33, 76, 155, 314, 633, 624, + 1263, 1250, 2538, 2520, 5079, 5072, 10162, 10146, 10010, 10011, + 20326, 20327, 20321, 20295, 20185, 20183, 20173, 20178, 20179, 20176, + 20177, + }, +} +SbrTables.ICC_DF = { + 7, + { 14, 14, 12, 10, 7, 5, 3, 1, 2, 4, 6, 8, 9, 11, 13 }, + { + 16383, 16382, 4094, 1022, 126, 30, 6, 0, 2, 14, + 62, 254, 510, 2046, 8190, + }, +} +SbrTables.ICC_DT = { + 7, + { 14, 13, 11, 9, 7, 5, 3, 1, 2, 4, 6, 8, 10, 12, 14 }, + { + 16382, 8190, 2046, 510, 126, 30, 6, 0, 2, 14, + 62, 254, 1022, 4094, 16383, + }, +} + +return SbrTables diff --git a/src/Rosetta/codecs/audio/Aac/init.luau b/src/Rosetta/codecs/audio/Aac/init.luau index 8b79e4e..c443ade 100644 --- a/src/Rosetta/codecs/audio/Aac/init.luau +++ b/src/Rosetta/codecs/audio/Aac/init.luau @@ -1,11 +1,13 @@ --!strict --!optimize 2 --- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): AudioSpecificConfig, ADTS, raw_data_block. --- Written from the standard; no third-party code. HE-AAC decodes its AAC-LC core at the core rate. Not supported: Main --- prediction, LTP, SSR, coupling channels, 960-sample frames, ER / USAC (UNSUPPORTED). +-- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): AudioSpecificConfig, ADTS, raw_data_block; +-- HE-AAC (SBR, Aac/Sbr) and HE-AACv2 (baseline PS, Aac/Ps) at the doubled rate, signalled explicitly or found in the +-- stream (Decoder:probe). Written from the standard. Not supported: Main prediction, LTP, SSR, coupling channels, +-- 960-sample frames, down-sampled SBR, ER / USAC (UNSUPPORTED). local Err = require("@rosetta/core/Err") local Bits = require("@rosetta/core/Bits") local Decode = require("@rosetta/codecs/audio/Aac/Decode") +local Sbr = require("@rosetta/codecs/audio/Aac/Sbr") local Aac = {} @@ -218,17 +220,25 @@ function Aac.adts(b: buffer, o: number, limit: number): Adts? } end +export type DecoderOptions = { + sbr: boolean?, -- false: decode only the AAC-LC core of HE-AAC at the core rate (faster); default true +} + export type Decoder = { config: Config, - channels: number, - sampleRate: number, - frameLength: number, + channels: number, -- output channels (2 for a mono core with parametric stereo) + sampleRate: number, -- output rate (twice the core rate with SBR) + frameLength: number, -- samples per channel per frame: 1024, 2048 with SBR pcm: { number }, - sbr: boolean, + sbr: boolean, -- SBR data seen in the stream + sbrOutput: boolean, -- SBR decoding on (sampleRate is the SBR rate) + ps: boolean, -- parametric stereo output onExtension: ((kind: number, element: number, buf: buffer, bitPos: number, bits: number) -> ())?, decode: (self: Decoder, buf: buffer, offset: number?, length: number?) -> (number?, Err.Err?), + probe: (self: Decoder, buf: buffer, offset: number?, length: number?) -> boolean, reset: (self: Decoder) -> (), state: Decode.State, + allowSbr: boolean, } local function decode(self: Decoder, buf: buffer, offset: number?, length: number?): (number?, Err.Err?) @@ -246,16 +256,17 @@ local function decode(self: Decoder, buf: buffer, offset: number?, length: numbe if st.sbr then self.sbr = true end + local fl = self.frameLength if chans == 0 then - for i = 1, 1024 * self.channels do + for i = 1, fl * self.channels do st.pcm[i] = 0 end - return 1024, nil + return fl, nil end if chans ~= self.channels then return nil, Err.new(Err.CORRUPT, string.format("frame has %d channels, the configuration %d", chans, self.channels), o) end - return 1024, nil + return fl, nil end local function reset(self: Decoder) @@ -265,9 +276,71 @@ local function reset(self: Decoder) end c.prevShape = 0 end + for _, e in self.state.sbrElems do + Sbr.restart(e) + end end -function Aac.decoder(asc: any): (Decoder?, Err.Err?) +local function enableSbr(self: Decoder, rate: number, ps: boolean) + local st = self.state + st.sbrOn, st.sbrRate = true, rate + st.psOut = ps and self.config.channels == 1 + st.sbrElems = {} + self.sbrOutput = true + self.ps = st.psOut + self.sampleRate = rate + self.channels = if st.psOut then 2 else self.config.channels + self.frameLength = 2048 +end + +-- implicit signalling (14496-3 1.6.5.3): SBR / PS data in this frame switch the output to the doubled rate (and a mono +-- core with PS to stereo); call on the first frames before reading sampleRate / channels. true when it switched. +local function probe(self: Decoder, buf: buffer, offset: number?, length: number?): boolean + local cfg = self.config + if not self.allowSbr or self.ps or (self.sbrOutput and cfg.channels ~= 1) or type(buf) ~= "buffer" then + return false + end + local o = offset or 0 + local n = length or (buffer.len(buf) - o) + if o < 0 or n < 0 or o + n > buffer.len(buf) or cfg.sampleRate * 2 > 96000 then + return false + end + local scratch = Decode.state(cfg.sfIndex) + scratch.channels = cfg.channels + scratch.map = self.state.map + scratch.sbrOn = true + scratch.sbrRate = cfg.sampleRate * 2 + scratch.psOut = cfg.channels == 1 + local chans = Decode.frame(scratch, buf, o, n) + if not chans or not scratch.sbr then + return false + end + local ps = false + for _, e in scratch.sbrElems do + if e.ps and e.ps.haveConfig then + ps = true + end + end + if self.sbrOutput and not ps then + return false + end + enableSbr(self, self.config.sampleRate * 2, ps) + return true +end + +-- probe the first frames (offs / sizes: 1-based packet lists) then reset; true when the output format changed +function Aac.probeFrames(d: Decoder, buf: buffer, offs: { number }, sizes: { number }, maxFrames: number?): boolean + local changed = false + for i = 1, math.min(#offs, maxFrames or 8) do + if d:probe(buf, offs[i], sizes[i]) then + changed = true + end + end + return changed +end + +function Aac.decoder(asc: any, opts: DecoderOptions?): (Decoder?, Err.Err?) + local allowSbr = not (opts and opts.sbr == false) local cfg: Config if type(asc) == "buffer" then local c, err = Aac.config(asc) @@ -301,18 +374,28 @@ function Aac.decoder(asc: any): (Decoder?, Err.Err?) if cfg.channelConfig >= 3 then st.map = CONFIG_MAP[cfg.channelConfig] end - return { + local d: Decoder = { config = cfg, channels = cfg.channels, sampleRate = cfg.sampleRate, frameLength = 1024, pcm = st.pcm, sbr = cfg.sbr, + sbrOutput = false, + ps = false, onExtension = nil, decode = decode, + probe = probe, reset = reset, state = st, - }, nil + allowSbr = allowSbr, + } + -- explicit signalling; an extension rate equal to the core rate is down-sampled SBR (the core is decoded alone) + local ext = cfg.extensionRate or cfg.sampleRate * 2 + if allowSbr and cfg.sbr and ext == cfg.sampleRate * 2 and ext <= 96000 then + enableSbr(d, ext, cfg.ps) + end + return d, nil end return Aac diff --git a/src/Rosetta/containers/AudioWrap.luau b/src/Rosetta/containers/AudioWrap.luau index 4f82bd4..0b3da58 100644 --- a/src/Rosetta/containers/AudioWrap.luau +++ b/src/Rosetta/containers/AudioWrap.luau @@ -310,6 +310,13 @@ local function adts(track: Types.Track, packets: { Types.Packet }): (buffer?, Er profile = b0 // 8 -- audio object type rateIndex = (b0 % 8) * 2 + b1 // 128 chanConfig = (b1 // 8) % 16 + -- HE-AAC (5) / HE-AACv2 (29), 14496-3 1.6.2.1: ADTS carries the AAC-LC core (the decoder finds SBR / PS in it) + if (profile == 5 or profile == 29) and rateIndex ~= 15 and buffer.len(c) >= 4 then + local v = buffer.readu8(c, 1) * 65536 + buffer.readu8(c, 2) * 256 + buffer.readu8(c, 3) + local extIndex = (v // 2 ^ 15) % 16 -- after aot (5), sfi (4), channels (4): bits 13 .. 16 + local core = if extIndex == 15 then -1 else (v // 2 ^ 10) % 32 -- bits 17 .. 21 + profile = if core == 2 then 2 else -1 + end else local rates = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 } profile = 2 diff --git a/src/Rosetta/init.luau b/src/Rosetta/init.luau index 6af74a1..a0db2dd 100644 --- a/src/Rosetta/init.luau +++ b/src/Rosetta/init.luau @@ -304,12 +304,12 @@ end -- api hooks (a format never requires another format): opts.decodeImage / encodeImage, opts.plugins, opts.decodeModule, -- opts.openAudio (containers/AudioWrap: PCM -> WAV, Opus / Vorbis -> Ogg, AAC -> ADTS, MP3 / FLAC / AC-3 as they are) -local function openAudio(demuxer: any, track: any): (any, Err.Err?) +local function openAudio(demuxer: any, track: any, sbr: boolean?): (any, Err.Err?) local buf, fmt, err = AudioWrap.wrap(demuxer, track) if not buf then return nil, err end - local dec, oerr = F.open(buf, { format = fmt, category = "audio" }) + local dec, oerr = F.open(buf, { format = fmt, category = "audio", sbr = sbr }) -- Matroska CodecDelay: ffmpeg drops it for MPEG audio / AAC; Opus has its own pre-skip and Vorbis's delay is -- already the decoder's own (its first packet returns nothing) -- MP4 edit lists add the play length (the encoder's end padding is cut, as ffmpeg does) @@ -332,7 +332,11 @@ local function withHooks(o: Options): Options c.plugins = F.plugins end if c.openAudio == nil then - c.openAudio = openAudio + -- opts.sbr reaches the AAC decoder of a video's audio track + local sbr = o.sbr + c.openAudio = function(demuxer: any, track: any): (any, Err.Err?) + return openAudio(demuxer, track, sbr) + end end if c.decodeImage == nil then c.decodeImage = function(b: buffer): (any, Err.Err?)