QtFastStart: optimize

~4-6x faster
~3~4x memory usage reduction (now mostly adds what we are rewriting in
the metadatas)
This commit is contained in:
href 2020-08-29 13:05:23 +02:00
parent dfceb03cf4
commit 24d522c3b3

View file

@ -13,10 +13,11 @@ defmodule Pleroma.Helpers.QtFastStart do
# ISO/IEC 14496-12:2015, ISO/IEC 15444-12:2015 # ISO/IEC 14496-12:2015, ISO/IEC 15444-12:2015
# Paracetamol # Paracetamol
def fix(binary = <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::binary>>) do def fix(binary = <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::bits>>) do
index = fix(binary, binary, 0, []) index = fix(binary, 0, nil, nil, [])
case index do case index do
:abort -> binary
[{"ftyp", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index) [{"ftyp", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
[{"ftyp", _, _, _, _}, {"free", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index) [{"ftyp", _, _, _, _}, {"free", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
_ -> binary _ -> binary
@ -27,37 +28,32 @@ def fix(binary) do
binary binary
end end
defp fix(<<>>, _bin, _pos, acc) do # MOOV have been seen before MDAT- abort
:lists.reverse(acc) defp fix(<<_::bits>>, _, true, false, _) do
:abort
end end
defp fix( defp fix(
<<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), rest::binary>>, <<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
bin,
pos, pos,
got_moov,
got_mdat,
acc acc
) do ) do
if fourcc == "mdat" && size == 0 do full_size = (size - 8) * 8
# mdat with 0 size means "seek to the end" -- also, in that case the file is probably OK. <<data::bits-size(full_size), rest::bits>> = rest
acc = [
{fourcc, pos, byte_size(bin) - pos, byte_size(bin) - pos,
<<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), rest::binary>>}
| acc
]
fix(<<>>, bin, byte_size(bin), acc)
else
full_size = size - 8
<<data::binary-size(full_size), rest::binary>> = rest
acc = [ acc = [
{fourcc, pos, pos + size, size, {fourcc, pos, pos + size, size,
<<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), data::binary>>} <<size::integer-big-size(32), fourcc::bits-size(32), data::bits>>}
| acc | acc
] ]
fix(rest, bin, pos + size, acc) fix(rest, pos + size, got_moov || fourcc == "moov", got_mdat || fourcc == "mdat", acc)
end end
defp fix(<<>>, _pos, _, _, acc) do
:lists.reverse(acc)
end end
defp faststart(index) do defp faststart(index) do
@ -72,60 +68,63 @@ defp faststart(index) do
{{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0) {{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
offset = -free_size + moov_size offset = -free_size + moov_size
rest = for {_, _, _, _, data} <- index, do: data, into: <<>> rest = for {_, _, _, _, data} <- index, do: data, into: []
<<moov_head::binary-size(8), moov_data::binary>> = moov <<moov_head::bits-size(64), moov_data::bits>> = moov
new_moov = fix_moov(moov_data, offset) [ftyp, moov_head, fix_moov(moov_data, offset, []), rest]
<<ftyp::binary, moov_head::binary, new_moov::binary, rest::binary>>
end end
defp fix_moov(moov, offset) do
fix_moov(moov, offset, <<>>)
end
defp fix_moov(<<>>, _, acc), do: acc
defp fix_moov( defp fix_moov(
<<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), rest::binary>>, <<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
offset, offset,
acc acc
) do ) do
full_size = size - 8 full_size = (size - 8) * 8
<<data::binary-size(full_size), rest::binary>> = rest <<data::bits-size(full_size), rest::bits>> = rest
data = data =
cond do cond do
fourcc in ["trak", "mdia", "minf", "stbl"] -> fourcc in ["trak", "mdia", "minf", "stbl"] ->
# Theses contains sto or co64 part # Theses contains sto or co64 part
<<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), [<<size::integer-big-size(32), fourcc::bits-size(32)>>, fix_moov(data, offset, [])]
fix_moov(data, offset, <<>>)::binary>>
fourcc in ["stco", "co64"] -> fourcc in ["stco", "co64"] ->
# fix the damn thing # fix the damn thing
<<version::integer-big-size(4)-unit(8), count::integer-big-size(4)-unit(8), <<version::integer-big-size(32), count::integer-big-size(32), rest::bits>> = data
rest::binary>> = data
entry_size = entry_size =
case fourcc do case fourcc do
"stco" -> 4 "stco" -> 32
"co64" -> 8 "co64" -> 64
end end
{_, result} = [
Enum.reduce(1..count, {rest, <<>>}, fn _, <<size::integer-big-size(32), fourcc::bits-size(32), version::integer-big-size(32),
{<<pos::integer-big-size(entry_size)-unit(8), count::integer-big-size(32)>>,
rest::binary>>, acc} -> rewrite_entries(entry_size, offset, rest, [])
{rest, <<acc::binary, pos + offset::integer-big-size(entry_size)-unit(8)>>} ]
end)
<<size::integer-big-size(4)-unit(8), fourcc::binary-size(4),
version::integer-big-size(4)-unit(8), count::integer-big-size(4)-unit(8),
result::binary>>
true -> true ->
<<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), data::binary>> [<<size::integer-big-size(32), fourcc::bits-size(32)>>, data]
end end
acc = <<acc::binary, data::binary>> acc = [acc | data]
fix_moov(rest, offset, acc) fix_moov(rest, offset, acc)
end end
defp fix_moov(<<>>, _, acc), do: acc
for size <- [32, 64] do
defp rewrite_entries(
unquote(size),
offset,
<<pos::integer-big-size(unquote(size)), rest::bits>>,
acc
) do
rewrite_entries(unquote(size), offset, rest, [
acc | <<pos + offset::integer-big-size(unquote(size))>>
])
end
end
defp rewrite_entries(_, _, <<>>, acc), do: acc
end end