X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fhelpers%2Fqt_fast_start.ex;h=c4d11b9dd49e1ac10bf3c8742b3c3b3a6fc3d2fb;hb=5624366056b026b17439756a3057676308e7f7d9;hp=694b583b988c47ab7c2eaf8bb3e03bf3c97cfa4a;hpb=dfceb03cf47374fdeab60784476b2e266208a4bb;p=akkoma
diff --git a/lib/pleroma/helpers/qt_fast_start.ex b/lib/pleroma/helpers/qt_fast_start.ex
index 694b583b9..c4d11b9dd 100644
--- a/lib/pleroma/helpers/qt_fast_start.ex
+++ b/lib/pleroma/helpers/qt_fast_start.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Helpers.QtFastStart do
@@ -13,10 +13,11 @@ defmodule Pleroma.Helpers.QtFastStart do
# ISO/IEC 14496-12:2015, ISO/IEC 15444-12:2015
# Paracetamol
- def fix(binary = <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::binary>>) do
- index = fix(binary, binary, 0, [])
+ def fix(<<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::bits>> = binary) do
+ index = fix(binary, 0, nil, nil, [])
case index do
+ :abort -> binary
[{"ftyp", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
[{"ftyp", _, _, _, _}, {"free", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
_ -> binary
@@ -27,43 +28,39 @@ defmodule Pleroma.Helpers.QtFastStart do
binary
end
- defp fix(<<>>, _bin, _pos, acc) do
- :lists.reverse(acc)
+ # MOOV have been seen before MDAT- abort
+ defp fix(<<_::bits>>, _, true, false, _) do
+ :abort
end
defp fix(
- <>,
- bin,
+ <>,
pos,
+ got_moov,
+ got_mdat,
acc
) do
- if fourcc == "mdat" && size == 0 do
- # mdat with 0 size means "seek to the end" -- also, in that case the file is probably OK.
- acc = [
- {fourcc, pos, byte_size(bin) - pos, byte_size(bin) - pos,
- <>}
- | acc
- ]
-
- fix(<<>>, bin, byte_size(bin), acc)
- else
- full_size = size - 8
- <> = rest
-
- acc = [
- {fourcc, pos, pos + size, size,
- <>}
- | acc
- ]
-
- fix(rest, bin, pos + size, acc)
- end
+ full_size = (size - 8) * 8
+ <> = rest
+
+ acc = [
+ {fourcc, pos, pos + size, size,
+ <>}
+ | acc
+ ]
+
+ fix(rest, pos + size, got_moov || fourcc == "moov", got_mdat || fourcc == "mdat", acc)
+ end
+
+ defp fix(<<>>, _pos, _, _, acc) do
+ :lists.reverse(acc)
end
defp faststart(index) do
{{_ftyp, _, _, _, ftyp}, index} = List.keytake(index, "ftyp", 0)
- # Skip re-writing the free fourcc as it's kind of useless. Why stream useless bytes when you can do without?
+ # Skip re-writing the free fourcc as it's kind of useless.
+ # Why stream useless bytes when you can do without?
{free_size, index} =
case List.keytake(index, "free", 0) do
{{_, _, _, size, _}, index} -> {size, index}
@@ -72,60 +69,63 @@ defmodule Pleroma.Helpers.QtFastStart do
{{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
offset = -free_size + moov_size
- rest = for {_, _, _, _, data} <- index, do: data, into: <<>>
- <> = moov
- new_moov = fix_moov(moov_data, offset)
- <>
- end
-
- defp fix_moov(moov, offset) do
- fix_moov(moov, offset, <<>>)
+ rest = for {_, _, _, _, data} <- index, do: data, into: []
+ <> = moov
+ [ftyp, moov_head, fix_moov(moov_data, offset, []), rest]
end
- defp fix_moov(<<>>, _, acc), do: acc
-
defp fix_moov(
- <>,
+ <>,
offset,
acc
) do
- full_size = size - 8
- <> = rest
+ full_size = (size - 8) * 8
+ <> = rest
data =
cond do
fourcc in ["trak", "mdia", "minf", "stbl"] ->
# Theses contains sto or co64 part
- <>)::binary>>
+ [<>, fix_moov(data, offset, [])]
fourcc in ["stco", "co64"] ->
# fix the damn thing
- <> = data
+ <> = data
entry_size =
case fourcc do
- "stco" -> 4
- "co64" -> 8
+ "stco" -> 32
+ "co64" -> 64
end
- {_, result} =
- Enum.reduce(1..count, {rest, <<>>}, fn _,
- {<>, acc} ->
- {rest, <>}
- end)
-
- <>
+ [
+ <>,
+ rewrite_entries(entry_size, offset, rest, [])
+ ]
true ->
- <>
+ [<>, data]
end
- acc = <>
+ acc = [acc | data]
fix_moov(rest, offset, acc)
end
+
+ defp fix_moov(<<>>, _, acc), do: acc
+
+ for size <- [32, 64] do
+ defp rewrite_entries(
+ unquote(size),
+ offset,
+ <>,
+ acc
+ ) do
+ rewrite_entries(unquote(size), offset, rest, [
+ acc | <>
+ ])
+ end
+ end
+
+ defp rewrite_entries(_, _, <<>>, acc), do: acc
end