From 738b8813494cc84e0bb6c57ec46f41e51314e0e3 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sun, 1 Nov 2026 19:44:18 -0600 Subject: [PATCH] audio perf: Ogg page CRC slice-by-4, Opus comb filter index offsets --- src/Rosetta/codecs/audio/Opus/Celt.luau | 10 ++++++---- src/Rosetta/containers/Ogg/Page.luau | 25 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Rosetta/codecs/audio/Opus/Celt.luau b/src/Rosetta/codecs/audio/Opus/Celt.luau index ff10607..98adddf 100644 --- a/src/Rosetta/codecs/audio/Opus/Celt.luau +++ b/src/Rosetta/codecs/audio/Opus/Celt.luau @@ -1433,14 +1433,16 @@ local function combFilter(mem: { number }, base: number, T0: number, T1: number, overlap = 0 end local i = 0 + local d1 = 2 - T1 while i < overlap do local p = base + i - local x0 = mem[p - T1 + 2] + local x0 = mem[p + d1] local w = WINDOW[i + 1] local f = w * w local of = 1 - f - mem[p] = mem[p] + (of * g00) * mem[p - T0] + (of * g01) * (mem[p - T0 + 1] + mem[p - T0 - 1]) - + (of * g02) * (mem[p - T0 + 2] + mem[p - T0 - 2]) + (f * g10) * x2 + (f * g11) * (x1 + x3) + local t = p - T0 + mem[p] = mem[p] + (of * g00) * mem[t] + (of * g01) * (mem[t + 1] + mem[t - 1]) + + (of * g02) * (mem[t + 2] + mem[t - 2]) + (f * g10) * x2 + (f * g11) * (x1 + x3) + (f * g12) * (x0 + x4) x4 = x3 x3 = x2 @@ -1458,7 +1460,7 @@ local function combFilter(mem: { number }, base: number, T0: number, T1: number, x2 = mem[p - T1] x1 = mem[p - T1 + 1] for q = p, base + N - 1 do - local x0 = mem[q - T1 + 2] + local x0 = mem[q + d1] mem[q] = mem[q] + g10 * x2 + g11 * (x1 + x3) + g12 * (x0 + x4) x4 = x3 x3 = x2 diff --git a/src/Rosetta/containers/Ogg/Page.luau b/src/Rosetta/containers/Ogg/Page.luau index b35bc11..ebc99eb 100644 --- a/src/Rosetta/containers/Ogg/Page.luau +++ b/src/Rosetta/containers/Ogg/Page.luau @@ -9,7 +9,7 @@ -- 18 sequence (u32) | 22 crc (u32) | 26 segment count | 27 lacing values | data local Crc32 = require("@rosetta/core/Crc32") local readu8, readu32, readi32 = buffer.readu8, buffer.readu32, buffer.readi32 -local band, bxor, lshift, rshift = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift +local band, bxor, lshift, rshift, byteswap = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift, bit32.byteswap local Page = {} @@ -31,9 +31,28 @@ export type Info = { local CRC = Crc32.MSB_TABLE Page.CRC_TABLE = CRC +-- slice-by-4: T2 .. T4[i] = the CRC of byte i followed by 1 .. 3 zero bytes; the CRC is linear, so four bytes w +-- (big-endian) update crc to the xor of the bytes of crc ~ w through T4, T3, T2 and CRC +local T2, T3, T4 = table.create(256, 0), table.create(256, 0), table.create(256, 0) +for i = 0, 255 do + local c = CRC[i + 1] + c = bxor(lshift(c, 8), CRC[rshift(c, 24) + 1]) + T2[i + 1] = c + c = bxor(lshift(c, 8), CRC[rshift(c, 24) + 1]) + T3[i + 1] = c + c = bxor(lshift(c, 8), CRC[rshift(c, 24) + 1]) + T4[i + 1] = c +end + local function crcRange(b: buffer, from: number, to: number, crc: number): number - for i = from, to - 1 do - crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, i)) + 1]) + local i = from + while i + 4 <= to do + local x = bxor(crc, byteswap(readu32(b, i))) + crc = bxor(T4[rshift(x, 24) + 1], T3[band(rshift(x, 16), 255) + 1], T2[band(rshift(x, 8), 255) + 1], CRC[band(x, 255) + 1]) + i += 4 + end + for j = i, to - 1 do + crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, j)) + 1]) end return crc end