Rewrite MP4/MOV binaries to be faststart
[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, _::binary>>) do
17 index = fix(binary, binary, 0, [])
18
19 case index do
20 [{"ftyp", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
21 [{"ftyp", _, _, _, _}, {"free", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
22 _ -> binary
23 end
24 end
25
26 def fix(binary) do
27 binary
28 end
29
30 defp fix(<<>>, _bin, _pos, acc) do
31 :lists.reverse(acc)
32 end
33
34 defp fix(
35 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), rest::binary>>,
36 bin,
37 pos,
38 acc
39 ) do
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.
42 acc = [
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>>}
45 | acc
46 ]
47
48 fix(<<>>, bin, byte_size(bin), acc)
49 else
50 full_size = size - 8
51 <<data::binary-size(full_size), rest::binary>> = rest
52
53 acc = [
54 {fourcc, pos, pos + size, size,
55 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), data::binary>>}
56 | acc
57 ]
58
59 fix(rest, bin, pos + size, acc)
60 end
61 end
62
63 defp faststart(index) do
64 {{_ftyp, _, _, _, ftyp}, index} = List.keytake(index, "ftyp", 0)
65
66 # Skip re-writing the free fourcc as it's kind of useless. Why stream useless bytes when you can do without?
67 {free_size, index} =
68 case List.keytake(index, "free", 0) do
69 {{_, _, _, size, _}, index} -> {size, index}
70 _ -> {0, index}
71 end
72
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>>
79 end
80
81 defp fix_moov(moov, offset) do
82 fix_moov(moov, offset, <<>>)
83 end
84
85 defp fix_moov(<<>>, _, acc), do: acc
86
87 defp fix_moov(
88 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), rest::binary>>,
89 offset,
90 acc
91 ) do
92 full_size = size - 8
93 <<data::binary-size(full_size), rest::binary>> = rest
94
95 data =
96 cond do
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>>
101
102 fourcc in ["stco", "co64"] ->
103 # fix the damn thing
104 <<version::integer-big-size(4)-unit(8), count::integer-big-size(4)-unit(8),
105 rest::binary>> = data
106
107 entry_size =
108 case fourcc do
109 "stco" -> 4
110 "co64" -> 8
111 end
112
113 {_, result} =
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)>>}
118 end)
119
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),
122 result::binary>>
123
124 true ->
125 <<size::integer-big-size(4)-unit(8), fourcc::binary-size(4), data::binary>>
126 end
127
128 acc = <<acc::binary, data::binary>>
129 fix_moov(rest, offset, acc)
130 end
131 end