move to 2.5.5
[anni] / lib / pleroma / helpers / qt_fast_start.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Helpers.QtFastStart do
6   @moduledoc """
7   (WIP) Converts a "slow start" (data before metadatas) mov/mp4 file to a "fast start" one (metadatas before data).
8   """
9
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
14   #               Paracetamol
15
16   def fix(<<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::bits>> = binary) do
17     index = fix(binary, 0, nil, nil, [])
18
19     case index do
20       :abort -> binary
21       [{"ftyp", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
22       [{"ftyp", _, _, _, _}, {"free", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
23       _ -> binary
24     end
25   end
26
27   def fix(binary) do
28     binary
29   end
30
31   # MOOV have been seen before MDAT- abort
32   defp fix(<<_::bits>>, _, true, false, _) do
33     :abort
34   end
35
36   defp fix(
37          <<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
38          pos,
39          got_moov,
40          got_mdat,
41          acc
42        ) do
43     full_size = (size - 8) * 8
44     <<data::bits-size(full_size), rest::bits>> = rest
45
46     acc = [
47       {fourcc, pos, pos + size, size,
48        <<size::integer-big-size(32), fourcc::bits-size(32), data::bits>>}
49       | acc
50     ]
51
52     fix(rest, pos + size, got_moov || fourcc == "moov", got_mdat || fourcc == "mdat", acc)
53   end
54
55   defp fix(<<>>, _pos, _, _, acc) do
56     :lists.reverse(acc)
57   end
58
59   defp faststart(index) do
60     {{_ftyp, _, _, _, ftyp}, index} = List.keytake(index, "ftyp", 0)
61
62     # Skip re-writing the free fourcc as it's kind of useless.
63     # Why stream useless bytes when you can do without?
64     {free_size, index} =
65       case List.keytake(index, "free", 0) do
66         {{_, _, _, size, _}, index} -> {size, index}
67         _ -> {0, index}
68       end
69
70     {{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
71     offset = -free_size + moov_size
72     rest = for {_, _, _, _, data} <- index, do: data, into: []
73     <<moov_head::bits-size(64), moov_data::bits>> = moov
74     [ftyp, moov_head, fix_moov(moov_data, offset, []), rest]
75   end
76
77   defp fix_moov(
78          <<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
79          offset,
80          acc
81        ) do
82     full_size = (size - 8) * 8
83     <<data::bits-size(full_size), rest::bits>> = rest
84
85     data =
86       cond do
87         fourcc in ["trak", "mdia", "minf", "stbl"] ->
88           # Theses contains sto or co64 part
89           [<<size::integer-big-size(32), fourcc::bits-size(32)>>, fix_moov(data, offset, [])]
90
91         fourcc in ["stco", "co64"] ->
92           # fix the damn thing
93           <<version::integer-big-size(32), count::integer-big-size(32), rest::bits>> = data
94
95           entry_size =
96             case fourcc do
97               "stco" -> 32
98               "co64" -> 64
99             end
100
101           [
102             <<size::integer-big-size(32), fourcc::bits-size(32), version::integer-big-size(32),
103               count::integer-big-size(32)>>,
104             rewrite_entries(entry_size, offset, rest, [])
105           ]
106
107         true ->
108           [<<size::integer-big-size(32), fourcc::bits-size(32)>>, data]
109       end
110
111     acc = [acc | data]
112     fix_moov(rest, offset, acc)
113   end
114
115   defp fix_moov(<<>>, _, acc), do: acc
116
117   for size <- [32, 64] do
118     defp rewrite_entries(
119            unquote(size),
120            offset,
121            <<pos::integer-big-size(unquote(size)), rest::bits>>,
122            acc
123          ) do
124       rewrite_entries(unquote(size), offset, rest, [
125         acc | <<pos + offset::integer-big-size(unquote(size))>>
126       ])
127     end
128   end
129
130   defp rewrite_entries(_, _, <<>>, acc), do: acc
131 end