--!strict --!native --!optimize 2 -- codecs/audio/Musepack/Decode: one Musepack SV7 / SV8 frame (1152 samples per channel). -- A line-by-line port of libmpcdec's mpc_decoder.c, requant.c and synth_filter.c (musepack_src_r475, BSD-3-Clause, (c) The -- Musepack Development Team, licenses/libmpcdec-BSD.txt). The caller pads the buffer by PAD bytes past `limit`. local T = require("@rosetta/codecs/audio/Musepack/Tables") local readu32, bswap, extract, band, bor, rshift, lshift, bnot, bxor = buffer.readu32, bit32.byteswap, bit32.extract, bit32.band, bit32.bor, bit32.rshift, bit32.lshift, bit32.bnot, bit32.bxor local abs = math.abs local Decode = {} Decode.PAD = 8192 Decode.DELAY = 481 -- MPC_DECODER_SYNTH_DELAY Decode.FRAME = 1152 local L = T.lut local HDR, SCFI7, DSCF7 = L(T.HuffHdr), L(T.HuffSCFI), L(T.HuffDSCF) local Q7: { { T.Lut } } = { { L(T.HuffQ1_0), L(T.HuffQ1_1) }, { L(T.HuffQ2_0), L(T.HuffQ2_1) }, { L(T.HuffQ3_0), L(T.HuffQ3_1) }, { L(T.HuffQ4_0), L(T.HuffQ4_1) }, { L(T.HuffQ5_0), L(T.HuffQ5_1) }, { L(T.HuffQ6_0), L(T.HuffQ6_1) }, { L(T.HuffQ7_0), L(T.HuffQ7_1) }, } local BANDS8 = L(T.H_Bands, T.S_Bands) local SCFI8 = { L(T.H_SCFI_1, T.S_SCFI_1), L(T.H_SCFI_2, T.S_SCFI_2) } local DSCF8 = { L(T.H_DSCF_1, T.S_DSCF_1), L(T.H_DSCF_2, T.S_DSCF_2) } local RES8 = { L(T.H_Res_1, T.S_Res_1), L(T.H_Res_2, T.S_Res_2) } local Q1_8 = L(T.H_Q1, T.S_Q1) local Q9UP = L(T.H_Q9up, T.S_Q9up) -- mpc_can_Q[res - 2][0 / 1] for res 2 and 5 .. 8; res 3 / 4 use Q34[res - 2] local Q8: { [number]: { T.Lut } } = { [2] = { L(T.H_Q2_1, T.S_Q2_1), L(T.H_Q2_2, T.S_Q2_2) }, [5] = { L(T.H_Q5_1, T.S_Q5_1), L(T.H_Q5_2, T.S_Q5_2) }, [6] = { L(T.H_Q6_1, T.S_Q6_1), L(T.H_Q6_2, T.S_Q6_2) }, [7] = { L(T.H_Q7_1, T.S_Q7_1), L(T.H_Q7_2, T.S_Q7_2) }, [8] = { L(T.H_Q8_1, T.S_Q8_1), L(T.H_Q8_2, T.S_Q8_2) }, } local Q34 = { L(T.H_Q3, T.S_Q3), L(T.H_Q4, T.S_Q4) } local THRES = { [2] = 3, [5] = 1, [6] = 3, [7] = 4, [8] = 8 } local CC, DC, RES_BIT = T.CC, T.DC, T.RES_BIT local CNK, CNK_LEN, CNK_LOST, LOG2, LOG2_LOST = T.CNK, T.CNK_LEN, T.CNK_LOST, T.LOG2, T.LOG2_LOST -- the f32 rounding of the C tables (libmpcdec decodes in single precision) local F32 = buffer.create(4) local function f32(x: number): number buffer.writef32(F32, 0, x) return buffer.readf32(F32, 0) end for i, v in CC do CC[i] = f32(v) end -- SCF[i]: the scalefactor of index i (0 .. 255, the index's low byte): 2^-15 * 0.83298...^(s - 1), s = i as int8 -- (libmpcdec's mpc_decoder_scale_output in float mode: 1 dB steps, the table filled outwards from index 1) local SCF = table.create(256, 0) do local f1 = 1 / 32768 local f2 = f1 SCF[2] = f32(f1) f1 *= 0.83298066476582673961 f2 *= 1 / 0.83298066476582673961 for n = 1, 128 do SCF[band(1 + n, 255) + 1] = f32(f1) SCF[band(1 - n, 255) + 1] = f32(f2) f1 *= 0.83298066476582673961 f2 *= 1 / 0.83298066476582673961 end end -- the synthesis window D (Di_opt / 65536), 32 x 16, row-major local DW = table.create(512, 0) for i, v in T.DI do DW[i] = f32(v / 65536) end local B: buffer = buffer.create(0) local P = 0 local function bits(n: number): number if n <= 0 then return 0 end if n > 24 then local hi = bits(n - 16) return hi * 65536 + bits(16) end local w = bswap(readu32(B, rshift(P, 3))) local v = extract(w, 32 - band(P, 7) - n, n) P += n return v end local function peek16(): number local w = bswap(readu32(B, rshift(P, 3))) return extract(w, 16 - band(P, 7), 16) end local function huff(t: T.Lut): number local code = peek16() local k = rshift(code, 8) + 1 local n = t.len[k] if n > 0 then P += n return t.val[k] end local j = t.val[k] local codes = t.codes while code < codes[j] do j += 1 end n = t.lens[j] P += n local sym = t.sym if sym then return sym[band(t.vals[j] - rshift(code, 16 - n), 255) + 1] or 0 end return t.vals[j] end -- mpc_bits_log_dec: 0 .. max local function logDec(max: number): number if max <= 0 then return 0 end local v = 0 local len, lost = LOG2[max], LOG2_LOST[max] if len > 1 then v = bits(len - 1) end if v >= lost then v = v * 2 + bits(1) - lost end return v end -- mpc_bits_enum_dec: an n-bit word with k bits set local function enumDec(k: number, n: number): number local out = 0 local code = bits(CNK_LEN[k][n] - 1) local lost = CNK_LOST[k][n] if code >= lost then code = code * 2 + bits(1) - lost end repeat n -= 1 local c = CNK[k][n + 1] if code >= c then out = bor(out, lshift(1, n)) code -= c k -= 1 end until k <= 0 or n <= 0 return out end -- mpc_random_int: two polynomial counters, XORed local PARITY = table.create(256, 0) for i = 0, 255 do local p, v = 0, i while v > 0 do p = bxor(p, band(v, 1)) v = rshift(v, 1) end PARITY[i + 1] = p end -- mpc_compute_new_V: 32 subband samples Y[y .. y + 31] -> 64 V values at V[o .. o + 63] (the ISO 11172-3 matrixing -- through Byeong Gi Lee's fast DCT) -- (two halves: as one function, its native lowering fails register allocation and the whole function runs -- interpreted) local function newVEven(Y: { number }, y: number, V: { number }, o: number) local A00 = Y[y] + Y[y + 31] local A01 = Y[y + 1] + Y[y + 30] local A02 = Y[y + 2] + Y[y + 29] local A03 = Y[y + 3] + Y[y + 28] local A04 = Y[y + 4] + Y[y + 27] local A05 = Y[y + 5] + Y[y + 26] local A06 = Y[y + 6] + Y[y + 25] local A07 = Y[y + 7] + Y[y + 24] local A08 = Y[y + 8] + Y[y + 23] local A09 = Y[y + 9] + Y[y + 22] local A10 = Y[y + 10] + Y[y + 21] local A11 = Y[y + 11] + Y[y + 20] local A12 = Y[y + 12] + Y[y + 19] local A13 = Y[y + 13] + Y[y + 18] local A14 = Y[y + 14] + Y[y + 17] local A15 = Y[y + 15] + Y[y + 16] local B00 = A00 + A15 local B01 = A01 + A14 local B02 = A02 + A13 local B03 = A03 + A12 local B04 = A04 + A11 local B05 = A05 + A10 local B06 = A06 + A09 local B07 = A07 + A08 local B08 = (A00 - A15) * 0.5024192929 local B09 = (A01 - A14) * 0.5224986076 local B10 = (A02 - A13) * 0.5669440627 local B11 = (A03 - A12) * 0.6468217969 local B12 = (A04 - A11) * 0.7881546021 local B13 = (A05 - A10) * 1.0606776476 local B14 = (A06 - A09) * 1.7224471569 local B15 = (A07 - A08) * 5.1011486053 A00 = B00 + B07 A01 = B01 + B06 A02 = B02 + B05 A03 = B03 + B04 A04 = (B00 - B07) * 0.5097956061 A05 = (B01 - B06) * 0.6013448834 A06 = (B02 - B05) * 0.8999761939 A07 = (B03 - B04) * 2.5629155636 A08 = B08 + B15 A09 = B09 + B14 A10 = B10 + B13 A11 = B11 + B12 A12 = (B08 - B15) * 0.5097956061 A13 = (B09 - B14) * 0.6013448834 A14 = (B10 - B13) * 0.8999761939 A15 = (B11 - B12) * 2.5629155636 B00 = A00 + A03 B01 = A01 + A02 B02 = (A00 - A03) * 0.5411961079 B03 = (A01 - A02) * 1.3065630198 B04 = A04 + A07 B05 = A05 + A06 B06 = (A04 - A07) * 0.5411961079 B07 = (A05 - A06) * 1.3065630198 B08 = A08 + A11 B09 = A09 + A10 B10 = (A08 - A11) * 0.5411961079 B11 = (A09 - A10) * 1.3065630198 B12 = A12 + A15 B13 = A13 + A14 B14 = (A12 - A15) * 0.5411961079 B15 = (A13 - A14) * 1.3065630198 A00 = B00 + B01 A01 = (B00 - B01) * 0.7071067691 A02 = B02 + B03 A03 = (B02 - B03) * 0.7071067691 A04 = B04 + B05 A05 = (B04 - B05) * 0.7071067691 A06 = B06 + B07 A07 = (B06 - B07) * 0.7071067691 A08 = B08 + B09 A09 = (B08 - B09) * 0.7071067691 A10 = B10 + B11 A11 = (B10 - B11) * 0.7071067691 A12 = B12 + B13 A13 = (B12 - B13) * 0.7071067691 A14 = B14 + B15 A15 = (B14 - B15) * 0.7071067691 V[o + 48] = -A00 V[o] = A01 V[o + 8] = A03 V[o + 40] = -A02 - A03 V[o + 12] = A07 local t = A05 + A07 V[o + 4] = t V[o + 36] = -(t + A06) V[o + 44] = -A04 - A06 - A07 V[o + 14] = A15 t = A11 + A15 V[o + 10] = t V[o + 6] = t + A13 t = A09 + A13 + A15 V[o + 2] = t t = -t - A14 V[o + 34] = t V[o + 38] = t + A09 - A10 - A11 t = -(A12 + A14 + A15) V[o + 46] = t - A08 V[o + 42] = t - A10 - A11 end local function newVOdd(Y: { number }, y: number, V: { number }, o: number) local A00, A01, A02, A03, A04, A05, A06, A07, A08, A09, A10, A11, A12, A13, A14, A15 local B00, B01, B02, B03, B04, B05, B06, B07, B08, B09, B10, B11, B12, B13, B14, B15 local t A00 = (Y[y] - Y[y + 31]) * 0.5006030202 A01 = (Y[y + 1] - Y[y + 30]) * 0.5054709315 A02 = (Y[y + 2] - Y[y + 29]) * 0.5154473186 A03 = (Y[y + 3] - Y[y + 28]) * 0.5310425758 A04 = (Y[y + 4] - Y[y + 27]) * 0.5531039238 A05 = (Y[y + 5] - Y[y + 26]) * 0.5829349756 A06 = (Y[y + 6] - Y[y + 25]) * 0.6225041151 A07 = (Y[y + 7] - Y[y + 24]) * 0.6748083234 A08 = (Y[y + 8] - Y[y + 23]) * 0.7445362806 A09 = (Y[y + 9] - Y[y + 22]) * 0.8393496275 A10 = (Y[y + 10] - Y[y + 21]) * 0.9725682139 A11 = (Y[y + 11] - Y[y + 20]) * 1.1694399118 A12 = (Y[y + 12] - Y[y + 19]) * 1.4841645956 A13 = (Y[y + 13] - Y[y + 18]) * 2.0577809811 A14 = (Y[y + 14] - Y[y + 17]) * 3.4076085091 A15 = (Y[y + 15] - Y[y + 16]) * 10.1900081635 B00 = A00 + A15 B01 = A01 + A14 B02 = A02 + A13 B03 = A03 + A12 B04 = A04 + A11 B05 = A05 + A10 B06 = A06 + A09 B07 = A07 + A08 B08 = (A00 - A15) * 0.5024192929 B09 = (A01 - A14) * 0.5224986076 B10 = (A02 - A13) * 0.5669440627 B11 = (A03 - A12) * 0.6468217969 B12 = (A04 - A11) * 0.7881546021 B13 = (A05 - A10) * 1.0606776476 B14 = (A06 - A09) * 1.7224471569 B15 = (A07 - A08) * 5.1011486053 A00 = B00 + B07 A01 = B01 + B06 A02 = B02 + B05 A03 = B03 + B04 A04 = (B00 - B07) * 0.5097956061 A05 = (B01 - B06) * 0.6013448834 A06 = (B02 - B05) * 0.8999761939 A07 = (B03 - B04) * 2.5629155636 A08 = B08 + B15 A09 = B09 + B14 A10 = B10 + B13 A11 = B11 + B12 A12 = (B08 - B15) * 0.5097956061 A13 = (B09 - B14) * 0.6013448834 A14 = (B10 - B13) * 0.8999761939 A15 = (B11 - B12) * 2.5629155636 B00 = A00 + A03 B01 = A01 + A02 B02 = (A00 - A03) * 0.5411961079 B03 = (A01 - A02) * 1.3065630198 B04 = A04 + A07 B05 = A05 + A06 B06 = (A04 - A07) * 0.5411961079 B07 = (A05 - A06) * 1.3065630198 B08 = A08 + A11 B09 = A09 + A10 B10 = (A08 - A11) * 0.5411961079 B11 = (A09 - A10) * 1.3065630198 B12 = A12 + A15 B13 = A13 + A14 B14 = (A12 - A15) * 0.5411961079 B15 = (A13 - A14) * 1.3065630198 A00 = (B00 + B01) A01 = (B00 - B01) * 0.7071067691 A02 = (B02 + B03) A03 = (B02 - B03) * 0.7071067691 A04 = (B04 + B05) A05 = (B04 - B05) * 0.7071067691 A06 = (B06 + B07) A07 = (B06 - B07) * 0.7071067691 A08 = (B08 + B09) A09 = (B08 - B09) * 0.7071067691 A10 = (B10 + B11) A11 = (B10 - B11) * 0.7071067691 A12 = (B12 + B13) A13 = (B12 - B13) * 0.7071067691 A14 = (B14 + B15) A15 = (B14 - B15) * 0.7071067691 V[o + 15] = A15 t = A07 + A15 V[o + 13] = t t += A11 V[o + 11] = t V[o + 5] = t + A05 + A13 t = A03 + A11 + A15 V[o + 9] = t V[o + 7] = t + A13 t = A01 + A09 + A13 + A15 V[o + 1] = t V[o + 33] = -t - A14 t = A05 + A07 + A09 + A13 + A15 V[o + 3] = t V[o + 35] = -t - A06 - A14 t = -(A10 + A11 + A13 + A14 + A15) V[o + 37] = t - A05 - A06 - A07 V[o + 39] = t - A02 - A03 t += A13 - A12 V[o + 41] = t - A02 - A03 V[o + 43] = t - A04 - A06 - A07 t = -(A08 + A12 + A14 + A15) V[o + 47] = t - A00 V[o + 45] = t - A04 - A06 - A07 for i = 0, 15 do V[o + 32 - i] = -V[o + i] end for i = 33, 47 do V[o + 96 - i] = V[o + i] end end local function newV(Y: { number }, y: number, V: { number }, o: number) newVEven(Y, y, V, o) newVOdd(Y, y, V, o) end export type State = { version: number, maxBand: number, ms: boolean, channels: number, resL: { number }, resR: { number }, scfL: { number }, scfR: { number }, scfiL: { number }, scfiR: { number }, dscfL: { boolean }, dscfR: { boolean }, msf: { boolean }, qL: { number }, qR: { number }, yL: { number }, yR: { number }, vL: { number }, vR: { number }, r1: number, r2: number, lastMaxBand: number, } function Decode.state(version: number, maxBand: number, ms: boolean, channels: number): State local st: State = { version = version, maxBand = maxBand, ms = ms, channels = channels, resL = table.create(33, 0), resR = table.create(33, 0), scfL = table.create(99, 0), scfR = table.create(99, 0), scfiL = table.create(33, 0), scfiR = table.create(33, 0), dscfL = table.create(33, false), dscfR = table.create(33, false), msf = table.create(33, false), qL = table.create(32 * 36, 0), qR = table.create(32 * 36, 0), yL = table.create(36 * 32, 0), yR = table.create(36 * 32, 0), vL = table.create(2304 + 960, 0), vR = table.create(2304 + 960, 0), r1 = 1, r2 = 1, lastMaxBand = 0, } return st end -- libmpcdec assumes scf 0 at the stream start, 0x01010101 mid-SV7; the noise generator restarts, as in a fresh stream function Decode.reset(st: State, scf: number) for i = 1, 99 do st.scfL[i] = scf st.scfR[i] = scf end for i = 1, 33 do st.resL[i], st.resR[i] = 0, 0 st.dscfL[i], st.dscfR[i] = false, false st.msf[i] = false end for i = 1, 2304 + 960 do st.vL[i], st.vR[i] = 0, 0 end st.lastMaxBand = 0 st.r1, st.r2 = 1, 1 end local function noise(st: State, q: { number }, o: number) local r1, r2 = st.r1, st.r2 for k = 0, 35 do local t1 = PARITY[band(r1, 0xF5) + 1] local t2 = PARITY[band(rshift(r2, 25), 0x63) + 1] r1 = bor(rshift(r1, 1), lshift(t1, 31)) r2 = bor(band(lshift(r2, 1), 0xFFFFFFFF), t2) local t = bxor(r1, r2) q[o + k] = band(rshift(t, 24), 255) + band(rshift(t, 16), 255) + band(rshift(t, 8), 255) + band(t, 255) - 510 end st.r1, st.r2 = r1, r2 end -- mpc_decoder_read_bitstream_sv7 local function readSv7(st: State, limit: number): (boolean?, string?) local resL, resR, msf = st.resL, st.resR, st.msf local maxUsed = 0 local ms = st.ms resL[1] = bits(4) resR[1] = bits(4) if resL[1] ~= 0 or resR[1] ~= 0 then if ms then msf[1] = bits(1) == 1 end maxUsed = 1 end for n = 1, st.maxBand do local idx = huff(HDR) resL[n + 1] = if idx ~= 4 then resL[n] + idx else bits(4) idx = huff(HDR) resR[n + 1] = if idx ~= 4 then resR[n] + idx else bits(4) local l, r = resL[n + 1], resR[n + 1] if l < -1 or l > 17 or r < -1 or r > 17 then return nil, "bad band resolution" end if l ~= 0 or r ~= 0 then if ms then msf[n + 1] = bits(1) == 1 end maxUsed = n + 1 end end if resL[1] > 17 or resR[1] > 17 then return nil, "bad band resolution" end -- SCFI local scfiL, scfiR = st.scfiL, st.scfiR for n = 1, maxUsed do if resL[n] ~= 0 then scfiL[n] = huff(SCFI7) end if resR[n] ~= 0 then scfiR[n] = huff(SCFI7) end end if P > limit then return nil, "truncated frame" end -- SCF / DSCF for n = 1, maxUsed do for c = 1, 2 do local res = if c == 1 then resL[n] else resR[n] if res ~= 0 then local scf = if c == 1 then st.scfL else st.scfR local scfi = if c == 1 then scfiL[n] else scfiR[n] local o = (n - 1) * 3 local idx = huff(DSCF7) local s0 = if idx ~= 8 then scf[o + 3] + idx else bits(6) local s1, s2 if scfi == 1 then idx = huff(DSCF7) s1 = if idx ~= 8 then s0 + idx else bits(6) s2 = s1 elseif scfi == 3 then s1, s2 = s0, s0 elseif scfi == 2 then s1 = s0 idx = huff(DSCF7) s2 = if idx ~= 8 then s1 + idx else bits(6) else idx = huff(DSCF7) s1 = if idx ~= 8 then s0 + idx else bits(6) idx = huff(DSCF7) s2 = if idx ~= 8 then s1 + idx else bits(6) end scf[o + 1] = if s0 > 1024 then 0x8080 else s0 scf[o + 2] = if s1 > 1024 then 0x8080 else s1 scf[o + 3] = if s2 > 1024 then 0x8080 else s2 end end end if P > limit then return nil, "truncated frame" end -- samples for n = 1, maxUsed do for c = 1, 2 do local res = if c == 1 then resL[n] else resR[n] local q = if c == 1 then st.qL else st.qR local o = (n - 1) * 36 + 1 if res == -1 then noise(st, q, o) elseif res == 1 then local tab = Q7[1][bits(1) + 1] for k = 0, 35, 3 do local idx = huff(tab) q[o + k] = idx % 3 - 1 q[o + k + 1] = (idx // 3) % 3 - 1 q[o + k + 2] = idx // 9 - 1 end elseif res == 2 then local tab = Q7[2][bits(1) + 1] for k = 0, 35, 2 do local idx = huff(tab) q[o + k] = idx % 5 - 2 q[o + k + 1] = idx // 5 - 2 end elseif res >= 3 and res <= 7 then local tab = Q7[res][bits(1) + 1] for k = 0, 35 do q[o + k] = huff(tab) end elseif res >= 8 then local nb, dc = RES_BIT[res], DC[res + 2] for k = 0, 35 do q[o + k] = bits(nb) - dc end end if P > limit then return nil, "truncated frame" end end end -- bands above maxUsed: silent for n = maxUsed + 1, st.maxBand + 1 do resL[n], resR[n] = 0, 0 end return true, nil end local function readSv8(st: State, key: boolean, limit: number): (boolean?, string?) local resL, resR, msf = st.resL, st.resR, st.msf local maxBand = st.maxBand local maxUsed if key then maxUsed = logDec(maxBand + 1) else maxUsed = st.lastMaxBand + huff(BANDS8) if maxUsed > 32 then maxUsed -= 33 end end if maxUsed > maxBand + 1 then return nil, "bad band count" end st.lastMaxBand = maxUsed if maxUsed > 0 then local l, r = huff(RES8[1]), huff(RES8[1]) resL[maxUsed] = if l > 15 then l - 17 else l resR[maxUsed] = if r > 15 then r - 17 else r for n = maxUsed - 1, 1, -1 do l = huff(RES8[if resL[n + 1] > 2 then 2 else 1]) + resL[n + 1] resL[n] = if l > 15 then l - 17 else l r = huff(RES8[if resR[n + 1] > 2 then 2 else 1]) + resR[n + 1] resR[n] = if r > 15 then r - 17 else r end if st.ms then local tot = 0 for n = 1, maxUsed do if resL[n] ~= 0 or resR[n] ~= 0 then tot += 1 end end local cnt = logDec(tot) local tmp = 0 if cnt ~= 0 and cnt ~= tot then tmp = enumDec(math.min(cnt, tot - cnt), tot) end if cnt * 2 > tot then tmp = bnot(tmp) end for n = maxUsed, 1, -1 do if resL[n] ~= 0 or resR[n] ~= 0 then msf[n] = band(tmp, 1) == 1 tmp = rshift(tmp, 1) end end end end for n = maxUsed + 1, maxBand + 1 do resL[n], resR[n] = 0, 0 end -- SCFI local dscfL, dscfR = st.dscfL, st.dscfR if key then for n = 1, 32 do dscfL[n], dscfR[n] = true, true end end local scfiL, scfiR = st.scfiL, st.scfiR for n = 1, maxUsed do local cnt = -1 if resL[n] ~= 0 then cnt += 1 end if resR[n] ~= 0 then cnt += 1 end if cnt >= 0 then local tmp = huff(SCFI8[cnt + 1]) if resL[n] ~= 0 then scfiL[n] = rshift(tmp, 2 * cnt) end if resR[n] ~= 0 then scfiR[n] = band(tmp, 3) end end end if P > limit then return nil, "truncated frame" end -- SCF / DSCF for n = 1, maxUsed do for c = 1, 2 do local res = if c == 1 then resL[n] else resR[n] if res ~= 0 then local scf = if c == 1 then st.scfL else st.scfR local scfi = if c == 1 then scfiL[n] else scfiR[n] local dscf = if c == 1 then dscfL else dscfR local o = (n - 1) * 3 if dscf[n] then scf[o + 1] = bits(7) - 6 dscf[n] = false else local tmp = huff(DSCF8[2]) if tmp == 64 then tmp += bits(6) end scf[o + 1] = band(scf[o + 3] - 25 + tmp, 127) - 6 end for m = 0, 1 do if band(lshift(scfi, m), 2) == 0 then local tmp = huff(DSCF8[1]) if tmp == 31 then tmp = 64 + bits(6) end scf[o + m + 2] = band(scf[o + m + 1] - 25 + tmp, 127) - 6 else scf[o + m + 2] = scf[o + m + 1] end end end end end if P > limit then return nil, "truncated frame" end -- samples for n = 1, maxUsed do for c = 1, 2 do local res = if c == 1 then resL[n] else resR[n] local q = if c == 1 then st.qL else st.qR local o = (n - 1) * 36 + 1 if res == 2 then local tabs = Q8[2] local thres = 3 local idx = 6 for k = 0, 35, 3 do local tmp = huff(tabs[if idx > thres then 2 else 1]) local a, b, cc = tmp % 5 - 2, (tmp // 5) % 5 - 2, tmp // 25 - 2 q[o + k], q[o + k + 1], q[o + k + 2] = a, b, cc idx = rshift(idx, 1) + abs(a) + abs(b) + abs(cc) end elseif res == 1 then local k = 0 while k < 36 do local kmax = k + 18 local cnt = huff(Q1_8) local idx = 0 if cnt > 0 and cnt < 18 then idx = enumDec(if cnt <= 9 then cnt else 18 - cnt, 18) end if cnt > 9 then idx = bnot(idx) end while k < kmax do q[o + k] = 0 if band(idx, 0x20000) ~= 0 then q[o + k] = bits(1) * 2 - 1 end idx = lshift(idx, 1) k += 1 end end elseif res == -1 then noise(st, q, o) elseif res == 3 or res == 4 then local tab = Q34[res - 2] for k = 0, 35, 2 do local s = band(huff(tab), 255) local s1, s2 = band(s, 15), rshift(s, 4) q[o + k] = if s1 >= 8 then s1 - 16 else s1 q[o + k + 1] = if s2 >= 8 then s2 - 16 else s2 end elseif res >= 5 and res <= 8 then local tabs = Q8[res] local thres = THRES[res] local idx = 2 * thres for k = 0, 35 do local v = huff(tabs[if idx > thres then 2 else 1]) q[o + k] = v idx = rshift(idx, 1) + abs(v) end elseif res >= 9 then local sh = res - 9 local dc = DC[res + 2] for k = 0, 35 do local v = band(huff(Q9UP), 255) if sh > 0 then v = v * 2 ^ sh + bits(sh) end q[o + k] = v - dc end elseif res < -1 then return nil, "bad band resolution" end if P > limit then return nil, "truncated frame" end end end return true, nil end local function requant(st: State) local resL, resR, msf = st.resL, st.resR, st.msf local scfL, scfR, qL, qR, yL, yR = st.scfL, st.scfR, st.qL, st.qR, st.yL, st.yR for b = 1, st.maxBand + 1 do local rl, rr = resL[b], resR[b] local ccl, ccr = CC[rl + 2] or 0, CC[rr + 2] or 0 local ms = msf[b] local o = (b - 1) * 36 + 1 for part = 0, 2 do local fl = if rl ~= 0 then f32(ccl * SCF[band(scfL[(b - 1) * 3 + part + 1], 255) + 1]) else 0 local fr = if rr ~= 0 then f32(ccr * SCF[band(scfR[(b - 1) * 3 + part + 1], 255) + 1]) else 0 local y = part * 12 * 32 + b local qo = o + part * 12 if ms then for k = 0, 11 do local l, r = fl * qL[qo + k], fr * qR[qo + k] yL[y], yR[y] = l + r, l - r y += 32 end else for k = 0, 11 do yL[y], yR[y] = fl * qL[qo + k], fr * qR[qo + k] y += 32 end end end end end -- mpc_synthese_filter_float local function synth(V: { number }, Y: { number }, out: { number }, base: number, step: number) table.move(V, 1, 960, 2305) local o = 2304 + 1 local dw = DW local p = base for n = 0, 35 do o -= 64 newV(Y, n * 32 + 1, V, o) for k = 0, 31 do local v = o + k local d = k * 16 + 1 out[p] = V[v] * dw[d] + V[v + 96] * dw[d + 1] + V[v + 128] * dw[d + 2] + V[v + 224] * dw[d + 3] + V[v + 256] * dw[d + 4] + V[v + 352] * dw[d + 5] + V[v + 384] * dw[d + 6] + V[v + 480] * dw[d + 7] + V[v + 512] * dw[d + 8] + V[v + 608] * dw[d + 9] + V[v + 640] * dw[d + 10] + V[v + 736] * dw[d + 11] + V[v + 768] * dw[d + 12] + V[v + 864] * dw[d + 13] + V[v + 896] * dw[d + 14] + V[v + 992] * dw[d + 15] p += step end end end function Decode.frame(st: State, buf: buffer, bitPos: number, limitBits: number, key: boolean, out: { number }, base: number): (number?, string?) if bitPos >= 0 then if rshift(limitBits, 3) + Decode.PAD > buffer.len(buf) or bitPos > limitBits then return nil, "truncated frame" end B, P = buf, bitPos local ok, err if st.version == 8 then ok, err = readSv8(st, key, limitBits) else ok, err = readSv7(st, limitBits) end B = buffer.create(0) if not ok then return nil, err end else for b = 1, st.maxBand + 1 do st.resL[b], st.resR[b] = 0, 0 end end local pos = P requant(st) local ch = st.channels synth(st.vL, st.yL, out, base, ch) if ch > 1 then synth(st.vR, st.yR, out, base + 1, ch) end return if bitPos >= 0 then pos else bitPos, nil end function Decode.bitsAt(buf: buffer, bitPos: number, n: number): number local w = bswap(readu32(buf, rshift(bitPos, 3))) return extract(w, 32 - band(bitPos, 7) - n, n) end return Decode