ws-video: Vorbis granules from the block sizes (exact Ogg pages for Matroska Vorbis), ADTS AAC from TS passed through, audio checks read until filled

This commit is contained in:
4DBug committed 2026-10-31 09:53:16 -05:00
1 parent e044209ab1
commit 242ba7e5dc
2 files changed
+87 -2

No files matched your search

+77 -1
View File
@@ -225,6 +225,62 @@ local function xiphHeaders(c: buffer): { buffer }?
return out
end
-- Vorbis packet sizes: the block sizes from the identification header and each mode's block flag from the end of the
-- setup header (read backwards from its framing bit: mode entries are 41 bits whose window / transform types are 0,
-- preceded by the 6-bit mode count), so the Ogg granules are exact: a packet yields (previous + current block) / 4
-- samples (the first one as if its previous block had its own size, as ffmpeg times it).
local function bit(b: buffer, k: number): number
return bit32.extract(buffer.readu8(b, k // 8), k % 8, 1)
end
local function bits(b: buffer, k: number, n: number): number
local v = 0
for i = n - 1, 0, -1 do
v = v * 2 + bit(b, k + i)
end
return v
end
local function vorbisModes(id: buffer, setup: buffer): ({ number }?, { boolean }?)
if buffer.len(id) < 30 or buffer.len(setup) < 8 then
return nil
end
local bs = buffer.readu8(id, 28)
local sizes = { 2 ^ (bs % 16), 2 ^ (bs // 16) }
-- the framing bit: the highest set bit of the last non-zero byte
local last = buffer.len(setup) - 1
while last > 0 and buffer.readu8(setup, last) == 0 do
last -= 1
end
local v = buffer.readu8(setup, last)
local hb = 7
while hb > 0 and bit32.extract(v, hb, 1) == 0 do
hb -= 1
end
local f = last * 8 + hb
local flags: { boolean } = {}
local pos = f
while #flags < 64 and pos - 41 >= 7 * 8 do
local s0 = pos - 41
if bits(setup, s0 + 1, 16) ~= 0 or bits(setup, s0 + 17, 16) ~= 0 then
break
end
table.insert(flags, 1, bit(setup, s0) == 1)
pos = s0
end
-- the largest count whose 6-bit field matches
for n = #flags, 1, -1 do
local start = f - 41 * n
if start - 6 >= 0 and bits(setup, start - 6, 6) == n - 1 then
local out = {}
for i = #flags - n + 1, #flags do
table.insert(out, flags[i])
end
return sizes, out
end
end
return nil
end
-- ------------------------------------------------------------------------------------------------ ADTS
local function adts(track: Types.Track, packets: { Types.Packet }): (buffer?, Err.Err?)
local c = track.config
@@ -368,14 +424,34 @@ function AudioWrap.wrap(dmx: any, track: Types.Track): (buffer?, string?, Err.Er
if not headers or #headers ~= 3 then
return nil, nil, Err.new(Err.CORRUPT, "Vorbis track without its three headers")
end
local sizes, modes = vorbisModes(headers[1], headers[3])
local granule, prev = 0, nil :: number?
local rate = track.sampleRate or 44100
local scale = track.timescale
local out = ogg(headers, packets, function(p: Types.Packet): number
return math.floor((p.pts + (p.duration or 0)) / scale * rate + 0.5)
if not sizes or not modes then
return math.floor((p.pts + (p.duration or 0)) / scale * rate + 0.5) -- from the container's times
end
local d = p.data
if buffer.len(d) == 0 or buffer.readu8(d, 0) % 2 ~= 0 then
return granule
end
local mb = 0
while 2 ^ mb < #modes do
mb += 1
end
local m = if mb > 0 and buffer.len(d) * 8 > mb then bits(d, 1, mb) else 0
local cur = sizes[if modes[m + 1] then 2 else 1]
granule += ((prev or cur) + cur) // 4 -- the first packet counts as if after a block of its own size (ffmpeg)
prev = cur
return granule
end)
return out, "vorbis", nil
end
if codec == "aac" then
if track.adts then
return concat(packets), "aac", nil -- already ADTS (MPEG-TS)
end
local b, err = adts(track, packets)
return b, if b then "aac" else nil, err
end
+10 -1
View File
@@ -144,7 +144,16 @@ function C.audio(t: any, v: any, c: any, label: string, exact: boolean?, minSnr:
local a = v.audio
a:seek(0)
local want = #c.pcm // 4
local out, got = a:read(want // a.channels)
local out = buffer.create(want * 4)
local got = 0
while got * a.channels < want do
local chunk, k = a:read(want // a.channels - got)
if not chunk or k <= 0 then
break
end
buffer.copy(out, got * a.channels * 4, chunk, 0, k * a.channels * 4)
got += k
end
local n = got * a.channels
t:check(n == want or n * 4 == c.pcmBytes, label .. ": " .. n .. " samples read of " .. want)
n = math.min(n, want)