8cba06e544fc8366ed63d76e9ab289f83aff690f
[akkoma] / lib / pleroma / helpers / qt_fast_start.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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(binary = <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::bits>>) 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. Why stream useless bytes when you can do without?
63 {free_size, index} =
64 case List.keytake(index, "free", 0) do
65 {{_, _, _, size, _}, index} -> {size, index}
66 _ -> {0, index}
67 end
68
69 {{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
70 offset = -free_size + moov_size
71 rest = for {_, _, _, _, data} <- index, do: data, into: []
72 <<moov_head::bits-size(64), moov_data::bits>> = moov
73 [ftyp, moov_head, fix_moov(moov_data, offset, []), rest]
74 end
75
76 defp fix_moov(
77 <<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
78 offset,
79 acc
80 ) do
81 full_size = (size - 8) * 8
82 <<data::bits-size(full_size), rest::bits>> = rest
83
84 data =
85 cond do
86 fourcc in ["trak", "mdia", "minf", "stbl"] ->
87 # Theses contains sto or co64 part
88 [<<size::integer-big-size(32), fourcc::bits-size(32)>>, fix_moov(data, offset, [])]
89
90 fourcc in ["stco", "co64"] ->
91 # fix the damn thing
92 <<version::integer-big-size(32), count::integer-big-size(32), rest::bits>> = data
93
94 entry_size =
95 case fourcc do
96 "stco" -> 32
97 "co64" -> 64
98 end
99
100 [
101 <<size::integer-big-size(32), fourcc::bits-size(32), version::integer-big-size(32),
102 count::integer-big-size(32)>>,
103 rewrite_entries(entry_size, offset, rest, [])
104 ]
105
106 true ->
107 [<<size::integer-big-size(32), fourcc::bits-size(32)>>, data]
108 end
109
110 acc = [acc | data]
111 fix_moov(rest, offset, acc)
112 end
113
114 defp fix_moov(<<>>, _, acc), do: acc
115
116 for size <- [32, 64] do
117 defp rewrite_entries(
118 unquote(size),
119 offset,
120 <<pos::integer-big-size(unquote(size)), rest::bits>>,
121 acc
122 ) do
123 rewrite_entries(unquote(size), offset, rest, [
124 acc | <<pos + offset::integer-big-size(unquote(size))>>
125 ])
126 end
127 end
128
129 defp rewrite_entries(_, _, <<>>, acc), do: acc
130 end