Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.8KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Helpers.QtFastStart do
  5. @moduledoc """
  6. (WIP) Converts a "slow start" (data before metadatas) mov/mp4 file to a "fast start" one (metadatas before data).
  7. """
  8. # TODO: Cleanup and optimizations
  9. # Inspirations: https://www.ffmpeg.org/doxygen/3.4/qt-faststart_8c_source.html
  10. # https://github.com/danielgtaylor/qtfaststart/blob/master/qtfaststart/processor.py
  11. # ISO/IEC 14496-12:2015, ISO/IEC 15444-12:2015
  12. # Paracetamol
  13. def fix(<<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70, _::bits>> = binary) do
  14. index = fix(binary, 0, nil, nil, [])
  15. case index do
  16. :abort -> binary
  17. [{"ftyp", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
  18. [{"ftyp", _, _, _, _}, {"free", _, _, _, _}, {"mdat", _, _, _, _} | _] -> faststart(index)
  19. _ -> binary
  20. end
  21. end
  22. def fix(binary) do
  23. binary
  24. end
  25. # MOOV have been seen before MDAT- abort
  26. defp fix(<<_::bits>>, _, true, false, _) do
  27. :abort
  28. end
  29. defp fix(
  30. <<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
  31. pos,
  32. got_moov,
  33. got_mdat,
  34. acc
  35. ) do
  36. full_size = (size - 8) * 8
  37. <<data::bits-size(full_size), rest::bits>> = rest
  38. acc = [
  39. {fourcc, pos, pos + size, size,
  40. <<size::integer-big-size(32), fourcc::bits-size(32), data::bits>>}
  41. | acc
  42. ]
  43. fix(rest, pos + size, got_moov || fourcc == "moov", got_mdat || fourcc == "mdat", acc)
  44. end
  45. defp fix(<<>>, _pos, _, _, acc) do
  46. :lists.reverse(acc)
  47. end
  48. defp faststart(index) do
  49. {{_ftyp, _, _, _, ftyp}, index} = List.keytake(index, "ftyp", 0)
  50. # Skip re-writing the free fourcc as it's kind of useless.
  51. # Why stream useless bytes when you can do without?
  52. {free_size, index} =
  53. case List.keytake(index, "free", 0) do
  54. {{_, _, _, size, _}, index} -> {size, index}
  55. _ -> {0, index}
  56. end
  57. {{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
  58. offset = -free_size + moov_size
  59. rest = for {_, _, _, _, data} <- index, do: data, into: []
  60. <<moov_head::bits-size(64), moov_data::bits>> = moov
  61. [ftyp, moov_head, fix_moov(moov_data, offset, []), rest]
  62. end
  63. defp fix_moov(
  64. <<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
  65. offset,
  66. acc
  67. ) do
  68. full_size = (size - 8) * 8
  69. <<data::bits-size(full_size), rest::bits>> = rest
  70. data =
  71. cond do
  72. fourcc in ["trak", "mdia", "minf", "stbl"] ->
  73. # Theses contains sto or co64 part
  74. [<<size::integer-big-size(32), fourcc::bits-size(32)>>, fix_moov(data, offset, [])]
  75. fourcc in ["stco", "co64"] ->
  76. # fix the damn thing
  77. <<version::integer-big-size(32), count::integer-big-size(32), rest::bits>> = data
  78. entry_size =
  79. case fourcc do
  80. "stco" -> 32
  81. "co64" -> 64
  82. end
  83. [
  84. <<size::integer-big-size(32), fourcc::bits-size(32), version::integer-big-size(32),
  85. count::integer-big-size(32)>>,
  86. rewrite_entries(entry_size, offset, rest, [])
  87. ]
  88. true ->
  89. [<<size::integer-big-size(32), fourcc::bits-size(32)>>, data]
  90. end
  91. acc = [acc | data]
  92. fix_moov(rest, offset, acc)
  93. end
  94. defp fix_moov(<<>>, _, acc), do: acc
  95. for size <- [32, 64] do
  96. defp rewrite_entries(
  97. unquote(size),
  98. offset,
  99. <<pos::integer-big-size(unquote(size)), rest::bits>>,
  100. acc
  101. ) do
  102. rewrite_entries(unquote(size), offset, rest, [
  103. acc | <<pos + offset::integer-big-size(unquote(size))>>
  104. ])
  105. end
  106. end
  107. defp rewrite_entries(_, _, <<>>, acc), do: acc
  108. end