Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85950754
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/pleroma/helpers/qt_fast_start.ex b/lib/pleroma/helpers/qt_fast_start.ex
index fa472613d..21e6f3888 100644
--- a/lib/pleroma/helpers/qt_fast_start.ex
+++ b/lib/pleroma/helpers/qt_fast_start.ex
@@ -1,144 +1,157 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Helpers.QtFastStart do
@moduledoc """
Converts a "slow start" (data before metadata) MP4/MOV file to a "fast start" one
(metadata before data) by relocating the moov atom before the mdat atom and updating
chunk offset references in stco/co64 atoms accordingly.
"""
+ require Logger
+
def fix(<<size::integer-big-size(32), 0x66, 0x74, 0x79, 0x70, _::bits>> = binary)
when size >= 8 do
index = fix(binary, 0, nil, nil, [])
case index do
:abort ->
binary
[{"ftyp", _, _, _, _}, {"mdat", _, _, _, _} | _] ->
- faststart(index)
+ faststart(index, binary)
[{"ftyp", _, _, _, _}, {"free", _, _, _, _}, {"mdat", _, _, _, _} | _] ->
- faststart(index)
+ faststart(index, binary)
_ ->
binary
end
end
def fix(binary), do: binary
# MOOV have been seen before MDAT- abort
defp fix(<<_::bits>>, _, true, false, _) do
:abort
end
defp fix(
<<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
pos,
got_moov,
got_mdat,
acc
) do
try do
full_size = (size - 8) * 8
<<data::bits-size(full_size), rest::bits>> = rest
acc = [
{fourcc, pos, pos + size, size,
<<size::integer-big-size(32), fourcc::bits-size(32), data::bits>>}
| acc
]
fix(rest, pos + size, got_moov || fourcc == "moov", got_mdat || fourcc == "mdat", acc)
rescue
_ ->
:abort
end
end
defp fix(<<>>, _pos, _, _, acc) do
:lists.reverse(acc)
end
- defp faststart(index) do
+ # The original binary when it cannot be rewritten (no moov, a broken
+ # atom, an offset that would not fit).
+ defp faststart(index, binary) do
{{_ftyp, _, _, _, ftyp}, index} = List.keytake(index, "ftyp", 0)
- # Skip re-writing the free fourcc as it's kind of useless.
+ # Skip re-writing a free atom right after ftyp 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}
+ case index do
+ [{"free", _, _, size, _} | index] -> {size, index}
_ -> {0, index}
end
- {{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
- offset = -free_size + moov_size
+ {{_moov, moov_start, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
+
+ # Data before the moov moves back by its size, data after it stays put;
+ # all of it moves forward by the dropped free atom.
+ shift = fn pos -> if(pos < moov_start, do: moov_size, else: 0) - free_size end
+
rest = Enum.map(index, fn {_, _, _, _, data} -> data end)
<<moov_head::bits-size(64), moov_data::bits>> = moov
- IO.iodata_to_binary([ftyp, moov_head, fix_moov(moov_data, offset, []), rest])
+ IO.iodata_to_binary([ftyp, moov_head, fix_moov(moov_data, shift, []), rest])
+ rescue
+ e ->
+ Logger.warning("#{__MODULE__}: leaving the file as it is: #{inspect(e)}")
+ binary
end
defp fix_moov(
<<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
- offset,
+ shift,
acc
) do
full_size = (size - 8) * 8
<<data::bits-size(full_size), rest::bits>> = rest
data =
cond do
fourcc in ["trak", "mdia", "minf", "stbl"] ->
- # Theses contains sto or co64 part
- [<<size::integer-big-size(32), fourcc::bits-size(32)>>, fix_moov(data, offset, [])]
+ # These contain the stco or co64 part
+ [<<size::integer-big-size(32), fourcc::bits-size(32)>>, fix_moov(data, shift, [])]
fourcc in ["stco", "co64"] ->
# fix the damn thing
<<version::integer-big-size(32), count::integer-big-size(32), rest::bits>> = data
entry_size =
case fourcc do
"stco" -> 32
"co64" -> 64
end
[
<<size::integer-big-size(32), fourcc::bits-size(32), version::integer-big-size(32),
count::integer-big-size(32)>>,
- rewrite_entries(entry_size, offset, rest, [])
+ rewrite_entries(entry_size, shift, rest, [])
]
true ->
[<<size::integer-big-size(32), fourcc::bits-size(32)>>, data]
end
acc = [acc | data]
- fix_moov(rest, offset, acc)
+ fix_moov(rest, shift, acc)
end
defp fix_moov(<<>>, _, acc), do: acc
for size <- [32, 64] do
defp rewrite_entries(
unquote(size),
- offset,
+ shift,
<<pos::integer-big-size(unquote(size)), rest::bits>>,
acc
) do
- rewrite_entries(
- unquote(size),
- offset,
- rest,
- acc ++
- [
- <<pos + offset::integer-big-size(unquote(size))>>
- ]
- )
+ new_pos = pos + shift.(pos)
+
+ # an offset that does not fit (a file near 4 GiB with stco) would be cut
+ if new_pos >= unquote(Bitwise.bsl(1, size)) do
+ raise ArgumentError, "chunk offset out of range"
+ end
+
+ rewrite_entries(unquote(size), shift, rest, [
+ acc | <<new_pos::integer-big-size(unquote(size))>>
+ ])
end
end
defp rewrite_entries(_, _, <<>>, acc), do: acc
end
diff --git a/test/pleroma/helpers/qt_fast_start_test.exs b/test/pleroma/helpers/qt_fast_start_test.exs
new file mode 100644
index 000000000..3845a2585
--- /dev/null
+++ b/test/pleroma/helpers/qt_fast_start_test.exs
@@ -0,0 +1,120 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Helpers.QtFastStartTest do
+ use ExUnit.Case, async: true
+ alias Pleroma.Helpers.QtFastStart
+
+ # Synthetic files: every chunk is a 4-byte marker, and the moov holds an
+ # stco (or co64) table pointing at the markers. After the fix, the table
+ # must still point at the same markers, in the same order.
+
+ defp atom(type, payload), do: <<byte_size(payload) + 8::32, type::binary, payload::binary>>
+
+ defp mdat(markers), do: {:mdat, markers}
+
+ defp table(:stco, offsets), do: for(o <- offsets, into: <<>>, do: <<o::32>>)
+ defp table(:co64, offsets), do: for(o <- offsets, into: <<>>, do: <<o::64>>)
+
+ defp moov(type, offsets) do
+ chunks =
+ atom(Atom.to_string(type), <<0::32, length(offsets)::32, table(type, offsets)::binary>>)
+
+ atom("moov", atom("trak", atom("mdia", atom("minf", atom("stbl", chunks)))))
+ end
+
+ # The file for a layout of :ftyp, :free, :moov and mdat(markers) entries,
+ # and the markers in file order.
+ defp build(layout, type \\ :stco) do
+ ftyp = atom("ftyp", "isom" <> <<0::32>> <> "isommp41")
+ free = atom("free", <<0::64>>)
+ # the moov size does not depend on the offsets
+ moov_size =
+ byte_size(moov(type, Enum.flat_map(layout, &markers/1) |> Enum.map(fn _ -> 0 end)))
+
+ {parts, offsets, _} =
+ Enum.reduce(layout, {[], [], 0}, fn
+ :ftyp, {parts, offsets, pos} ->
+ {[ftyp | parts], offsets, pos + byte_size(ftyp)}
+
+ :free, {parts, offsets, pos} ->
+ {[free | parts], offsets, pos + byte_size(free)}
+
+ :moov, {parts, offsets, pos} ->
+ {[:moov | parts], offsets, pos + moov_size}
+
+ {:mdat, markers}, {parts, offsets, pos} ->
+ body = Enum.join(markers)
+ starts = for i <- 0..(length(markers) - 1), do: pos + 8 + i * 4
+ {[atom("mdat", body) | parts], offsets ++ starts, pos + 8 + byte_size(body)}
+ end)
+
+ moov = moov(type, offsets)
+
+ parts
+ |> Enum.reverse()
+ |> Enum.map(fn
+ :moov -> moov
+ part -> part
+ end)
+ |> IO.iodata_to_binary()
+ end
+
+ defp markers({:mdat, markers}), do: markers
+ defp markers(_), do: []
+
+ defp top_level(<<size::32, type::binary-size(4), rest::binary>>) do
+ <<_::binary-size(size - 8), rest::binary>> = rest
+ [type | top_level(rest)]
+ end
+
+ defp top_level(<<>>), do: []
+
+ # where the chunk table starts, and its entry width in bytes
+ defp chunk_table(file) do
+ case :binary.match(file, "stco") do
+ {pos, _} -> {pos + 8, 4}
+ :nomatch -> {elem(:binary.match(file, "co64"), 0) + 8, 8}
+ end
+ end
+
+ # the bytes each chunk offset points at
+ defp pointed(file) do
+ {start, width} = chunk_table(file)
+ <<_::binary-size(start), count::32, table::binary-size(count * width), _::binary>> = file
+ for <<offset::size(width * 8) <- table>>, do: binary_part(file, offset, 4)
+ end
+
+ test "moves moov in front of mdat and keeps the chunk offsets right" do
+ for layout <- [
+ [:ftyp, mdat(~w(AAAA BBBB)), :moov],
+ [:ftyp, :free, mdat(~w(AAAA BBBB)), :moov],
+ [:ftyp, mdat(~w(AAAA BBBB)), :free, :moov],
+ [:ftyp, mdat(~w(AAAA)), :moov, mdat(~w(BBBB CCCC))],
+ [:ftyp, mdat(~w(AAAA)), :moov, :free]
+ ],
+ type <- [:stco, :co64] do
+ file = build(layout, type)
+ fixed = QtFastStart.fix(file)
+
+ assert ["ftyp", "moov" | _] = top_level(fixed), inspect({layout, type})
+ assert pointed(fixed) == pointed(file), inspect({layout, type})
+ assert pointed(file) == Enum.flat_map(layout, &markers/1)
+ end
+ end
+
+ test "drops a free atom only when it sits right after ftyp" do
+ assert top_level(QtFastStart.fix(build([:ftyp, :free, mdat(~w(AAAA)), :moov]))) ==
+ ~w(ftyp moov mdat)
+
+ assert top_level(QtFastStart.fix(build([:ftyp, mdat(~w(AAAA)), :free, :moov]))) ==
+ ~w(ftyp moov mdat free)
+ end
+
+ test "leaves fast-start files and other data alone" do
+ fast = QtFastStart.fix(build([:ftyp, mdat(~w(AAAA)), :moov]))
+ assert QtFastStart.fix(fast) == fast
+ assert QtFastStart.fix("not a video") == "not a video"
+ end
+end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Nov 18, 10:58 PM (1 d, 22 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1840770
Default Alt Text
(10 KB)
Attached To
Mode
rPUBE pleroma-upstream
Attached
Detach File
Event Timeline