absorb: merge ws/absorb-opus

This commit is contained in:
4DBug committed 2026-10-31 08:35:16 -05:00
commit 3fb01e655f
17 files changed
+6611 -2

No files matched your search

+12
View File
@@ -38,6 +38,18 @@ local list: { Entry } = {
return require("@formats/audio/mp3")
end,
},
{
name = "opus",
category = "audio",
path = "audio/opus",
extensions = { "opus" },
mime = { "audio/opus", "audio/ogg" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg" },
caps = { decode = true, encode = false, streaming = true, seek = true, lossy = true, channels = 255, rates = { 48000 } },
load = function()
return require("@formats/audio/opus")
end,
},
{
name = "wav",
category = "audio",
File diff suppressed because it is too large. Load diff
+58
View File
@@ -0,0 +1,58 @@
--!strict
-- audio/opus/CeltTables: Generated from libopus celt/static_modes_float.h and celt/quant_bands.c (BSD, see licenses/). 1-based arrays.
return {
logN400 = {
0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36,
},
cache_index50 = {
-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41, 82, 82, 123, 164, 200, 222, 0, 0, 0,
0, 0, 0, 0, 0, 41, 41, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295, 41, 41, 41, 41, 41, 41,
41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305, 318, 328, 336, 123, 123, 123, 123, 123, 123, 123, 123, 240,
240, 240, 240, 305, 305, 305, 318, 318, 343, 351, 358, 364, 240, 240, 240, 240, 240, 240, 240, 240, 305, 305, 305, 305,
343, 343, 343, 351, 351, 370, 376, 382, 387,
},
cache_bits50 = {
40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, 31, 34, 36,
38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50, 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62,
63, 63, 65, 66, 67, 68, 69, 70, 71, 71, 40, 20, 33, 41, 48, 53, 57, 61, 64, 66, 69, 71, 73, 75,
76, 78, 80, 82, 85, 87, 89, 91, 92, 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123,
124, 126, 128, 40, 23, 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, 97, 100, 102, 105, 107, 111, 115, 118, 121,
124, 126, 129, 131, 135, 139, 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35, 28, 49, 65,
78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, 153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211,
216, 220, 225, 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137, 148, 157, 166, 174, 182, 189, 195, 201,
207, 217, 227, 235, 243, 251, 17, 35, 63, 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250,
25, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, 185, 190, 200, 208, 215, 222, 229, 235, 240,
245, 255, 16, 36, 65, 89, 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41, 74, 103, 128,
151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, 163, 186, 207, 227, 246, 12, 39, 71, 99, 123, 144, 164,
182, 198, 214, 228, 241, 253, 9, 44, 81, 113, 142, 168, 192, 214, 235, 255, 7, 49, 90, 127, 160, 191, 220, 247,
6, 51, 95, 134, 170, 203, 234, 7, 47, 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57,
106, 151, 192, 231, 5, 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, 224, 5, 60, 113, 161, 206, 248, 4, 65,
122, 175, 224, 4, 67, 127, 182, 234,
},
cache_caps50 = {
224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37, 224, 224, 224,
224, 224, 224, 224, 224, 240, 240, 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40, 160, 160, 160, 160, 160, 160,
160, 160, 185, 185, 185, 185, 193, 193, 193, 183, 183, 172, 138, 64, 38, 240, 240, 240, 240, 240, 240, 240, 240, 207,
207, 207, 207, 204, 204, 204, 193, 193, 180, 143, 66, 40, 185, 185, 185, 185, 185, 185, 185, 185, 193, 193, 193, 193,
193, 193, 193, 183, 183, 172, 138, 65, 39, 207, 207, 207, 207, 207, 207, 207, 207, 204, 204, 204, 204, 201, 201, 201,
188, 188, 176, 141, 66, 40, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 194, 194, 194, 184, 184, 173,
139, 65, 39, 204, 204, 204, 204, 204, 204, 204, 204, 201, 201, 201, 201, 198, 198, 198, 187, 187, 175, 140, 66, 40,
},
e_prob_model = {
72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, 64, 128, 92, 78, 92, 79, 92, 78, 90, 79,
116, 41, 115, 40, 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11, 24, 179, 48, 138, 54, 135,
54, 132, 53, 134, 56, 133, 55, 132, 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, 91, 67,
100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50, 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73,
93, 74, 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, 146, 19, 162, 12, 165, 10, 178, 7,
189, 6, 190, 8, 177, 9, 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, 73, 91, 78, 89,
86, 80, 92, 66, 93, 64, 102, 59, 103, 60, 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45,
61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, 112, 38, 124, 26, 132, 27, 136, 19, 140, 20,
155, 14, 159, 16, 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10, 21, 178, 59, 110, 71, 86,
75, 85, 84, 83, 91, 66, 88, 73, 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, 112, 56,
129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42, 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32,
120, 36, 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, 154, 26, 166, 21, 173, 16, 184, 13,
184, 10, 150, 13, 139, 15, 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, 96, 67, 101, 73,
107, 72, 113, 55, 118, 52, 125, 52, 118, 52, 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40,
},
}
+466
View File
@@ -0,0 +1,466 @@
--!strict
--!native
--!optimize 2
-- audio/opus/Decoder: The Opus decoder proper (libopus src/opus_decoder.c and opus_multistream_decoder.c): per-frame mode switching
-- between SILK, hybrid and CELT, the redundant CELT frames of mode transitions, cross-fades, PLC fallbacks, the
-- multi-frame packet loop and multistream (channel mapping) decoding. Output is 48 kHz float in Luau arrays.
local RD = require("@formats/audio/opus/RangeDecoder")
local Packet = require("@formats/audio/opus/Packet")
local Celt = require("@formats/audio/opus/Celt")
local Silk = require("@formats/audio/opus/Silk")
local Decoder = {}
local MODE_SILK, MODE_HYBRID, MODE_CELT = Packet.MODE_SILK_ONLY, Packet.MODE_HYBRID, Packet.MODE_CELT_ONLY
local BW_NB, BW_MB, BW_WB, BW_SWB = Packet.BW_NB, Packet.BW_MB, Packet.BW_WB, Packet.BW_SWB
local F20, F10, F5, F2_5 = 960, 480, 240, 120
local min, max = math.min, math.max
local readu8 = buffer.readu8
local WINDOW = Celt.window
local TRANS = table.create(F5 * 2, 0)
local REDUND = table.create(F5 * 2, 0)
local SILKTMP = table.create(F10 * 2, 0)
local SILENCE = buffer.create(2)
buffer.writeu8(SILENCE, 0, 0xFF)
buffer.writeu8(SILENCE, 1, 0xFF)
local EMPTY_EC = RD.new(buffer.create(0), 0, 0)
local OFFS = table.create(48, 0)
local SIZES = table.create(48, 0)
export type State = {
channels: number,
celt: any,
silk: any,
ctrl: any,
stream_channels: number,
frame_size: number,
mode: number,
bandwidth: number,
prev_mode: number,
prev_redundancy: boolean,
last_packet_duration: number,
rangeFinal: number,
}
function Decoder.new(channels: number): State
return {
channels = channels,
celt = Celt.new(channels),
silk = Silk.new(channels),
ctrl = {
nChannelsAPI = channels,
nChannelsInternal = channels,
API_sampleRate = 48000,
internalSampleRate = 16000,
payloadSize_ms = 20,
},
stream_channels = channels,
frame_size = 120,
mode = 0,
bandwidth = 0,
prev_mode = 0,
prev_redundancy = false,
last_packet_duration = 0,
rangeFinal = 0,
}
end
function Decoder.reset(st: State)
Celt.reset(st.celt)
Silk.reset(st.silk)
st.stream_channels = st.channels
st.frame_size = 120
st.mode = 0
st.bandwidth = 0
st.prev_mode = 0
st.prev_redundancy = false
st.last_packet_duration = 0
st.rangeFinal = 0
end
-- out = w^2 * in2 + (1 - w^2) * in1 over `overlap` samples per channel
local function smoothFade(in1: { number }, b1: number, in2: { number }, b2: number, out: { number }, bo: number, overlap: number, ch: number)
for c = 0, ch - 1 do
for i = 0, overlap - 1 do
local w = WINDOW[i + 1]
w *= w
local k = i * ch + c
out[bo + k] = w * in2[b2 + k] + (1 - w) * in1[b1 + k]
end
end
end
local function decodeFrame(st: State, buf: buffer?, off: number, len: number, pcm: { number }, pbase: number, frameSize: number): number
local ch = st.channels
local celt = st.celt
if frameSize < F2_5 then
return -2
end
frameSize = min(frameSize, 2880)
local data = buf
if len <= 1 then
data = nil
frameSize = min(frameSize, st.frame_size)
end
local audiosize, mode, bandwidth
local dec = EMPTY_EC
if data then
audiosize = st.frame_size
mode = st.mode
bandwidth = st.bandwidth
dec = RD.new(data, off, len)
else
audiosize = frameSize
mode = if st.prev_redundancy then MODE_CELT else st.prev_mode
bandwidth = 0
if mode == 0 then
for i = 0, audiosize * ch - 1 do
pcm[pbase + i] = 0
end
return audiosize
end
if audiosize > F20 then
repeat
local ret = decodeFrame(st, nil, 0, 0, pcm, pbase, min(audiosize, F20))
if ret < 0 then
return ret
end
pbase += ret * ch
audiosize -= ret
until audiosize <= 0
return frameSize
elseif audiosize < F20 then
if audiosize > F10 then
audiosize = F10
elseif mode ~= MODE_SILK and audiosize > F5 and audiosize < F10 then
audiosize = F5
end
end
end
local celtAccum = mode ~= MODE_CELT
local transition = false
if data and st.prev_mode > 0
and ((mode == MODE_CELT and st.prev_mode ~= MODE_CELT and not st.prev_redundancy)
or (mode ~= MODE_CELT and st.prev_mode == MODE_CELT)) then
transition = true
end
if transition and mode == MODE_CELT then
decodeFrame(st, nil, 0, 0, TRANS, 1, min(F5, audiosize))
end
if audiosize > frameSize then
return -1
end
frameSize = audiosize
-- SILK
if mode ~= MODE_CELT then
local tooSmall = frameSize < F10
local sarr, sbase = pcm, pbase
if tooSmall then
sarr, sbase = SILKTMP, 1
end
if st.prev_mode == MODE_CELT then
Silk.reset(st.silk)
end
local ctrl = st.ctrl
ctrl.payloadSize_ms = max(10, 1000 * audiosize // 48000)
if data then
ctrl.nChannelsInternal = st.stream_channels
if mode == MODE_SILK then
ctrl.internalSampleRate = if bandwidth == BW_NB then 8000 elseif bandwidth == BW_MB then 12000 else 16000
else
ctrl.internalSampleRate = 16000
end
end
local lost = if data == nil then 1 else 0
local decoded = 0
repeat
local ret, n = Silk.decode(st.silk, ctrl, lost, decoded == 0, dec, sarr, sbase + decoded * ch)
if ret ~= 0 or n <= 0 then
if lost ~= 0 or n <= 0 then
n = frameSize - decoded
for i = 0, n * ch - 1 do
sarr[sbase + decoded * ch + i] = 0
end
else
return -3
end
end
decoded += n
until decoded >= frameSize
if tooSmall then
for i = 0, frameSize * ch - 1 do
pcm[pbase + i] = SILKTMP[i + 1]
end
end
end
local startBand = 0
local redundancy, celtToSilk = false, false
local redundancyBytes = 0
if mode ~= MODE_CELT and data and RD.tell(dec) + 17 + (if mode == MODE_HYBRID then 20 else 0) <= 8 * len then
if mode == MODE_HYBRID then
redundancy = RD.bitLogp(dec, 12)
else
redundancy = true
end
if redundancy then
celtToSilk = RD.bitLogp(dec, 1)
redundancyBytes = if mode == MODE_HYBRID then RD.uint(dec, 256) + 2 else len - bit32.rshift(RD.tell(dec) + 7, 3)
len -= redundancyBytes
if len * 8 < RD.tell(dec) then
len = 0
redundancyBytes = 0
redundancy = false
end
dec.storage -= redundancyBytes
end
end
if mode ~= MODE_CELT then
startBand = 17
end
if redundancy then
transition = false
end
if transition and mode ~= MODE_CELT then
decodeFrame(st, nil, 0, 0, TRANS, 1, min(F5, audiosize))
end
if bandwidth ~= 0 then
celt["end"] = if bandwidth == BW_NB then 13 elseif bandwidth == BW_MB or bandwidth == BW_WB then 17 elseif bandwidth == BW_SWB then 19 else 21
end
celt.stream_channels = st.stream_channels
local redundantRng = 0
if redundancy and celtToSilk then
celt.start = 0
Celt.decode(celt, data, off + len, redundancyBytes, REDUND, 1, F5, nil, false)
redundantRng = celt.rng
end
celt.start = startBand
local celtRet = 0
if mode ~= MODE_SILK then
local celtFrameSize = min(F20, frameSize)
if mode ~= st.prev_mode and st.prev_mode > 0 and not st.prev_redundancy then
Celt.reset(celt)
end
celtRet = Celt.decode(celt, data, off, len, pcm, pbase, celtFrameSize, if data then dec else nil, celtAccum)
st.rangeFinal = celt.rng
else
if st.prev_mode == MODE_HYBRID and not (redundancy and celtToSilk and st.prev_redundancy) then
celt.start = 0
Celt.decode(celt, SILENCE, 0, 2, pcm, pbase, F2_5, nil, celtAccum)
end
st.rangeFinal = if data then dec.rng else 0
end
if redundancy and not celtToSilk then
Celt.reset(celt)
celt.start = 0
Celt.decode(celt, data, off + len, redundancyBytes, REDUND, 1, F5, nil, false)
redundantRng = celt.rng
local b = pbase + ch * (frameSize - F2_5)
smoothFade(pcm, b, REDUND, 1 + ch * F2_5, pcm, b, F2_5, ch)
end
if redundancy and celtToSilk and (st.prev_mode ~= MODE_SILK or st.prev_redundancy) then
for i = 0, F2_5 * ch - 1 do
pcm[pbase + i] = REDUND[1 + i]
end
smoothFade(REDUND, 1 + ch * F2_5, pcm, pbase + ch * F2_5, pcm, pbase + ch * F2_5, F2_5, ch)
end
if transition then
if audiosize >= F5 then
for i = 0, ch * F2_5 - 1 do
pcm[pbase + i] = TRANS[1 + i]
end
smoothFade(TRANS, 1 + ch * F2_5, pcm, pbase + ch * F2_5, pcm, pbase + ch * F2_5, F2_5, ch)
else
smoothFade(TRANS, 1, pcm, pbase, pcm, pbase, F2_5, ch)
end
end
if len <= 1 then
st.rangeFinal = 0
else
st.rangeFinal = bit32.bxor(st.rangeFinal, redundantRng)
end
st.prev_mode = mode
st.prev_redundancy = redundancy and not celtToSilk
if celtRet < 0 then
return celtRet
end
return audiosize
end
-- opus_decode_native: decodes one packet (buf == nil or len == 0: conceal frameSize samples). pcm[pbase + i * ch + c].
-- Returns the samples per channel (or < 0 on error) and the packet's size (self-delimited packets).
function Decoder.decode(st: State, buf: buffer?, off: number, len: number, pcm: { number }, pbase: number, frameSize: number, selfDelimited: boolean?): (number, number)
local ch = st.channels
if buf == nil or len == 0 then
if frameSize % 120 ~= 0 then
return -1, 0
end
local count = 0
repeat
local ret = decodeFrame(st, nil, 0, 0, pcm, pbase + count * ch, frameSize - count)
if ret <= 0 then
return if ret < 0 then ret else -3, 0
end
count += ret
until count >= frameSize
st.last_packet_duration = count
return count, 0
end
local b = buf :: buffer
local toc = readu8(b, off)
local packetMode = Packet.mode(toc)
local packetBandwidth = Packet.bandwidth(toc)
local packetFrameSize = Packet.samplesPerFrame(toc, 48000)
local packetChannels = Packet.channels(toc)
local count, _, packetOffset = Packet.parse(b, off, len, OFFS, SIZES, selfDelimited)
if count < 0 then
return -4, 0
end
if count * packetFrameSize > frameSize then
return -2, 0
end
st.mode = packetMode
st.bandwidth = packetBandwidth
st.frame_size = packetFrameSize
st.stream_channels = packetChannels
local nb = 0
for i = 1, count do
local ret = decodeFrame(st, b, OFFS[i], SIZES[i], pcm, pbase + nb * ch, frameSize - nb)
if ret < 0 then
return ret, 0
end
nb += ret
end
st.last_packet_duration = nb
return nb, packetOffset
end
-- ---------------------------------------------------------------------------------------------------------------
-- multistream (channel mapping families 0 / 1 / 255)
export type MS = {
channels: number,
streams: number,
coupled: number,
mapping: { number },
decoders: { State },
tmp: { number },
}
function Decoder.newMultistream(channels: number, streams: number, coupled: number, mapping: { number }): MS
local decs = {}
for s = 0, streams - 1 do
decs[s + 1] = Decoder.new(if s < coupled then 2 else 1)
end
return {
channels = channels,
streams = streams,
coupled = coupled,
mapping = mapping,
decoders = decs,
tmp = table.create(5760 * 2, 0),
}
end
function Decoder.resetMultistream(ms: MS)
for _, d in ms.decoders do
Decoder.reset(d)
end
end
-- copies channel `sc` of src (stride sstride) into every output channel mapped to stream channel index `id`
local function copyOut(ms: MS, id: number, src: { number }, sc: number, sstride: number, pcm: { number }, pbase: number, n: number)
local nch = ms.channels
for c = 0, nch - 1 do
if ms.mapping[c + 1] == id then
local o = pbase + c
local s = 1 + sc
for _ = 1, n do
pcm[o] = src[s]
o += nch
s += sstride
end
end
end
end
-- opus_multistream_decode_native (float). Returns the samples per channel, or < 0 on error.
function Decoder.decodeMultistream(ms: MS, buf: buffer?, off: number, len: number, pcm: { number }, pbase: number, frameSize: number): number
local doPlc = buf == nil or len == 0
frameSize = min(frameSize, 5760)
if not doPlc and len < 2 * ms.streams - 1 then
return -4
end
local tmp = ms.tmp
for s = 0, ms.streams - 1 do
local d = ms.decoders[s + 1]
if not doPlc and len <= 0 then
return -3
end
local ret, packetOffset = Decoder.decode(d, if doPlc then nil else buf, off, if doPlc then 0 else len, tmp, 1, frameSize, s ~= ms.streams - 1)
if not doPlc then
off += packetOffset
len -= packetOffset
end
if ret <= 0 then
return if ret < 0 then ret else -3
end
frameSize = ret
if s < ms.coupled then
copyOut(ms, s * 2, tmp, 0, 2, pcm, pbase, frameSize)
copyOut(ms, s * 2 + 1, tmp, 1, 2, pcm, pbase, frameSize)
else
copyOut(ms, s + ms.coupled, tmp, 0, 1, pcm, pbase, frameSize)
end
end
local nch = ms.channels
for c = 0, nch - 1 do
if ms.mapping[c + 1] == 255 then
local o = pbase + c
for _ = 1, frameSize do
pcm[o] = 0
o += nch
end
end
end
return frameSize
end
-- writes src[base + 1 .. base + count] * gain to out as f32 from byte offset o
function Decoder.writeF32(src: { number }, base: number, count: number, gain: number, out: buffer, o: number)
local writef32 = buffer.writef32
if gain == 1 then
for i = base + 1, base + count do
writef32(out, o, src[i])
o += 4
end
else
for i = base + 1, base + count do
writef32(out, o, src[i] * gain)
o += 4
end
end
end
-- Stereo downmix of n frames of srcCh-channel audio (src[base + 1 ..], interleaved): matrix[c * 2 + 1 / + 2] are the
-- left / right weights of channel c. Writes f32 to out from byte offset o.
function Decoder.writeDownmix(src: { number }, base: number, n: number, srcCh: number, matrix: { number }, gain: number, out: buffer, o: number)
local writef32 = buffer.writef32
local p = base
for _ = 1, n do
local l, r = 0, 0
for c = 0, srcCh - 1 do
local v = src[p + c + 1]
l += v * matrix[c * 2 + 1]
r += v * matrix[c * 2 + 2]
end
writef32(out, o, l * gain)
writef32(out, o + 4, r * gain)
o += 8
p += srcCh
end
end
return Decoder
+342
View File
@@ -0,0 +1,342 @@
--!strict
--!native
--!optimize 2
-- audio/opus/Mdct: CELT inverse MDCT (libopus celt/mdct.c clt_mdct_backward) over a mixed-radix complex FFT (a port of the
-- kiss_fft stages used by libopus: radix 5, 3, 4 and 2). The 48 kHz mode uses N = 1920 >> shift for
-- shift = 0..3 (FFT sizes 480, 240, 120, 60). Arrays are Luau tables of numbers, 1-based.
local Mdct = {}
local sin, cos, pi = math.sin, math.cos, math.pi
type Plan = {
n: number, -- FFT size (N / 4)
factors: { number },
bitrev: { number },
twr: { number },
twi: { number },
trig: { number }, -- MDCT twiddles: (-sin, cos) pairs, N / 2 values
fr: { number },
fi: { number },
}
local FACTORS = {
[0] = { 5, 96, 3, 32, 4, 8, 2, 4, 4, 1 },
[1] = { 5, 48, 3, 16, 4, 4, 4, 1 },
[2] = { 5, 24, 3, 8, 2, 4, 4, 1 },
[3] = { 5, 12, 3, 4, 4, 1 },
}
local function bitrevTable(f: { number }, fout: number, fpos: number, fstride: number, factors: { number }, fi: number)
local p = factors[fi]
local m = factors[fi + 1]
if m == 1 then
for j = 0, p - 1 do
f[fpos + 1] = fout + j
fpos += fstride
end
else
for j = 0, p - 1 do
bitrevTable(f, fout, fpos, fstride * p, factors, fi + 2)
fpos += fstride
fout += m
end
end
end
local plans: { Plan } = {}
for shift = 0, 3 do
local N = 1920 // 2 ^ shift
local n = N // 4
local factors = FACTORS[shift]
local bitrev = table.create(n, 0)
bitrevTable(bitrev, 0, 0, 1, factors, 1)
local twr, twi = table.create(n, 0), table.create(n, 0)
for k = 0, n - 1 do
local ph = -2 * pi * k / n
twr[k + 1] = cos(ph)
twi[k + 1] = sin(ph)
end
local N2, N4 = N // 2, N // 4
local trig = table.create(N2, 0)
for i = 0, N4 - 1 do
trig[2 * i + 1] = -sin(2 * pi * (i + 0.125) / N)
trig[2 * i + 2] = cos(2 * pi * (i + 0.125) / N)
end
plans[shift + 1] = {
n = n,
factors = factors,
bitrev = bitrev,
twr = twr,
twi = twi,
trig = trig,
fr = table.create(n, 0),
fi = table.create(n, 0),
}
end
local function bfly2(fr: { number }, fi: { number }, N: number)
local tw = 0.7071067812
local p = 1
for _ = 1, N do
-- k = 0
local tr, ti = fr[p + 4], fi[p + 4]
fr[p + 4] = fr[p] - tr
fi[p + 4] = fi[p] - ti
fr[p] += tr
fi[p] += ti
-- k = 1
local a, b = fr[p + 5], fi[p + 5]
tr = (a + b) * tw
ti = (b - a) * tw
fr[p + 5] = fr[p + 1] - tr
fi[p + 5] = fi[p + 1] - ti
fr[p + 1] += tr
fi[p + 1] += ti
-- k = 2
tr = fi[p + 6]
ti = -fr[p + 6]
fr[p + 6] = fr[p + 2] - tr
fi[p + 6] = fi[p + 2] - ti
fr[p + 2] += tr
fi[p + 2] += ti
-- k = 3
a, b = fr[p + 7], fi[p + 7]
tr = (b - a) * tw
ti = -(b + a) * tw
fr[p + 7] = fr[p + 3] - tr
fi[p + 7] = fi[p + 3] - ti
fr[p + 3] += tr
fi[p + 3] += ti
p += 8
end
end
local function bfly4(fr: { number }, fi: { number }, fstride: number, twr: { number }, twi: { number }, m: number, N: number, mm: number)
if m == 1 then
local p = 1
for _ = 1, N do
local r0, i0 = fr[p], fi[p]
local r1, i1 = fr[p + 1], fi[p + 1]
local r2, i2 = fr[p + 2], fi[p + 2]
local r3, i3 = fr[p + 3], fi[p + 3]
local s0r, s0i = r0 - r2, i0 - i2
r0 += r2
i0 += i2
local s1r, s1i = r1 + r3, i1 + i3
fr[p + 2] = r0 - s1r
fi[p + 2] = i0 - s1i
fr[p] = r0 + s1r
fi[p] = i0 + s1i
s1r, s1i = r1 - r3, i1 - i3
fr[p + 1] = s0r + s1i
fi[p + 1] = s0i - s1r
fr[p + 3] = s0r - s1i
fi[p + 3] = s0i + s1r
p += 4
end
else
local m2, m3 = 2 * m, 3 * m
for i = 0, N - 1 do
local p = i * mm + 1
local t1, t2, t3 = 1, 1, 1
for _ = 0, m - 1 do
local ar, ai = fr[p + m], fi[p + m]
local wr, wi = twr[t1], twi[t1]
local s0r, s0i = ar * wr - ai * wi, ar * wi + ai * wr
ar, ai = fr[p + m2], fi[p + m2]
wr, wi = twr[t2], twi[t2]
local s1r, s1i = ar * wr - ai * wi, ar * wi + ai * wr
ar, ai = fr[p + m3], fi[p + m3]
wr, wi = twr[t3], twi[t3]
local s2r, s2i = ar * wr - ai * wi, ar * wi + ai * wr
local f0r, f0i = fr[p], fi[p]
local s5r, s5i = f0r - s1r, f0i - s1i
f0r += s1r
f0i += s1i
local s3r, s3i = s0r + s2r, s0i + s2i
local s4r, s4i = s0r - s2r, s0i - s2i
fr[p + m2] = f0r - s3r
fi[p + m2] = f0i - s3i
t1 += fstride
t2 += fstride * 2
t3 += fstride * 3
fr[p] = f0r + s3r
fi[p] = f0i + s3i
fr[p + m] = s5r + s4i
fi[p + m] = s5i - s4r
fr[p + m3] = s5r - s4i
fi[p + m3] = s5i + s4r
p += 1
end
end
end
end
local function bfly3(fr: { number }, fi: { number }, fstride: number, twr: { number }, twi: { number }, m: number, N: number, mm: number)
local m2 = 2 * m
local epi3i = twi[fstride * m + 1]
for i = 0, N - 1 do
local p = i * mm + 1
local t1, t2 = 1, 1
for _ = 1, m do
local ar, ai = fr[p + m], fi[p + m]
local wr, wi = twr[t1], twi[t1]
local s1r, s1i = ar * wr - ai * wi, ar * wi + ai * wr
ar, ai = fr[p + m2], fi[p + m2]
wr, wi = twr[t2], twi[t2]
local s2r, s2i = ar * wr - ai * wi, ar * wi + ai * wr
local s3r, s3i = s1r + s2r, s1i + s2i
local s0r, s0i = s1r - s2r, s1i - s2i
t1 += fstride
t2 += fstride * 2
local f0r, f0i = fr[p], fi[p]
local fmr, fmi = f0r - s3r * 0.5, f0i - s3i * 0.5
s0r *= epi3i
s0i *= epi3i
fr[p] = f0r + s3r
fi[p] = f0i + s3i
fr[p + m2] = fmr + s0i
fi[p + m2] = fmi - s0r
fr[p + m] = fmr - s0i
fi[p + m] = fmi + s0r
p += 1
end
end
end
local function bfly5(fr: { number }, fi: { number }, fstride: number, twr: { number }, twi: { number }, m: number, N: number, mm: number)
local yar, yai = twr[fstride * m + 1], twi[fstride * m + 1]
local ybr, ybi = twr[fstride * 2 * m + 1], twi[fstride * 2 * m + 1]
for i = 0, N - 1 do
local p0 = i * mm + 1
local p1, p2, p3, p4 = p0 + m, p0 + 2 * m, p0 + 3 * m, p0 + 4 * m
for u = 0, m - 1 do
local s0r, s0i = fr[p0], fi[p0]
local k = u * fstride
local ar, ai = fr[p1], fi[p1]
local wr, wi = twr[k + 1], twi[k + 1]
local s1r, s1i = ar * wr - ai * wi, ar * wi + ai * wr
ar, ai = fr[p2], fi[p2]
wr, wi = twr[2 * k + 1], twi[2 * k + 1]
local s2r, s2i = ar * wr - ai * wi, ar * wi + ai * wr
ar, ai = fr[p3], fi[p3]
wr, wi = twr[3 * k + 1], twi[3 * k + 1]
local s3r, s3i = ar * wr - ai * wi, ar * wi + ai * wr
ar, ai = fr[p4], fi[p4]
wr, wi = twr[4 * k + 1], twi[4 * k + 1]
local s4r, s4i = ar * wr - ai * wi, ar * wi + ai * wr
local s7r, s7i = s1r + s4r, s1i + s4i
local s10r, s10i = s1r - s4r, s1i - s4i
local s8r, s8i = s2r + s3r, s2i + s3i
local s9r, s9i = s2r - s3r, s2i - s3i
fr[p0] = s0r + (s7r + s8r)
fi[p0] = s0i + (s7i + s8i)
local s5r = s0r + (s7r * yar + s8r * ybr)
local s5i = s0i + (s7i * yar + s8i * ybr)
local s6r = s10i * yai + s9i * ybi
local s6i = -(s10r * yai + s9r * ybi)
fr[p1] = s5r - s6r
fi[p1] = s5i - s6i
fr[p4] = s5r + s6r
fi[p4] = s5i + s6i
local s11r = s0r + (s7r * ybr + s8r * yar)
local s11i = s0i + (s7i * ybr + s8i * yar)
local s12r = s9i * yai - s10i * ybi
local s12i = s10r * ybi - s9r * yai
fr[p2] = s11r + s12r
fi[p2] = s11i + s12i
fr[p3] = s11r - s12r
fi[p3] = s11i - s12i
p0 += 1
p1 += 1
p2 += 1
p3 += 1
p4 += 1
end
end
end
-- in-place forward FFT of bit-reversed input (opus_fft_impl)
local function fftImpl(pl: Plan)
local factors = pl.factors
local fr, fi, twr, twi = pl.fr, pl.fi, pl.twr, pl.twi
local fstride = { 1 }
local L = 0
local m
repeat
local p = factors[2 * L + 1]
m = factors[2 * L + 2]
fstride[L + 2] = fstride[L + 1] * p
L += 1
until m == 1
m = factors[2 * L]
for i = L - 1, 0, -1 do
local m2 = if i ~= 0 then factors[2 * i] else 1
local radix = factors[2 * i + 1]
if radix == 2 then
bfly2(fr, fi, fstride[i + 1])
elseif radix == 4 then
bfly4(fr, fi, fstride[i + 1], twr, twi, m, fstride[i + 1], m2)
elseif radix == 3 then
bfly3(fr, fi, fstride[i + 1], twr, twi, m, fstride[i + 1], m2)
else
bfly5(fr, fi, fstride[i + 1], twr, twi, m, fstride[i + 1], m2)
end
m = m2
end
end
-- clt_mdct_backward: input coefficients inp[inBase + k*stride] (k = 0 .. N/2-1, inBase is the 1-based index of
-- coefficient 0), output written to out[outBase .. outBase + N/2 + overlap/2) with TDAC windowing of the first
-- `overlap` samples (out[outBase] is sample 0).
function Mdct.backward(inp: { number }, inBase: number, out: { number }, outBase: number, window: { number }, overlap: number, shift: number, stride: number)
local pl = plans[shift + 1]
local N4 = pl.n
local N2 = N4 * 2
local trig = pl.trig
local bitrev = pl.bitrev
local fr, fi = pl.fr, pl.fi
-- pre-rotate, storing in bit-reversed order (real and imaginary swapped: an FFT is used instead of an IFFT)
local xp1 = inBase
local xp2 = inBase + stride * (N2 - 1)
for i = 0, N4 - 1 do
local x1 = inp[xp1]
local x2 = inp[xp2]
local t0, t1 = trig[2 * i + 1], trig[2 * i + 2]
local rev = bitrev[i + 1] + 1
fi[rev] = x2 * t1 + x1 * t0
fr[rev] = x1 * t1 - x2 * t0
xp1 += 2 * stride
xp2 -= 2 * stride
end
fftImpl(pl)
-- post-rotate
local h = outBase + (overlap // 2)
for k = 0, N4 - 1 do
local re = fi[k + 1]
local im = fr[k + 1]
local t0, t1 = trig[2 * k + 1], trig[2 * k + 2]
out[h + 2 * k] = re * t1 + im * t0
out[h + N2 - 1 - 2 * k] = re * t0 - im * t1
end
-- mirror on both sides for TDAC
local xp = outBase + overlap - 1
local yp = outBase
for i = 0, overlap // 2 - 1 do
local x1 = out[xp]
local x2 = out[yp]
local w1 = window[i + 1]
local w2 = window[overlap - i]
out[yp] = x2 * w2 - x1 * w1
out[xp] = x2 * w1 + x1 * w2
xp -= 1
yp += 1
end
end
return Mdct
+235
View File
@@ -0,0 +1,235 @@
--!strict
--!native
--!optimize 2
-- audio/opus/Packet: Opus packet framing (RFC 6716 section 3): the TOC byte and frame packing codes 0-3, with padding.
local Packet = {}
local readu8 = buffer.readu8
local band, rshift = bit32.band, bit32.rshift
Packet.MODE_SILK_ONLY = 1000
Packet.MODE_HYBRID = 1001
Packet.MODE_CELT_ONLY = 1002
-- bandwidths as in libopus (OPUS_BANDWIDTH_NARROWBAND .. FULLBAND)
Packet.BW_NB = 1101
Packet.BW_MB = 1102
Packet.BW_WB = 1103
Packet.BW_SWB = 1104
Packet.BW_FB = 1105
function Packet.mode(toc: number): number
if band(toc, 0x80) ~= 0 then
return Packet.MODE_CELT_ONLY
elseif band(toc, 0x60) == 0x60 then
return Packet.MODE_HYBRID
else
return Packet.MODE_SILK_ONLY
end
end
function Packet.bandwidth(toc: number): number
if band(toc, 0x80) ~= 0 then
local bw = 1102 + band(rshift(toc, 5), 3)
if bw == 1102 then
bw = 1101
end
return bw
elseif band(toc, 0x60) == 0x60 then
return if band(toc, 0x10) ~= 0 then 1105 else 1104
else
return 1101 + band(rshift(toc, 5), 3)
end
end
function Packet.channels(toc: number): number
return if band(toc, 4) ~= 0 then 2 else 1
end
-- samples per frame at rate Fs
function Packet.samplesPerFrame(toc: number, Fs: number): number
if band(toc, 0x80) ~= 0 then
local a = band(rshift(toc, 3), 3)
return bit32.lshift(Fs, a) // 400
elseif band(toc, 0x60) == 0x60 then
return if band(toc, 0x08) ~= 0 then Fs // 50 else Fs // 100
else
local a = band(rshift(toc, 3), 3)
if a == 3 then
return Fs * 60 // 1000
end
return bit32.lshift(Fs, a) // 100
end
end
-- total samples of a packet at 48 kHz, or -1 if invalid
function Packet.samples(buf: buffer, off: number, len: number): number
if len < 1 then
return -1
end
local toc = readu8(buf, off)
local c = band(toc, 3)
local count
if c == 0 then
count = 1
elseif c ~= 3 then
count = 2
else
if len < 2 then
return -1
end
count = band(readu8(buf, off + 1), 0x3F)
end
local n = count * Packet.samplesPerFrame(toc, 48000)
if n * 25 > 48000 * 3 then
return -1
end
return n
end
local function parseSize(buf: buffer, p: number, len: number): (number, number)
if len < 1 then
return -1, -1
end
local b0 = readu8(buf, p)
if b0 < 252 then
return 1, b0
elseif len < 2 then
return -1, -1
end
return 2, 4 * readu8(buf, p + 1) + b0
end
-- Splits a packet (opus_packet_parse_impl). Fills offsets[i] / sizes[i] (1-based, absolute buffer offsets) and
-- returns the frame count, the TOC byte and the packet's total size including padding (self-delimited packets, as in
-- multistream, carry an explicit size for the last frame). The count is -1 for an invalid packet.
function Packet.parse(buf: buffer, off: number, len: number, offsets: { number }, sizes: { number }, selfDelimited: boolean?): (number, number, number)
if len < 1 then
return -1, 0, 0
end
local toc = readu8(buf, off)
local framesize = Packet.samplesPerFrame(toc, 48000)
local p = off + 1
len -= 1
local lastSize = len
local count
local cbr = false
local pad = 0
local c = band(toc, 3)
if c == 0 then
count = 1
elseif c == 1 then
count = 2
cbr = true
if not selfDelimited then
if band(len, 1) ~= 0 then
return -1, toc, 0
end
lastSize = len // 2
sizes[1] = lastSize
end
elseif c == 2 then
count = 2
local bytes, s = parseSize(buf, p, len)
if bytes < 0 then
return -1, toc, 0
end
len -= bytes
if s < 0 or s > len then
return -1, toc, 0
end
sizes[1] = s
p += bytes
lastSize = len - s
else
if len < 1 then
return -1, toc, 0
end
local ch = readu8(buf, p)
p += 1
count = band(ch, 0x3F)
if count <= 0 or framesize * count > 5760 then
return -1, toc, 0
end
len -= 1
if band(ch, 0x40) ~= 0 then
local pb
repeat
if len <= 0 then
return -1, toc, 0
end
pb = readu8(buf, p)
p += 1
len -= 1
local tmp = if pb == 255 then 254 else pb
len -= tmp
pad += tmp
until pb ~= 255
end
if len < 0 then
return -1, toc, 0
end
cbr = band(ch, 0x80) == 0
if not cbr then
lastSize = len
for i = 1, count - 1 do
local bytes, s = parseSize(buf, p, len)
if bytes < 0 then
return -1, toc, 0
end
len -= bytes
if s < 0 or s > len then
return -1, toc, 0
end
sizes[i] = s
p += bytes
lastSize -= bytes + s
end
if lastSize < 0 then
return -1, toc, 0
end
elseif not selfDelimited then
lastSize = len // count
if lastSize * count ~= len then
return -1, toc, 0
end
for i = 1, count - 1 do
sizes[i] = lastSize
end
end
end
if selfDelimited then
local bytes, s = parseSize(buf, p, len)
if bytes < 0 then
return -1, toc, 0
end
len -= bytes
if s < 0 or s > len then
return -1, toc, 0
end
p += bytes
sizes[count] = s
if cbr then
if s * count > len then
return -1, toc, 0
end
for i = 1, count - 1 do
sizes[i] = s
end
elseif bytes + s > lastSize then
return -1, toc, 0
end
else
if lastSize > 1275 then
return -1, toc, 0
end
sizes[count] = lastSize
end
for i = 1, count do
offsets[i] = p
p += sizes[i]
end
return count, toc, pad + (p - off)
end
return Packet
+214
View File
@@ -0,0 +1,214 @@
--!strict
--!native
--!optimize 2
-- audio/opus/RangeDecoder: Range decoder (RFC 6716 section 4.1, libopus celt/entdec.c). Reads one Opus frame from a `buffer` slice
-- [base, base + storage). Raw bits (ec_dec_bits) are read from the end of the slice backwards.
-- All values stay below 2^32, so plain Luau numbers hold them exactly; floor division is exact below 2^52.
local RangeDecoder = {}
export type Dec = {
buf: buffer,
base: number,
storage: number,
offs: number,
end_offs: number,
end_window: number,
nend_bits: number,
nbits_total: number,
rng: number,
val: number,
rem: number,
ext: number,
error: number,
}
local SYM_BITS = 8
local CODE_BITS = 32
local CODE_TOP = 2147483648 -- 1 << 31
local CODE_BOT = 8388608 -- CODE_TOP >> 8
local CODE_EXTRA = 7 -- (CODE_BITS - 2) % SYM_BITS + 1
local SYM_MAX = 255
local UINT_BITS = 8
local WINDOW_SIZE = 32
local countlz = bit32.countlz
local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift
local readu8 = buffer.readu8
local function ilog(x: number): number
return 32 - countlz(x)
end
RangeDecoder.ilog = ilog
local function normalize(d: Dec)
local rng, val, rem = d.rng, d.val, d.rem
while rng <= CODE_BOT do
d.nbits_total += SYM_BITS
rng = rng * 256
local sym = rem
local offs = d.offs
if offs < d.storage then
rem = readu8(d.buf, d.base + offs)
d.offs = offs + 1
else
rem = 0
end
sym = rshift(bor(lshift(sym, 8), rem), SYM_BITS - CODE_EXTRA) -- (sym << 8 | rem) >> 1
val = band(val * 256 + band(SYM_MAX, bit32.bnot(sym)), CODE_TOP - 1)
end
d.rng, d.val, d.rem = rng, val, rem
end
function RangeDecoder.new(buf: buffer, base: number, storage: number): Dec
local d: Dec = {
buf = buf,
base = base,
storage = storage,
offs = 0,
end_offs = 0,
end_window = 0,
nend_bits = 0,
nbits_total = CODE_BITS + 1 - ((CODE_BITS - CODE_EXTRA) // SYM_BITS) * SYM_BITS,
rng = 128, -- 1 << CODE_EXTRA
val = 0,
rem = 0,
ext = 0,
error = 0,
}
local rem = 0
if storage > 0 then
rem = readu8(buf, base)
d.offs = 1
end
d.rem = rem
d.val = d.rng - 1 - rshift(rem, SYM_BITS - CODE_EXTRA)
normalize(d)
return d
end
-- ec_decode: the cumulative frequency of the next symbol, for a total of ft
function RangeDecoder.decode(d: Dec, ft: number): number
local ext = d.rng // ft
d.ext = ext
local s = d.val // ext
return ft - math.min(s + 1, ft)
end
function RangeDecoder.decodeBin(d: Dec, bits: number): number
local ext = rshift(d.rng, bits)
d.ext = ext
local s = d.val // ext
local ft = lshift(1, bits)
return ft - math.min(s + 1, ft)
end
function RangeDecoder.update(d: Dec, fl: number, fh: number, ft: number)
local s = d.ext * (ft - fh)
d.val = band(d.val - s, 0xFFFFFFFF) -- uint32 wrap (corrupt streams only)
if fl > 0 then
d.rng = d.ext * (fh - fl)
else
d.rng -= s
end
normalize(d)
end
function RangeDecoder.bitLogp(d: Dec, logp: number): boolean
local r = d.rng
local v = d.val
local s = rshift(r, logp)
local ret = v < s
if ret then
d.rng = s
else
d.val = v - s
d.rng = r - s
end
normalize(d)
return ret
end
-- ec_dec_icdf over a Luau array (1-based) starting at index `off` (icdf[off] is entry 0)
function RangeDecoder.icdf(d: Dec, icdf: { number }, off: number, ftb: number): number
local s = d.rng
local v = d.val
local r = rshift(s, ftb)
local ret = -1
local t
repeat
t = s
ret += 1
s = r * icdf[off + ret]
until v >= s
d.val = v - s
d.rng = t - s
normalize(d)
return ret
end
function RangeDecoder.bits(d: Dec, bits: number): number
local window = d.end_window
local available = d.nend_bits
if available < bits then
repeat
local b = 0
local eo = d.end_offs
if eo < d.storage then
eo += 1
d.end_offs = eo
b = readu8(d.buf, d.base + d.storage - eo)
end
window = bor(window, lshift(b, available))
available += SYM_BITS
until available > WINDOW_SIZE - SYM_BITS
end
local ret = band(window, lshift(1, bits) - 1)
if bits == 32 then
ret = window
end
window = rshift(window, bits)
available -= bits
d.end_window = window
d.nend_bits = available
d.nbits_total += bits
return ret
end
function RangeDecoder.uint(d: Dec, ft0: number): number
local ft = ft0 - 1
local ftb = ilog(ft)
if ftb > UINT_BITS then
ftb -= UINT_BITS
local ft1 = rshift(ft, ftb) + 1
local s = RangeDecoder.decode(d, ft1)
RangeDecoder.update(d, s, s + 1, ft1)
local t = bor(lshift(s, ftb), RangeDecoder.bits(d, ftb))
if t <= ft then
return t
end
d.error = 1
return ft
else
local s = RangeDecoder.decode(d, ft0)
RangeDecoder.update(d, s, s + 1, ft0)
return s
end
end
function RangeDecoder.tell(d: Dec): number
return d.nbits_total - ilog(d.rng)
end
local correction = { 35733, 38967, 42495, 46340, 50535, 55109, 60097, 65535 }
function RangeDecoder.tellFrac(d: Dec): number
local nbits = d.nbits_total * 8
local l = ilog(d.rng)
local r = rshift(d.rng, l - 16)
local b = rshift(r, 12) - 8
if r > correction[b + 1] then
b += 1
end
return nbits - (l * 8 + b)
end
return RangeDecoder
File diff suppressed because it is too large. Load diff
+372
View File
@@ -0,0 +1,372 @@
--!strict
--!optimize 2
-- audio/opus/SilkTables: SILK decoder tables (generated from libopus silk/tables_*.c, pitch_est_tables.c, resampler_rom.c; BSD, see
-- licenses/). Multi-dimensional C arrays are flattened row-major; every array is 1-based.
local T = {}
T.LTP_per_index_iCDF = {
179, 99, 0,
}
T.LTP_gain_iCDF_0 = {
71, 56, 43, 30, 21, 12, 6, 0,
}
T.LTP_gain_iCDF_1 = {
199, 165, 144, 124, 109, 96, 84, 71, 61, 51, 42, 32, 23, 15, 8, 0,
}
T.LTP_gain_iCDF_2 = {
241, 225, 211, 199, 187, 175, 164, 153, 142, 132, 123, 114, 105, 96, 88, 80, 72, 64, 57, 50,
44, 38, 33, 29, 24, 20, 16, 12, 9, 5, 2, 0,
}
T.LTP_gain_vq_0 = {
4, 6, 24, 7, 5, 0, 0, 2, 0, 0, 12, 28, 41, 13, -4, -9, 15, 42, 25, 14,
1, -2, 62, 41, -9, -10, 37, 65, -4, 3, -6, 4, 66, 7, -8, 16, 14, 38, -3, 33,
}
T.LTP_gain_vq_1 = {
13, 22, 39, 23, 12, -1, 36, 64, 27, -6, -7, 10, 55, 43, 17, 1, 1, 8, 1, 1,
6, -11, 74, 53, -9, -12, 55, 76, -12, 8, -3, 3, 93, 27, -4, 26, 39, 59, 3, -8,
2, 0, 77, 11, 9, -8, 22, 44, -6, 7, 40, 9, 26, 3, 9, -7, 20, 101, -7, 4,
3, -8, 42, 26, 0, -15, 33, 68, 2, 23, -2, 55, 46, -2, 15, 3, -1, 21, 16, 41,
}
T.LTP_gain_vq_2 = {
-6, 27, 61, 39, 5, -11, 42, 88, 4, 1, -2, 60, 65, 6, -4, -1, -5, 73, 56, 1,
-9, 19, 94, 29, -9, 0, 12, 99, 6, 4, 8, -19, 102, 46, -13, 3, 2, 13, 3, 2,
9, -21, 84, 72, -18, -11, 46, 104, -22, 8, 18, 38, 48, 23, 0, -16, 70, 83, -21, 11,
5, -11, 117, 22, -8, -6, 23, 117, -12, 3, 3, -8, 95, 28, 4, -10, 15, 77, 60, -15,
-1, 4, 124, 2, -4, 3, 38, 84, 24, -25, 2, 13, 42, 13, 31, 21, -4, 56, 46, -1,
-1, 35, 79, -13, 19, -7, 65, 88, -9, -14, 20, 4, 81, 49, -29, 20, 0, 75, 3, -17,
5, -9, 44, 92, -8, 1, -3, 22, 69, 31, -6, 95, 41, -12, 5, 39, 67, 16, -4, 1,
0, -6, 120, 55, -36, -13, 44, 122, 4, -24, 81, 5, 11, 3, 7, 2, 0, 9, 10, 88,
}
T.NLSF_CB1_WB_Q8 = {
7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239, 13, 25, 41, 55,
69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236, 15, 21, 34, 51, 61, 78, 92, 106,
126, 136, 152, 167, 185, 205, 225, 240, 10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173,
189, 205, 221, 237, 17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240,
10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236, 8, 21, 37, 51,
65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218, 12, 15, 34, 55, 63, 78, 87, 108,
118, 131, 148, 167, 185, 203, 219, 236, 16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171,
186, 204, 220, 237, 11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241,
6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225, 11, 19, 30, 44,
57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234, 12, 19, 29, 46, 57, 71, 88, 100,
120, 132, 148, 165, 182, 199, 216, 233, 17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167,
185, 204, 222, 237, 14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240,
9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237, 16, 19, 36, 48,
57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236, 12, 17, 29, 54, 71, 81, 94, 104,
126, 136, 149, 164, 182, 201, 221, 237, 15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180,
194, 208, 223, 238, 8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239,
17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235, 14, 19, 36, 45,
61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238, 12, 18, 31, 45, 60, 76, 91, 107,
123, 138, 154, 171, 187, 204, 221, 236, 13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167,
185, 203, 220, 237, 17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240,
8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239, 13, 16, 41, 66,
73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241, 17, 25, 37, 52, 63, 75, 92, 102,
119, 132, 144, 160, 175, 191, 212, 231, 19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187,
200, 213, 227, 242, 18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239,
16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240, 15, 21, 35, 50,
61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237,
}
T.NLSF_CB1_WB_Wght_Q9 = {
3657, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2963, 2963, 2925, 2846, 3216, 3085, 2972, 3056,
3056, 3010, 3010, 3010, 2963, 2963, 3010, 2972, 2888, 2846, 2846, 2726, 3920, 4014, 2981, 3207, 3207, 2934, 3056, 2846,
3122, 3244, 2925, 2846, 2620, 2553, 2780, 2925, 3516, 3197, 3010, 3103, 3019, 2888, 2925, 2925, 2925, 2925, 2888, 2888,
2888, 2888, 2888, 2753, 5054, 5054, 2934, 3573, 3385, 3056, 3085, 2793, 3160, 3160, 2972, 2846, 2513, 2540, 2753, 2888,
4428, 4149, 2700, 2753, 2972, 3010, 2925, 2846, 2981, 3019, 2925, 2925, 2925, 2925, 2888, 2726, 3620, 3019, 2972, 3056,
3056, 2873, 2806, 3056, 3216, 3047, 2981, 3291, 3291, 2981, 3310, 2991, 5227, 5014, 2540, 3338, 3526, 3385, 3197, 3094,
3376, 2981, 2700, 2647, 2687, 2793, 2846, 2673, 5081, 5174, 4615, 4428, 2460, 2897, 3047, 3207, 3169, 2687, 2740, 2888,
2846, 2793, 2846, 2700, 3122, 2888, 2963, 2925, 2925, 2925, 2925, 2963, 2963, 2963, 2963, 2925, 2925, 2963, 2963, 2963,
4202, 3207, 2981, 3103, 3010, 2888, 2888, 2925, 2972, 2873, 2916, 3019, 2972, 3010, 3197, 2873, 3760, 3760, 3244, 3103,
2981, 2888, 2925, 2888, 2972, 2934, 2793, 2793, 2846, 2888, 2888, 2660, 3854, 4014, 3207, 3122, 3244, 2934, 3047, 2963,
2963, 3085, 2846, 2793, 2793, 2793, 2793, 2580, 3845, 4080, 3357, 3516, 3094, 2740, 3010, 2934, 3122, 3085, 2846, 2846,
2647, 2647, 2846, 2806, 5147, 4894, 3225, 3845, 3441, 3169, 2897, 3413, 3451, 2700, 2580, 2673, 2740, 2846, 2806, 2753,
4109, 3789, 3291, 3160, 2925, 2888, 2888, 2925, 2793, 2740, 2793, 2740, 2793, 2846, 2888, 2806, 5081, 5054, 3047, 3545,
3244, 3056, 3085, 2944, 3103, 2897, 2740, 2740, 2740, 2846, 2793, 2620, 4309, 4309, 2860, 2527, 3207, 3376, 3376, 3075,
3075, 3376, 3056, 2846, 2647, 2580, 2726, 2753, 3056, 2916, 2806, 2888, 2740, 2687, 2897, 3103, 3150, 3150, 3216, 3169,
3056, 3010, 2963, 2846, 4375, 3882, 2925, 2888, 2846, 2888, 2846, 2846, 2888, 2888, 2888, 2846, 2888, 2925, 2888, 2846,
2981, 2916, 2916, 2981, 2981, 3056, 3122, 3216, 3150, 3056, 3010, 2972, 2972, 2972, 2925, 2740, 4229, 4149, 3310, 3347,
2925, 2963, 2888, 2981, 2981, 2846, 2793, 2740, 2846, 2846, 2846, 2793, 4080, 4014, 3103, 3010, 2925, 2925, 2925, 2888,
2925, 2925, 2846, 2846, 2846, 2793, 2888, 2780, 4615, 4575, 3169, 3441, 3207, 2981, 2897, 3038, 3122, 2740, 2687, 2687,
2687, 2740, 2793, 2700, 4149, 4269, 3789, 3657, 2726, 2780, 2888, 2888, 3010, 2972, 2925, 2846, 2687, 2687, 2793, 2888,
4215, 3554, 2753, 2846, 2846, 2888, 2888, 2888, 2925, 2925, 2888, 2925, 2925, 2925, 2963, 2888, 5174, 4921, 2261, 3432,
3789, 3479, 3347, 2846, 3310, 3479, 3150, 2897, 2460, 2487, 2753, 2925, 3451, 3685, 3122, 3197, 3357, 3047, 3207, 3207,
2981, 3216, 3085, 2925, 2925, 2687, 2540, 2434, 2981, 3010, 2793, 2793, 2740, 2793, 2846, 2972, 3056, 3103, 3150, 3150,
3150, 3103, 3010, 3010, 2944, 2873, 2687, 2726, 2780, 3010, 3432, 3545, 3357, 3244, 3056, 3010, 2963, 2925, 2888, 2846,
3019, 2944, 2897, 3010, 3010, 2972, 3019, 3103, 3056, 3056, 3010, 2888, 2846, 2925, 2925, 2888, 3920, 3967, 3010, 3197,
3357, 3216, 3291, 3291, 3479, 3704, 3441, 2726, 2181, 2460, 2580, 2607,
}
T.NLSF_CB1_iCDF_WB = {
225, 204, 201, 184, 183, 175, 158, 154, 153, 135, 119, 115, 113, 110, 109, 99, 98, 95, 79, 68,
52, 50, 48, 45, 43, 32, 31, 27, 18, 10, 3, 0, 255, 251, 235, 230, 212, 201, 196, 182,
167, 166, 163, 151, 138, 124, 110, 104, 90, 78, 76, 70, 69, 57, 45, 34, 24, 21, 11, 6,
5, 4, 3, 0,
}
T.NLSF_CB2_SELECT_WB = {
0, 0, 0, 0, 0, 0, 0, 1, 100, 102, 102, 68, 68, 36, 34, 96, 164, 107, 158, 185,
180, 185, 139, 102, 64, 66, 36, 34, 34, 0, 1, 32, 208, 139, 141, 191, 152, 185, 155, 104,
96, 171, 104, 166, 102, 102, 102, 132, 1, 0, 0, 0, 0, 16, 16, 0, 80, 109, 78, 107,
185, 139, 103, 101, 208, 212, 141, 139, 173, 153, 123, 103, 36, 0, 0, 0, 0, 0, 0, 1,
48, 0, 0, 0, 0, 0, 0, 32, 68, 135, 123, 119, 119, 103, 69, 98, 68, 103, 120, 118,
118, 102, 71, 98, 134, 136, 157, 184, 182, 153, 139, 134, 208, 168, 248, 75, 189, 143, 121, 107,
32, 49, 34, 34, 34, 0, 17, 2, 210, 235, 139, 123, 185, 137, 105, 134, 98, 135, 104, 182,
100, 183, 171, 134, 100, 70, 68, 70, 66, 66, 34, 131, 64, 166, 102, 68, 36, 2, 1, 0,
134, 166, 102, 68, 34, 34, 66, 132, 212, 246, 158, 139, 107, 107, 87, 102, 100, 219, 125, 122,
137, 118, 103, 132, 114, 135, 137, 105, 171, 106, 50, 34, 164, 214, 141, 143, 185, 151, 121, 103,
192, 34, 0, 0, 0, 0, 0, 1, 208, 109, 74, 187, 134, 249, 159, 137, 102, 110, 154, 118,
87, 101, 119, 101, 0, 2, 0, 36, 36, 66, 68, 35, 96, 164, 102, 100, 36, 0, 2, 33,
167, 138, 174, 102, 100, 84, 2, 2, 100, 107, 120, 119, 36, 197, 24, 0,
}
T.NLSF_CB2_iCDF_WB = {
255, 254, 253, 244, 12, 3, 2, 1, 0, 255, 254, 252, 224, 38, 3, 2, 1, 0, 255, 254,
251, 209, 57, 4, 2, 1, 0, 255, 254, 244, 195, 69, 4, 2, 1, 0, 255, 251, 232, 184,
84, 7, 2, 1, 0, 255, 254, 240, 186, 86, 14, 2, 1, 0, 255, 254, 239, 178, 91, 30,
5, 1, 0, 255, 248, 227, 177, 100, 19, 2, 1, 0,
}
T.NLSF_PRED_WB_Q8 = {
175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182, 68, 62, 66, 60, 72,
117, 85, 90, 118, 136, 151, 142, 160, 142, 155,
}
T.NLSF_DELTA_MIN_WB_Q15 = {
100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3, 347,
}
T.NLSF_CB1_NB_MB_Q8 = {
12, 35, 60, 83, 108, 132, 157, 180, 206, 228, 15, 32, 55, 77, 101, 125, 151, 175, 201, 225,
19, 42, 66, 89, 114, 137, 162, 184, 209, 230, 12, 25, 50, 72, 97, 120, 147, 172, 200, 223,
26, 44, 69, 90, 114, 135, 159, 180, 205, 225, 13, 22, 53, 80, 106, 130, 156, 180, 205, 228,
15, 25, 44, 64, 90, 115, 142, 168, 196, 222, 19, 24, 62, 82, 100, 120, 145, 168, 190, 214,
22, 31, 50, 79, 103, 120, 151, 170, 203, 227, 21, 29, 45, 65, 106, 124, 150, 171, 196, 224,
30, 49, 75, 97, 121, 142, 165, 186, 209, 229, 19, 25, 52, 70, 93, 116, 143, 166, 192, 219,
26, 34, 62, 75, 97, 118, 145, 167, 194, 217, 25, 33, 56, 70, 91, 113, 143, 165, 196, 223,
21, 34, 51, 72, 97, 117, 145, 171, 196, 222, 20, 29, 50, 67, 90, 117, 144, 168, 197, 221,
22, 31, 48, 66, 95, 117, 146, 168, 196, 222, 24, 33, 51, 77, 116, 134, 158, 180, 200, 224,
21, 28, 70, 87, 106, 124, 149, 170, 194, 217, 26, 33, 53, 64, 83, 117, 152, 173, 204, 225,
27, 34, 65, 95, 108, 129, 155, 174, 210, 225, 20, 26, 72, 99, 113, 131, 154, 176, 200, 219,
34, 43, 61, 78, 93, 114, 155, 177, 205, 229, 23, 29, 54, 97, 124, 138, 163, 179, 209, 229,
30, 38, 56, 89, 118, 129, 158, 178, 200, 231, 21, 29, 49, 63, 85, 111, 142, 163, 193, 222,
27, 48, 77, 103, 133, 158, 179, 196, 215, 232, 29, 47, 74, 99, 124, 151, 176, 198, 220, 237,
33, 42, 61, 76, 93, 121, 155, 174, 207, 225, 29, 53, 87, 112, 136, 154, 170, 188, 208, 227,
24, 30, 52, 84, 131, 150, 166, 186, 203, 229, 37, 48, 64, 84, 104, 118, 156, 177, 201, 230,
}
T.NLSF_CB1_Wght_Q9 = {
2897, 2314, 2314, 2314, 2287, 2287, 2314, 2300, 2327, 2287, 2888, 2580, 2394, 2367, 2314, 2274, 2274, 2274, 2274, 2194,
2487, 2340, 2340, 2314, 2314, 2314, 2340, 2340, 2367, 2354, 3216, 2766, 2340, 2340, 2314, 2274, 2221, 2207, 2261, 2194,
2460, 2474, 2367, 2394, 2394, 2394, 2394, 2367, 2407, 2314, 3479, 3056, 2127, 2207, 2274, 2274, 2274, 2287, 2314, 2261,
3282, 3141, 2580, 2394, 2247, 2221, 2207, 2194, 2194, 2114, 4096, 3845, 2221, 2620, 2620, 2407, 2314, 2394, 2367, 2074,
3178, 3244, 2367, 2221, 2553, 2434, 2340, 2314, 2167, 2221, 3338, 3488, 2726, 2194, 2261, 2460, 2354, 2367, 2207, 2101,
2354, 2420, 2327, 2367, 2394, 2420, 2420, 2420, 2460, 2367, 3779, 3629, 2434, 2527, 2367, 2274, 2274, 2300, 2207, 2048,
3254, 3225, 2713, 2846, 2447, 2327, 2300, 2300, 2274, 2127, 3263, 3300, 2753, 2806, 2447, 2261, 2261, 2247, 2127, 2101,
2873, 2981, 2633, 2367, 2407, 2354, 2194, 2247, 2247, 2114, 3225, 3197, 2633, 2580, 2274, 2181, 2247, 2221, 2221, 2141,
3178, 3310, 2740, 2407, 2274, 2274, 2274, 2287, 2194, 2114, 3141, 3272, 2460, 2061, 2287, 2500, 2367, 2487, 2434, 2181,
3507, 3282, 2314, 2700, 2647, 2474, 2367, 2394, 2340, 2127, 3423, 3535, 3038, 3056, 2300, 1950, 2221, 2274, 2274, 2274,
3404, 3366, 2087, 2687, 2873, 2354, 2420, 2274, 2474, 2540, 3760, 3488, 1950, 2660, 2897, 2527, 2394, 2367, 2460, 2261,
3028, 3272, 2740, 2888, 2740, 2154, 2127, 2287, 2234, 2247, 3695, 3657, 2025, 1969, 2660, 2700, 2580, 2500, 2327, 2367,
3207, 3413, 2354, 2074, 2888, 2888, 2340, 2487, 2247, 2167, 3338, 3366, 2846, 2780, 2327, 2154, 2274, 2287, 2114, 2061,
2327, 2300, 2181, 2167, 2181, 2367, 2633, 2700, 2700, 2553, 2407, 2434, 2221, 2261, 2221, 2221, 2340, 2420, 2607, 2700,
3038, 3244, 2806, 2888, 2474, 2074, 2300, 2314, 2354, 2380, 2221, 2154, 2127, 2287, 2500, 2793, 2793, 2620, 2580, 2367,
3676, 3713, 2234, 1838, 2181, 2753, 2726, 2673, 2513, 2207, 2793, 3160, 2726, 2553, 2846, 2513, 2181, 2394, 2221, 2181,
}
T.NLSF_CB1_iCDF_NB_MB = {
212, 178, 148, 129, 108, 96, 85, 82, 79, 77, 61, 59, 57, 56, 51, 49, 48, 45, 42, 41,
40, 38, 36, 34, 31, 30, 21, 12, 10, 3, 1, 0, 255, 245, 244, 236, 233, 225, 217, 203,
190, 176, 175, 161, 149, 136, 125, 114, 102, 91, 81, 71, 60, 52, 43, 35, 28, 20, 19, 18,
12, 11, 5, 0,
}
T.NLSF_CB2_SELECT_NB_MB = {
16, 0, 0, 0, 0, 99, 66, 36, 36, 34, 36, 34, 34, 34, 34, 83, 69, 36, 52, 34,
116, 102, 70, 68, 68, 176, 102, 68, 68, 34, 65, 85, 68, 84, 36, 116, 141, 152, 139, 170,
132, 187, 184, 216, 137, 132, 249, 168, 185, 139, 104, 102, 100, 68, 68, 178, 218, 185, 185, 170,
244, 216, 187, 187, 170, 244, 187, 187, 219, 138, 103, 155, 184, 185, 137, 116, 183, 155, 152, 136,
132, 217, 184, 184, 170, 164, 217, 171, 155, 139, 244, 169, 184, 185, 170, 164, 216, 223, 218, 138,
214, 143, 188, 218, 168, 244, 141, 136, 155, 170, 168, 138, 220, 219, 139, 164, 219, 202, 216, 137,
168, 186, 246, 185, 139, 116, 185, 219, 185, 138, 100, 100, 134, 100, 102, 34, 68, 68, 100, 68,
168, 203, 221, 218, 168, 167, 154, 136, 104, 70, 164, 246, 171, 137, 139, 137, 155, 218, 219, 139,
}
T.NLSF_CB2_iCDF_NB_MB = {
255, 254, 253, 238, 14, 3, 2, 1, 0, 255, 254, 252, 218, 35, 3, 2, 1, 0, 255, 254,
250, 208, 59, 4, 2, 1, 0, 255, 254, 246, 194, 71, 10, 2, 1, 0, 255, 252, 236, 183,
82, 8, 2, 1, 0, 255, 252, 235, 180, 90, 17, 2, 1, 0, 255, 248, 224, 171, 97, 30,
4, 1, 0, 255, 254, 236, 173, 95, 37, 7, 1, 0,
}
T.NLSF_PRED_NB_MB_Q8 = {
179, 138, 140, 148, 151, 149, 153, 151, 163, 116, 67, 82, 59, 92, 72, 100, 89, 92,
}
T.NLSF_DELTA_MIN_NB_MB_Q15 = {
250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461,
}
T.pitch_lag_iCDF = {
253, 250, 244, 233, 212, 182, 150, 131, 120, 110, 98, 85, 72, 60, 49, 40, 32, 25, 19, 15,
13, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
}
T.pitch_delta_iCDF = {
210, 208, 206, 203, 199, 193, 183, 168, 142, 104, 74, 52, 37, 27, 20, 14, 10, 6, 4, 2,
0,
}
T.pitch_contour_iCDF = {
223, 201, 183, 167, 152, 138, 124, 111, 98, 88, 79, 70, 62, 56, 50, 44, 39, 35, 31, 27,
24, 21, 18, 16, 14, 12, 10, 8, 6, 4, 3, 2, 1, 0,
}
T.pitch_contour_NB_iCDF = {
188, 176, 155, 138, 119, 97, 67, 43, 26, 10, 0,
}
T.pitch_contour_10_ms_iCDF = {
165, 119, 80, 61, 47, 35, 27, 20, 14, 9, 4, 0,
}
T.pitch_contour_10_ms_NB_iCDF = {
113, 63, 0,
}
T.stereo_pred_quant_Q13 = {
-13732, -10050, -8266, -7526, -6500, -5000, -2950, -820, 820, 2950, 5000, 6500, 7526, 8266, 10050, 13732,
}
T.stereo_pred_joint_iCDF = {
249, 247, 246, 245, 244, 234, 210, 202, 201, 200, 197, 174, 82, 59, 56, 55, 54, 46, 22, 12,
11, 10, 9, 7, 0,
}
T.stereo_only_code_mid_iCDF = {
64, 0,
}
T.LBRR_flags_2_iCDF = {
203, 150, 0,
}
T.LBRR_flags_3_iCDF = {
215, 195, 166, 125, 110, 82, 0,
}
T.lsb_iCDF = {
120, 0,
}
T.LTPscale_iCDF = {
128, 64, 0,
}
T.type_offset_VAD_iCDF = {
232, 158, 10, 0,
}
T.type_offset_no_VAD_iCDF = {
230, 0,
}
T.NLSF_interpolation_factor_iCDF = {
243, 221, 192, 181, 0,
}
T.LTPScales_table_Q14 = {
15565, 12288, 8192,
}
T.uniform3_iCDF = {
171, 85, 0,
}
T.uniform4_iCDF = {
192, 128, 64, 0,
}
T.uniform5_iCDF = {
205, 154, 102, 51, 0,
}
T.uniform6_iCDF = {
213, 171, 128, 85, 43, 0,
}
T.uniform8_iCDF = {
224, 192, 160, 128, 96, 64, 32, 0,
}
T.NLSF_EXT_iCDF = {
100, 40, 16, 7, 3, 1, 0,
}
T.gain_iCDF = {
224, 112, 44, 15, 3, 2, 1, 0, 254, 237, 192, 132, 70, 23, 4, 0, 255, 252, 226, 155,
61, 11, 2, 0,
}
T.delta_gain_iCDF = {
250, 245, 234, 203, 71, 50, 42, 38, 35, 33, 31, 29, 28, 27, 26, 25, 24, 23, 22, 21,
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
0,
}
T.LSFCosTab_FIX_Q12 = {
8192, 8190, 8182, 8170, 8152, 8130, 8104, 8072, 8034, 7994, 7946, 7896, 7840, 7778, 7714, 7644, 7568, 7490, 7406, 7318,
7226, 7128, 7026, 6922, 6812, 6698, 6580, 6458, 6332, 6204, 6070, 5934, 5792, 5648, 5502, 5352, 5198, 5040, 4880, 4718,
4552, 4382, 4212, 4038, 3862, 3684, 3502, 3320, 3136, 2948, 2760, 2570, 2378, 2186, 1990, 1794, 1598, 1400, 1202, 1002,
802, 602, 402, 202, 0, -202, -402, -602, -802, -1002, -1202, -1400, -1598, -1794, -1990, -2186, -2378, -2570, -2760, -2948,
-3136, -3320, -3502, -3684, -3862, -4038, -4212, -4382, -4552, -4718, -4880, -5040, -5198, -5352, -5502, -5648, -5792, -5934, -6070, -6204,
-6332, -6458, -6580, -6698, -6812, -6922, -7026, -7128, -7226, -7318, -7406, -7490, -7568, -7644, -7714, -7778, -7840, -7896, -7946, -7994,
-8034, -8072, -8104, -8130, -8152, -8170, -8182, -8190, -8192,
}
T.pulses_per_block_iCDF = {
125, 51, 26, 18, 15, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 198, 105,
45, 22, 15, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 213, 162, 116, 83,
59, 43, 32, 24, 18, 15, 12, 9, 7, 6, 5, 3, 2, 0, 239, 187, 116, 59, 28, 16,
11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 250, 229, 188, 135, 86, 51, 30, 19,
13, 10, 8, 6, 5, 4, 3, 2, 1, 0, 249, 235, 213, 185, 156, 128, 103, 83, 66, 53,
42, 33, 26, 21, 17, 13, 10, 0, 254, 249, 235, 206, 164, 118, 77, 46, 27, 16, 10, 7,
5, 4, 3, 2, 1, 0, 255, 253, 249, 239, 220, 191, 156, 119, 85, 57, 37, 23, 15, 10,
6, 4, 2, 0, 255, 253, 251, 246, 237, 223, 203, 179, 152, 124, 98, 75, 55, 40, 29, 21,
15, 0, 255, 254, 253, 247, 220, 162, 106, 67, 42, 28, 18, 12, 9, 6, 4, 3, 2, 0,
}
T.rate_levels_iCDF = {
241, 190, 178, 132, 87, 74, 41, 14, 0, 223, 193, 157, 140, 106, 57, 39, 18, 0,
}
T.shell_code_table0 = {
128, 0, 214, 42, 0, 235, 128, 21, 0, 244, 184, 72, 11, 0, 248, 214, 128, 42, 7, 0,
248, 225, 170, 80, 25, 5, 0, 251, 236, 198, 126, 54, 18, 3, 0, 250, 238, 211, 159, 82,
35, 15, 5, 0, 250, 231, 203, 168, 128, 88, 53, 25, 6, 0, 252, 238, 216, 185, 148, 108,
71, 40, 18, 4, 0, 253, 243, 225, 199, 166, 128, 90, 57, 31, 13, 3, 0, 254, 246, 233,
212, 183, 147, 109, 73, 44, 23, 10, 2, 0, 255, 250, 240, 223, 198, 166, 128, 90, 58, 33,
16, 6, 1, 0, 255, 251, 244, 231, 210, 181, 146, 110, 75, 46, 25, 12, 5, 1, 0, 255,
253, 248, 238, 221, 196, 164, 128, 92, 60, 35, 18, 8, 3, 1, 0, 255, 253, 249, 242, 229,
208, 180, 146, 110, 76, 48, 27, 14, 7, 3, 1, 0,
}
T.shell_code_table1 = {
129, 0, 207, 50, 0, 236, 129, 20, 0, 245, 185, 72, 10, 0, 249, 213, 129, 42, 6, 0,
250, 226, 169, 87, 27, 4, 0, 251, 233, 194, 130, 62, 20, 4, 0, 250, 236, 207, 160, 99,
47, 17, 3, 0, 255, 240, 217, 182, 131, 81, 41, 11, 1, 0, 255, 254, 233, 201, 159, 107,
61, 20, 2, 1, 0, 255, 249, 233, 206, 170, 128, 86, 50, 23, 7, 1, 0, 255, 250, 238,
217, 186, 148, 108, 70, 39, 18, 6, 1, 0, 255, 252, 243, 226, 200, 166, 128, 90, 56, 30,
13, 4, 1, 0, 255, 252, 245, 231, 209, 180, 146, 110, 76, 47, 25, 11, 4, 1, 0, 255,
253, 248, 237, 219, 194, 163, 128, 93, 62, 37, 19, 8, 3, 1, 0, 255, 254, 250, 241, 226,
205, 177, 145, 111, 79, 51, 30, 15, 6, 2, 1, 0,
}
T.shell_code_table2 = {
129, 0, 203, 54, 0, 234, 129, 23, 0, 245, 184, 73, 10, 0, 250, 215, 129, 41, 5, 0,
252, 232, 173, 86, 24, 3, 0, 253, 240, 200, 129, 56, 15, 2, 0, 253, 244, 217, 164, 94,
38, 10, 1, 0, 253, 245, 226, 189, 132, 71, 27, 7, 1, 0, 253, 246, 231, 203, 159, 105,
56, 23, 6, 1, 0, 255, 248, 235, 213, 179, 133, 85, 47, 19, 5, 1, 0, 255, 254, 243,
221, 194, 159, 117, 70, 37, 12, 2, 1, 0, 255, 254, 248, 234, 208, 171, 128, 85, 48, 22,
8, 2, 1, 0, 255, 254, 250, 240, 220, 189, 149, 107, 67, 36, 16, 6, 2, 1, 0, 255,
254, 251, 243, 227, 201, 166, 128, 90, 55, 29, 13, 5, 2, 1, 0, 255, 254, 252, 246, 234,
213, 183, 147, 109, 73, 43, 22, 10, 4, 2, 1, 0,
}
T.shell_code_table3 = {
130, 0, 200, 58, 0, 231, 130, 26, 0, 244, 184, 76, 12, 0, 249, 214, 130, 43, 6, 0,
252, 232, 173, 87, 24, 3, 0, 253, 241, 203, 131, 56, 14, 2, 0, 254, 246, 221, 167, 94,
35, 8, 1, 0, 254, 249, 232, 193, 130, 65, 23, 5, 1, 0, 255, 251, 239, 211, 162, 99,
45, 15, 4, 1, 0, 255, 251, 243, 223, 186, 131, 74, 33, 11, 3, 1, 0, 255, 252, 245,
230, 202, 158, 105, 57, 24, 8, 2, 1, 0, 255, 253, 247, 235, 214, 179, 132, 84, 44, 19,
7, 2, 1, 0, 255, 254, 250, 240, 223, 196, 159, 112, 69, 36, 15, 6, 2, 1, 0, 255,
254, 253, 245, 231, 209, 176, 136, 93, 55, 27, 11, 3, 2, 1, 0, 255, 254, 253, 252, 239,
221, 194, 158, 117, 76, 42, 18, 4, 3, 2, 1, 0,
}
T.shell_code_table_offsets = {
0, 0, 2, 5, 9, 14, 20, 27, 35, 44, 54, 65, 77, 90, 104, 119, 135,
}
T.sign_iCDF = {
254, 49, 67, 77, 82, 93, 99, 198, 11, 18, 24, 31, 36, 45, 255, 46, 66, 78, 87, 94,
104, 208, 14, 21, 32, 42, 51, 66, 255, 94, 104, 109, 112, 115, 118, 248, 53, 69, 80, 88,
95, 102,
}
T.CB_lags_stage2_10_ms = {
0, 1, 0, 0, 0, 1,
}
T.CB_lags_stage3_10_ms = {
0, 0, 1, -1, 1, -1, 2, -2, 2, -2, 3, -3, 0, 1, 0, 1, -1, 2, -1, 2,
-2, 3, -2, 3,
}
T.CB_lags_stage2 = {
0, 2, -1, -1, -1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 2, 1, 0, 1, 1,
0, 0, -1, -1,
}
T.CB_lags_stage3 = {
0, 0, 1, -1, 0, 1, -1, 0, -1, 1, -2, 2, -2, -2, 2, -3, 2, 3, -3, -4,
3, -4, 4, 4, -5, 5, -6, -5, 6, -7, 6, 5, 8, -9, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, -1, 1, 0, 0, 1, -1, 0, 1, -1, -1, 1, -1, 2, 1, -1, 2,
-2, -2, 2, -2, 2, 2, 3, -3, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
0, 1, -1, 1, 0, 0, 2, 1, -1, 2, -1, -1, 2, -1, 2, 2, -1, 3, -2, -2,
-2, 3, 0, 1, 0, 0, 1, 0, 1, -1, 2, -1, 2, -1, 2, 3, -2, 3, -2, -2,
4, 4, -3, 5, -3, -4, 6, -4, 6, 5, -5, 8, -6, -5, -7, 9,
}
T.resampler_frac_FIR_12 = {
189, -600, 617, 30567, 117, -159, -1070, 29704, 52, 221, -2392, 28276, -4, 529, -3350, 26341, -48, 758, -3956, 23973,
-80, 905, -4235, 21254, -99, 972, -4222, 18278, -107, 967, -3957, 15143, -103, 896, -3487, 11950, -91, 773, -2865, 8798,
-71, 611, -2143, 5784, -46, 425, -1375, 2996,
}
return T
+408
View File
@@ -0,0 +1,408 @@
--!strict
-- audio/opus: Ogg Opus (RFC 6716 codec, RFC 7845 encapsulation). Absorbed from opus-luau dd2b4e5 (same author; a
-- port of the libopus 1.6 decoder, BSD: licenses/libopus-BSD.txt): the range decoder, CELT (float), SILK
-- (bit-exact fixed point), hybrid, mode transitions, PLC, multistream (mapping families 0, 1, 255).
-- Supports: decode of every Opus mode / bandwidth / frame size, mono, stereo and surround (optional stereo downmix,
-- opts.stereo), pre-skip, end trimming, output gain, OpusTags, sample-accurate seeking (granule bisection + 80 ms
-- pre-roll), damaged files (concealment). Output is always 48 kHz f32.
-- Not supported: encoding, mapping family 3 (ambisonics projection), chained streams (the first Opus stream is
-- decoded), CELT pitch PLC (noise PLC only).
-- Changes from opus-luau: the injected Ogg demuxer is containers/Ogg; errors are core/Err values; read() takes an
-- optional output buffer; decode() fully decodes to an Audio.
--
-- local dec, err = Opus.open(Bytes.reader(buf)) -- the Types.Decoder: dec:read(n, out?), dec:seek(s), ...
-- local audio = Opus.decode(Bytes.reader(buf)) -- a Types.Audio
-- Opus.Decoder.new(channels) + Opus.Decoder.decode(st, buf, off, len, pcm, base, maxFrames) -- raw packets
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Ogg = require("@formats/containers/Ogg")
local Decoder = require("@formats/audio/opus/Decoder")
local Packet = require("@formats/audio/opus/Packet")
local RangeDecoder = require("@formats/audio/opus/RangeDecoder")
local Celt = require("@formats/audio/opus/Celt")
local Silk = require("@formats/audio/opus/Silk")
-- ------------------------------------------------------------------------------------------------ contract
local Opus = {
name = "opus",
category = "audio",
extensions = { "opus" },
mime = { "audio/opus", "audio/ogg" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg" },
caps = { decode = true, encode = false, streaming = true, seek = true, lossy = true, channels = 255, rates = { 48000 } },
}
-- ------------------------------------------------------------------------------------------------ types
export type OpenOptions = {
verify: boolean?, -- check the Ogg page CRCs (default true; false decodes damaged files)
stereo: boolean?, -- downmix more than 2 channels to stereo (default false; dec.channels is then 2)
}
-- ------------------------------------------------------------------------------------------------ constants
local SAMPLE_RATE = 48000
-- stereo downmix weights for the Vorbis channel orders of mapping family 1 (3 to 8 channels), as in opusfile
local DOWNMIX = {
[3] = { 0.5858, 0, 0.4142, 0.4142, 0, 0.5858 },
[4] = { 0.4226, 0, 0, 0.4226, 0.366, 0.2114, 0.2114, 0.366 },
[5] = { 0.651, 0, 0.46, 0.46, 0, 0.651, 0.5636, 0.3254, 0.3254, 0.5636 },
[6] = { 0.529, 0, 0.3741, 0.3741, 0, 0.529, 0.4582, 0.2645, 0.2645, 0.4582, 0.3741, 0.3741 },
[7] = { 0.4553, 0, 0.322, 0.322, 0, 0.4553, 0.3943, 0.2277, 0.2277, 0.3943, 0.2788, 0.2788, 0.322, 0.322 },
[8] = { 0.3886, 0, 0.2748, 0.2748, 0, 0.3886, 0.3366, 0.1943, 0.1943, 0.3366, 0.3366, 0.1943, 0.1943, 0.3366, 0.2748, 0.2748 },
}
local PREROLL = 3840 -- 80 ms, RFC 7845 section 4.6
local MAX_PACKET = 5760 -- 120 ms
export type Head = {
version: number,
channels: number,
preSkip: number,
inputSampleRate: number,
outputGain: number, -- Q7.8 dB
mappingFamily: number,
streams: number,
coupled: number,
mapping: { number },
}
-- OpusHead (RFC 7845 section 5.1). Returns the header or nil, message.
local function parseHead(b: buffer, off: number, size: number): (Head?, string?)
if size < 19 or buffer.readstring(b, off, 8) ~= "OpusHead" then
return nil, "not an OpusHead packet"
end
local version = buffer.readu8(b, off + 8)
if bit32.rshift(version, 4) ~= 0 then
return nil, "unsupported OpusHead version " .. version
end
local channels = buffer.readu8(b, off + 9)
if channels < 1 then
return nil, "OpusHead: zero channels"
end
local head: Head = {
version = version,
channels = channels,
preSkip = buffer.readu16(b, off + 10),
inputSampleRate = buffer.readu32(b, off + 12),
outputGain = buffer.readi16(b, off + 16),
mappingFamily = buffer.readu8(b, off + 18),
streams = 1,
coupled = channels - 1,
mapping = {},
}
if head.mappingFamily == 0 then
if channels > 2 then
return nil, "OpusHead: mapping family 0 with more than 2 channels"
end
head.mapping = if channels == 2 then { 0, 1 } else { 0 }
else
if head.mappingFamily == 3 then
return nil, "unsupported channel mapping family 3 (projection)"
end
if size < 21 + channels then
return nil, "OpusHead: truncated channel mapping table"
end
head.streams = buffer.readu8(b, off + 19)
head.coupled = buffer.readu8(b, off + 20)
if head.streams < 1 or head.coupled > head.streams or head.streams + head.coupled > 255 then
return nil, "OpusHead: bad stream counts"
end
for c = 0, channels - 1 do
local m = buffer.readu8(b, off + 21 + c)
if m ~= 255 and m >= head.streams + head.coupled then
return nil, "OpusHead: bad channel mapping"
end
head.mapping[c + 1] = m
end
end
return head, nil
end
-- OpusTags (RFC 7845 section 5.2): vendor string and a table of lower-cased field names to values (repeated
-- fields joined with "; ").
local function parseTags(b: buffer, off: number, size: number): (string, { [string]: string })
local tags = {}
if size < 16 or buffer.readstring(b, off, 8) ~= "OpusTags" then
return "", tags
end
local p = off + 8
local e = off + size
local vlen = buffer.readu32(b, p)
p += 4
if p + vlen > e then
return "", tags
end
local vendor = buffer.readstring(b, p, vlen)
p += vlen
if p + 4 > e then
return vendor, tags
end
local n = buffer.readu32(b, p)
p += 4
for _ = 1, n do
if p + 4 > e then
break
end
local l = buffer.readu32(b, p)
p += 4
if p + l > e then
break
end
local s = buffer.readstring(b, p, l)
p += l
local eq = string.find(s, "=", 1, true)
if eq then
local k = string.lower(string.sub(s, 1, eq - 1))
local v = string.sub(s, eq + 1)
tags[k] = if tags[k] then tags[k] .. "; " .. v else v
end
end
return vendor, tags
end
Opus.Decoder = Decoder
Opus.Packet = Packet
Opus.RangeDecoder = RangeDecoder
Opus.Celt = Celt
Opus.Silk = Silk
Opus.parseHead = parseHead
Opus.parseTags = parseTags
Opus.SAMPLE_RATE = SAMPLE_RATE
-- ------------------------------------------------------------------------------------------------ sniff
-- 1 when the file starts with an Ogg page whose first packet is an OpusHead
function Opus.sniff(r: Bytes.Reader): number
if r:peek(4) ~= "OggS" then
return 0
end
local nseg = r:peekU8(26)
if nseg < 1 then
return 0
end
return if r:peek(8, 27 + nseg) == "OpusHead" then 1 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
-- decode(r, opts?) -> Audio | nil, Err: the whole stream (48 kHz f32)
function Opus.decode(r: any, opts: OpenOptions?): (Types.Audio?, Err.Err?)
local dec, err = Opus.open(r, opts)
if not dec then
return nil, err
end
local ch = dec.channels
local a = Types.Audio.new(SAMPLE_RATE, ch, dec.frames)
local total = 0
while total < dec.frames do
local pcm, n = dec:read(math.min(65536, dec.frames - total))
if n == 0 then
break
end
buffer.copy(a.samples, total * ch * 4, pcm, 0, n * ch * 4)
total += n
end
if total < dec.frames then -- a truncated file: keep what decoded
local s = buffer.create(total * ch * 4)
buffer.copy(s, 0, a.samples, 0, total * ch * 4)
a.samples = s
a.frames = total
end
a.tags = dec.tags
a.meta.source = { format = "opus", channels = dec.sourceChannels, preSkip = dec.preSkip, vendor = dec.vendor, mappingFamily = dec.head.mappingFamily, inputSampleRate = dec.head.inputSampleRate }
return a, nil
end
-- ------------------------------------------------------------------------------------------------ open
local Stream = {}
Stream.__index = Stream
-- Decodes the next packet into the pending buffer. Returns false at the end of the stream.
local function decodeNext(self: any): boolean
local p = self.demux:nextPacket(self.serial)
if p == nil then
return false
end
local n
if p.size > 0 then
n = Decoder.decodeMultistream(self.ms, p.data, p.offset, p.size, self.pend, 1, MAX_PACKET)
else
n = -1
end
if n < 0 then
-- corrupt or empty packet: conceal for its nominal duration
local dur = if p.size > 0 then Packet.samples(p.data, p.offset, p.size) else -1
if dur <= 0 then
dur = self.lastDuration
end
n = Decoder.decodeMultistream(self.ms, nil, 0, 0, self.pend, 1, dur)
if n < 0 then
n = dur
for i = 1, n * self.channels do
self.pend[i] = 0
end
end
end
self.lastDuration = n
self.pendStart = self.decoded
self.pendLen = n
self.pendPos = 0
self.decoded += n
return true
end
-- dec:read(maxFrames, out?) -> buffer of interleaved f32 samples, frames read (0 at the end of the stream). out (if
-- given and big enough) is filled instead of a new buffer.
function Stream.read(self: any, maxFrames: number, into: buffer?): (buffer, number)
local ch = self.channels
local mf = if type(maxFrames) == "number" and maxFrames == maxFrames then maxFrames else 0
local want = math.max(0, math.min(math.floor(mf), self.endSample - self.next))
local reuse = into ~= nil and buffer.len(into) >= want * ch * 4
local out = if reuse then into :: buffer else buffer.create(want * ch * 4)
local got = 0
local gain = self.gain
while got < want do
if self.pendPos >= self.pendLen then
if not decodeNext(self) then
break
end
end
-- skip decoded samples before the next wanted one (pre-skip, seek pre-roll)
local s = self.pendStart + self.pendPos
if s < self.next then
self.pendPos = math.min(self.pendLen, self.pendPos + (self.next - s))
continue
end
local n = math.min(self.pendLen - self.pendPos, want - got)
if self.downmix then
Decoder.writeDownmix(self.pend, self.pendPos * self.sourceChannels, n, self.sourceChannels, self.downmix, gain, out, got * 8)
else
Decoder.writeF32(self.pend, self.pendPos * ch, n * ch, gain, out, got * ch * 4)
end
self.pendPos += n
got += n
self.next += n
end
if got < want then
if not reuse then
local trimmed = buffer.create(got * ch * 4)
buffer.copy(trimmed, 0, out, 0, got * ch * 4)
out = trimmed
end
self.endSample = self.next -- the stream ended early (truncated file)
end
return out, got
end
-- current playback position in seconds
function Stream.position(self: any): number
return (self.next - self.preSkip) / SAMPLE_RATE
end
-- seek to a time in seconds (clamped to the stream): granule bisection with 80 ms pre-roll
function Stream.seek(self: any, seconds: number)
local sec = if type(seconds) == "number" and seconds == seconds then seconds else 0
local p = math.floor(math.max(0, math.min(sec, 1e12)) * SAMPLE_RATE + 0.5)
p = math.min(p, self.frames)
local target = p + self.preSkip -- decoded-sample index of the wanted sample
Decoder.resetMultistream(self.ms)
self.pendLen, self.pendPos = 0, 0
local g = target - PREROLL + self.startGranule
local start = -1
if g > self.startGranule then
start = self.demux:seekGranule(self.serial, g) or -1
end
if start < 0 then
self.demux:rewind(self.serial)
self.demux:nextPacket(self.serial) -- OpusHead
self.demux:nextPacket(self.serial) -- OpusTags
self.decoded = 0
else
self.decoded = start - self.startGranule
end
self.pendStart = self.decoded
self.next = target
end
-- open(r, opts?) -> Decoder | nil, Err (r: a core/Bytes Reader, or a buffer)
function Opus.open(r: any, opts: OpenOptions?): (any, Err.Err?)
local demux, derr = Ogg.open(r, if opts and opts.verify == false then { verify = false } else nil)
if not demux then
return nil, derr
end
local serial = demux:find("opus")
if serial == nil then
return nil, Err.new(Err.CORRUPT, "no Opus stream in the Ogg file")
end
local hp = demux:nextPacket(serial)
if hp == nil then
return nil, Err.new(Err.TRUNCATED, "missing OpusHead")
end
local head, herr = parseHead(hp.data, hp.offset, hp.size)
if not head then
return nil, Err.new(if string.find(herr or "", "unsupported", 1, true) then Err.UNSUPPORTED else Err.CORRUPT, herr or "bad OpusHead")
end
local tp = demux:nextPacket(serial)
if tp == nil then
return nil, Err.new(Err.TRUNCATED, "missing OpusTags")
end
local vendor, tags = parseTags(tp.data, tp.offset, tp.size)
-- granule of the first audio sample: first page granule minus the packets that end on it
local startGranule = 0
local sum = 0
while true do
local p = demux:nextPacket(serial)
if p == nil then
break
end
local d = if p.size > 0 then Packet.samples(p.data, p.offset, p.size) else 0
sum += math.max(d, 0)
if p.granule ~= -1 then
startGranule = math.max(0, p.granule - sum)
break
end
end
demux:rewind(serial)
demux:nextPacket(serial)
demux:nextPacket(serial)
local info = demux:stream(serial)
local last = if info and info.lastGranule and info.lastGranule >= 0 then info.lastGranule else startGranule
local frames = math.max(0, last - startGranule - head.preSkip)
local downmix = nil
if opts and opts.stereo and head.channels > 2 then
downmix = DOWNMIX[head.channels]
if downmix == nil or head.mappingFamily ~= 1 then -- unknown layout: every channel to both sides, equally
downmix = table.create(head.channels * 2, 1 / head.channels)
end
end
local self = setmetatable({
demux = demux,
serial = serial,
head = head,
vendor = vendor,
tags = tags,
sampleRate = SAMPLE_RATE,
channels = if downmix then 2 else head.channels,
sourceChannels = head.channels,
downmix = downmix,
frames = frames,
duration = frames / SAMPLE_RATE,
bitrate = if frames > 0 then math.floor(demux.size * 8 / (frames / SAMPLE_RATE)) else 0,
preSkip = head.preSkip,
startGranule = startGranule,
gain = 10 ^ (head.outputGain / (20 * 256)),
ms = Decoder.newMultistream(head.channels, head.streams, head.coupled, head.mapping),
pend = table.create(MAX_PACKET * head.channels, 0),
pendStart = 0,
pendLen = 0,
pendPos = 0,
decoded = 0, -- decoded-sample index of the next packet's first sample
next = head.preSkip, -- decoded-sample index of the next sample read() returns
endSample = head.preSkip + frames,
lastDuration = 960,
}, Stream)
return self, nil
end
return Opus