perf: F.detect: the extension's format scoring 1 returns at once (65x with a name), archive/br's trial-decode sniff runs last and only when it could still win; mpegts sniff finds sync bytes with string.find; inflate reuses its dynamic-table scratch buffers
This commit is contained in:
1 parent
11b7f89dab
commit
3320339507
3 files changed
+70
-25
No files matched your search
@@ -47,27 +47,35 @@ local CODEC: { [string]: string } = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
-- 188 or 192 (M2TS): three sync bytes a packet apart (fewer in a short file) within the first 2 KB
|
||||
local SIZES = { 188, 192 }
|
||||
local function packetSize(r: Bytes.Reader): (number, number)
|
||||
local n = r:remaining()
|
||||
for start = 0, math.min(n, 2048) - 1 do
|
||||
if r:peekU8(start) == 0x47 then
|
||||
for _, size in { 188, 192 } do
|
||||
local hits, need = 0, math.min(3, (n - start - 1) // size + 1)
|
||||
for k = 0, 2 do
|
||||
local at = start + k * size
|
||||
if at < n and r:peekU8(at) == 0x47 then
|
||||
hits += 1
|
||||
end
|
||||
local head = r:peek(2048 + 2 * 192)
|
||||
local lim = math.min(n, 2048)
|
||||
local start = 0
|
||||
while start < lim do
|
||||
local f = string.find(head, "G", start + 1, true) -- 0x47
|
||||
if not f or f > lim then
|
||||
break
|
||||
end
|
||||
start = f - 1
|
||||
for _, size in SIZES do
|
||||
local hits, need = 0, math.min(3, (n - start - 1) // size + 1)
|
||||
for k = 0, 2 do
|
||||
local at = start + k * size
|
||||
if at < n and string.byte(head, at + 1) == 0x47 then
|
||||
hits += 1
|
||||
end
|
||||
if hits >= need and (need >= 2 or start == 0) then
|
||||
if size == 192 and start >= 4 then
|
||||
return 192, start - 4
|
||||
elseif size == 188 then
|
||||
return 188, start
|
||||
end
|
||||
end
|
||||
if hits >= need and (need >= 2 or start == 0) then
|
||||
if size == 192 and start >= 4 then
|
||||
return 192, start - 4
|
||||
elseif size == 188 then
|
||||
return 188, start
|
||||
end
|
||||
end
|
||||
end
|
||||
start += 1
|
||||
end
|
||||
return 0, 0
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user