VideoTrack.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "core/VideoTrack.h"
  2. #include "core/TempoMap.h"
  3. #include <cctype>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include "core/Song.h"
  7. namespace lynx::videotrack
  8. {
  9. namespace
  10. {
  11. void sortClips (VideoTrack& track)
  12. {
  13. std::stable_sort (track.clips.begin(), track.clips.end(), [] (const VideoClip& a, const VideoClip& b) { return a.start < b.start; });
  14. }
  15. // Cuts the other clips where `keep` covers them (a clip fully under it goes)
  16. void clearUnder (VideoTrack& track, std::size_t keep)
  17. {
  18. const auto k = track.clips[keep];
  19. std::vector<VideoClip> out;
  20. for (std::size_t i = 0; i < track.clips.size(); ++i)
  21. {
  22. auto c = track.clips[i];
  23. if (i == keep || c.end() <= k.start + 1e-9 || c.start >= k.end() - 1e-9)
  24. {
  25. out.push_back (c);
  26. continue;
  27. }
  28. if (c.start < k.start)
  29. {
  30. // the part before `keep` stays (and a part after it, when `keep` sits inside)
  31. auto head = c;
  32. head.length = k.start - c.start;
  33. out.push_back (head);
  34. if (c.end() > k.end())
  35. {
  36. auto tail = c;
  37. tail.offset += k.end() - c.start;
  38. tail.start = k.end();
  39. tail.length = c.end() - k.end();
  40. tail.uid = c.uid ^ 0x9e3779b97f4a7c15ull; // (a new id for the new piece)
  41. out.push_back (tail);
  42. }
  43. }
  44. else if (c.end() > k.end())
  45. {
  46. c.offset += k.end() - c.start;
  47. c.length = c.end() - k.end();
  48. c.start = k.end();
  49. out.push_back (c);
  50. }
  51. // (else entirely covered: removed)
  52. }
  53. track.clips = std::move (out);
  54. sortClips (track);
  55. }
  56. int indexOfUid (const VideoTrack& track, std::uint64_t uid, double start)
  57. {
  58. for (std::size_t i = 0; i < track.clips.size(); ++i)
  59. if (track.clips[i].uid == uid && std::abs (track.clips[i].start - start) < 1e-9)
  60. return (int) i;
  61. return -1;
  62. }
  63. } // namespace
  64. std::optional<Showing> showing (const VideoTrack& track, double songSeconds) noexcept
  65. {
  66. if (track.muted)
  67. return std::nullopt;
  68. // the last clip starting at or before t (clips are sorted and do not overlap)
  69. const auto it = std::upper_bound (track.clips.begin(), track.clips.end(), songSeconds,
  70. [] (double t, const VideoClip& c) { return t < c.start; });
  71. if (it == track.clips.begin())
  72. return std::nullopt;
  73. const auto& c = *(it - 1);
  74. if (songSeconds >= c.end())
  75. return std::nullopt;
  76. return Showing { (int) (it - 1 - track.clips.begin()), c.offset + (songSeconds - c.start) };
  77. }
  78. double ticksForSeconds (const Song& song, double seconds)
  79. {
  80. if (seconds <= 0.0)
  81. return 0.0;
  82. const auto hasTempo = song.mode == PlayMode::song
  83. && std::any_of (song.automations.begin(), song.automations.end(),
  84. [] (const Automation& a) { return a.target.kind == AutomationTargetKind::tempo; });
  85. if (! hasTempo)
  86. return seconds * std::max (1.0, song.bpm) * ppq / 60.0;
  87. // the same 1/16 steps as Song::secondsForTicks (round 190ry: over the tempo clips only, the same values)
  88. constexpr std::int64_t step = ticksPerStep;
  89. const auto clips = tempomap::tempoClips (song);
  90. double at = 0.0;
  91. for (std::int64_t t = 0;; t += step)
  92. {
  93. const auto perTick = 60.0 / (std::max (1.0, tempomap::tempoAtIn (song, clips, (double) t)) * ppq);
  94. const auto span = perTick * (double) step;
  95. if (at + span >= seconds || t > (std::int64_t) 1 << 40)
  96. return (double) t + (seconds - at) / perTick;
  97. at += span;
  98. }
  99. }
  100. TempoMap::TempoMap (const Song& song, double maxSeconds)
  101. {
  102. secondsPerTick = 60.0 / (std::max (1.0, song.bpm) * ppq);
  103. const auto hasTempo = song.mode == PlayMode::song
  104. && std::any_of (song.automations.begin(), song.automations.end(),
  105. [] (const Automation& a) { return a.target.kind == AutomationTargetKind::tempo; });
  106. if (! hasTempo)
  107. return;
  108. constexpr std::int64_t step = ticksPerStep;
  109. const auto clips = tempomap::tempoClips (song); // (round 190ry: as Song::secondsForTicks)
  110. double at = 0.0;
  111. stepStarts.push_back (0.0);
  112. for (std::int64_t t = 0; at <= maxSeconds && stepStarts.size() < 4000000; t += step)
  113. {
  114. secondsPerTick = 60.0 / (std::max (1.0, tempomap::tempoAtIn (song, clips, (double) t)) * ppq);
  115. at += secondsPerTick * (double) step;
  116. stepStarts.push_back (at);
  117. }
  118. }
  119. double TempoMap::ticks (double s) const noexcept
  120. {
  121. if (s <= 0.0)
  122. return 0.0;
  123. if (stepStarts.size() < 2)
  124. return s / secondsPerTick;
  125. constexpr double step = (double) ticksPerStep;
  126. if (s >= stepStarts.back())
  127. return (double) (stepStarts.size() - 1) * step + (s - stepStarts.back()) / secondsPerTick;
  128. const auto it = std::upper_bound (stepStarts.begin(), stepStarts.end(), s);
  129. const auto k = (std::size_t) (it - stepStarts.begin()) - 1;
  130. const auto span = stepStarts[k + 1] - stepStarts[k];
  131. return ((double) k + (span > 0.0 ? (s - stepStarts[k]) / span : 0.0)) * step;
  132. }
  133. double TempoMap::seconds (double t) const noexcept
  134. {
  135. if (t <= 0.0)
  136. return 0.0;
  137. if (stepStarts.size() < 2)
  138. return t * secondsPerTick;
  139. constexpr double step = (double) ticksPerStep;
  140. const auto k = (std::size_t) (t / step);
  141. if (k + 1 >= stepStarts.size())
  142. return stepStarts.back() + (t - (double) (stepStarts.size() - 1) * step) * secondsPerTick;
  143. return stepStarts[k] + (stepStarts[k + 1] - stepStarts[k]) * (t / step - (double) k);
  144. }
  145. int addClip (VideoTrack& track, const std::string& path, double start, double duration, std::uint64_t uid)
  146. {
  147. VideoClip c;
  148. c.path = path;
  149. c.start = std::max (0.0, start);
  150. c.offset = 0.0;
  151. c.length = duration > 0.0 ? duration : 10.0;
  152. c.duration = std::max (0.0, duration);
  153. c.uid = uid;
  154. track.clips.push_back (c);
  155. clearUnder (track, track.clips.size() - 1);
  156. return indexOfUid (track, uid, c.start);
  157. }
  158. int moveClip (VideoTrack& track, int index, double start)
  159. {
  160. if (index < 0 || index >= (int) track.clips.size())
  161. return -1;
  162. auto& c = track.clips[(std::size_t) index];
  163. c.start = std::max (0.0, start);
  164. const auto uid = c.uid;
  165. const auto s = c.start;
  166. clearUnder (track, (std::size_t) index);
  167. return indexOfUid (track, uid, s);
  168. }
  169. void trimStart (VideoClip& clip, double newStart, double minLength) noexcept
  170. {
  171. // the head can't go before the file's start nor past the end minus minLength
  172. const auto earliest = clip.start - clip.offset;
  173. newStart = std::clamp (newStart, std::max (0.0, earliest), clip.end() - minLength);
  174. const auto delta = newStart - clip.start;
  175. clip.offset += delta;
  176. clip.length -= delta;
  177. clip.start = newStart;
  178. }
  179. void trimEnd (VideoClip& clip, double newEnd, double minLength) noexcept
  180. {
  181. auto latest = clip.duration > 0.0 ? clip.start + (clip.duration - clip.offset) : 1e12;
  182. newEnd = std::clamp (newEnd, clip.start + minLength, std::max (clip.start + minLength, latest));
  183. clip.length = newEnd - clip.start;
  184. }
  185. bool splitClip (VideoTrack& track, int index, double at, std::uint64_t uid)
  186. {
  187. if (index < 0 || index >= (int) track.clips.size())
  188. return false;
  189. auto& c = track.clips[(std::size_t) index];
  190. if (at <= c.start + 1e-6 || at >= c.end() - 1e-6)
  191. return false;
  192. auto right = c;
  193. right.uid = uid;
  194. right.offset += at - c.start;
  195. right.start = at;
  196. right.length = c.end() - at;
  197. c.length = at - c.start;
  198. track.clips.insert (track.clips.begin() + index + 1, right);
  199. return true;
  200. }
  201. double snapToFrame (const VideoSettings& v, double songSeconds) noexcept
  202. {
  203. const auto tc = timecodeOf (v, songSeconds);
  204. const auto f = std::floor (tc * v.rate.fps() + 0.5);
  205. return songOf (v, vtime::frameStart ((std::int64_t) f, v.rate));
  206. }
  207. double videoEnd (const std::vector<VideoTrack>& tracks) noexcept
  208. {
  209. double e = 0.0;
  210. for (const auto& t : tracks)
  211. for (const auto& c : t.clips)
  212. e = std::max (e, c.end());
  213. return e;
  214. }
  215. bool isVideoPath (const std::string& path) noexcept
  216. {
  217. const auto slash = path.find_last_of ("/\\");
  218. const auto dot = path.find_last_of ('.');
  219. if (dot == std::string::npos || (slash != std::string::npos && dot < slash))
  220. return false;
  221. std::string ext;
  222. for (auto i = dot + 1; i < path.size() && ext.size() < 8; ++i)
  223. ext += (char) std::tolower ((unsigned char) path[i]);
  224. static constexpr const char* list[] = { "mp4", "m4v", "mov", "mkv", "webm", "avi", "wmv", "mxf", "mpg", "mpeg", "m2v",
  225. "ts", "m2ts", "mts", "3gp", "flv", "dv", "ogv" };
  226. for (const auto* e : list)
  227. if (ext == e)
  228. return true;
  229. return false;
  230. }
  231. } // namespace lynx::videotrack