1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Helpers.QtFastStart do
7 (WIP) Converts a "slow start" (data before metadatas) mov/mp4 file to a "fast start" one (metadatas before data).
10 # TODO: Cleanup and optimizations
11 # Inspirations: https://www.ffmpeg.org/doxygen/3.4/qt-faststart_8c_source.html
12 # https://github.com/danielgtaylor/qtfaststart/blob/master/qtfaststart/processor.py
13 # ISO/IEC 14496-12:2015, ISO/IEC 15444-12:2015
16 def fix(binary = <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::binary>>) do
17 index = fix(binary, binary, 0, [])
20 [{"ftyp", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
21 [{"ftyp", _, _, _, _}, {"free", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
30 defp fix(<<>>, _bin, _pos, acc) do
35 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), rest::binary>>,
40 if fourcc == "mdat" && size == 0 do
41 # mdat with 0 size means "seek to the end" -- also, in that case the file is probably OK.
43 {fourcc, pos, byte_size(bin) - pos, byte_size(bin) - pos,
44 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), rest::binary>>}
48 fix(<<>>, bin, byte_size(bin), acc)
51 <<data::binary-size(full_size), rest::binary>> = rest
54 {fourcc, pos, pos + size, size,
55 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), data::binary>>}
59 fix(rest, bin, pos + size, acc)
63 defp faststart(index) do
64 {{_ftyp, _, _, _, ftyp}, index} = List.keytake(index, "ftyp", 0)
66 # Skip re-writing the free fourcc as it's kind of useless. Why stream useless bytes when you can do without?
68 case List.keytake(index, "free", 0) do
69 {{_, _, _, size, _}, index} -> {size, index}
73 {{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
74 offset = -free_size + moov_size
75 rest = for {_, _, _, _, data} <- index, do: data, into: <<>>
76 <<moov_head::binary-size(8), moov_data::binary>> = moov
77 new_moov = fix_moov(moov_data, offset)
78 <<ftyp::binary, moov_head::binary, new_moov::binary, rest::binary>>
81 defp fix_moov(moov, offset) do
82 fix_moov(moov, offset, <<>>)
85 defp fix_moov(<<>>, _, acc), do: acc
88 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), rest::binary>>,
93 <<data::binary-size(full_size), rest::binary>> = rest
97 fourcc in ["trak", "mdia", "minf", "stbl"] ->
98 # Theses contains sto or co64 part
99 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4),
100 fix_moov(data, offset, <<>>)::binary>>
102 fourcc in ["stco", "co64"] ->
104 <<version::integer-big-size(4)-unit(8), count::integer-big-size(4)-unit(8),
105 rest::binary>> = data
114 Enum.reduce(1..count, {rest, <<>>}, fn _,
115 {<<pos::integer-big-size(entry_size)-unit(8),
116 rest::binary>>, acc} ->
117 {rest, <<acc::binary, pos + offset::integer-big-size(entry_size)-unit(8)>>}
120 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4),
121 version::integer-big-size(4)-unit(8), count::integer-big-size(4)-unit(8),
125 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), data::binary>>
128 acc = <<acc::binary, data::binary>>
129 fix_moov(rest, offset, acc)