logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

qt_fast_start.ex (4064B)


  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. 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. try do
  37. full_size = (size - 8) * 8
  38. <<data::bits-size(full_size), rest::bits>> = rest
  39. acc = [
  40. {fourcc, pos, pos + size, size,
  41. <<size::integer-big-size(32), fourcc::bits-size(32), data::bits>>}
  42. | acc
  43. ]
  44. fix(rest, pos + size, got_moov || fourcc == "moov", got_mdat || fourcc == "mdat", acc)
  45. rescue
  46. _ ->
  47. :abort
  48. end
  49. end
  50. defp fix(<<>>, _pos, _, _, acc) do
  51. :lists.reverse(acc)
  52. end
  53. defp faststart(index) do
  54. {{_ftyp, _, _, _, ftyp}, index} = List.keytake(index, "ftyp", 0)
  55. # Skip re-writing the free fourcc as it's kind of useless.
  56. # Why stream useless bytes when you can do without?
  57. {free_size, index} =
  58. case List.keytake(index, "free", 0) do
  59. {{_, _, _, size, _}, index} -> {size, index}
  60. _ -> {0, index}
  61. end
  62. {{_moov, _, _, moov_size, moov}, index} = List.keytake(index, "moov", 0)
  63. offset = -free_size + moov_size
  64. rest = for {_, _, _, _, data} <- index, do: data, into: []
  65. <<moov_head::bits-size(64), moov_data::bits>> = moov
  66. [ftyp, moov_head, fix_moov(moov_data, offset, []), rest]
  67. end
  68. defp fix_moov(
  69. <<size::integer-big-size(32), fourcc::bits-size(32), rest::bits>>,
  70. offset,
  71. acc
  72. ) do
  73. full_size = (size - 8) * 8
  74. <<data::bits-size(full_size), rest::bits>> = rest
  75. data =
  76. cond do
  77. fourcc in ["trak", "mdia", "minf", "stbl"] ->
  78. # Theses contains sto or co64 part
  79. [<<size::integer-big-size(32), fourcc::bits-size(32)>>, fix_moov(data, offset, [])]
  80. fourcc in ["stco", "co64"] ->
  81. # fix the damn thing
  82. <<version::integer-big-size(32), count::integer-big-size(32), rest::bits>> = data
  83. entry_size =
  84. case fourcc do
  85. "stco" -> 32
  86. "co64" -> 64
  87. end
  88. [
  89. <<size::integer-big-size(32), fourcc::bits-size(32), version::integer-big-size(32),
  90. count::integer-big-size(32)>>,
  91. rewrite_entries(entry_size, offset, rest, [])
  92. ]
  93. true ->
  94. [<<size::integer-big-size(32), fourcc::bits-size(32)>>, data]
  95. end
  96. acc = [acc | data]
  97. fix_moov(rest, offset, acc)
  98. end
  99. defp fix_moov(<<>>, _, acc), do: acc
  100. for size <- [32, 64] do
  101. defp rewrite_entries(
  102. unquote(size),
  103. offset,
  104. <<pos::integer-big-size(unquote(size)), rest::bits>>,
  105. acc
  106. ) do
  107. rewrite_entries(
  108. unquote(size),
  109. offset,
  110. rest,
  111. acc ++
  112. [
  113. <<pos + offset::integer-big-size(unquote(size))>>
  114. ]
  115. )
  116. end
  117. end
  118. defp rewrite_entries(_, _, <<>>, acc), do: acc
  119. end