1
0

RowVisitor.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * RowVisitor.cpp
  3. * --------------
  4. * Purpose: Class for recording which rows of a song has already been visited, used for detecting when a module starts to loop.
  5. * Notes : The class keeps track of rows that have been visited by the player before.
  6. * This way, we can tell when the module starts to loop, i.e. we can determine the song length,
  7. * or find out that a given point of the module can never be reached.
  8. *
  9. * In some module formats, infinite loops can be achieved through pattern loops (e.g. E60 / E61 / E61 in one channel of a ProTracker MOD).
  10. * To detect such loops, we store a set of loop counts across all channels encountered for each row.
  11. * As soon as a set of loop counts is encountered twice for a specific row, we know that the track ends up in an infinite loop.
  12. * As a result of this design, it is safe to evaluate pattern loops in CSoundFile::GetLength.
  13. * Authors: OpenMPT Devs
  14. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  15. */
  16. #include "stdafx.h"
  17. #include "RowVisitor.h"
  18. #include "Sndfile.h"
  19. OPENMPT_NAMESPACE_BEGIN
  20. RowVisitor::LoopState::LoopState(const ChannelStates &chnState, const bool ignoreRow)
  21. {
  22. // Rather than storing the exact loop count vector, we compute a FNV-1a 64-bit hash of it.
  23. // This means we can store the loop state in a small and fixed amount of memory.
  24. // In theory there is the possibility of hash collisions for different loop states, but in practice,
  25. // the relevant inputs for the hashing algorithm are extremely unlikely to produce collisions.
  26. // There may be better hashing algorithms, but many of them are much more complex and cannot be applied easily in an incremental way.
  27. uint64 hash = FNV1a_BASIS;
  28. if(ignoreRow)
  29. {
  30. hash = (hash ^ 0xFFu) * FNV1a_PRIME;
  31. #ifdef MPT_VERIFY_ROWVISITOR_LOOPSTATE
  32. m_counts.emplace_back(uint8(0xFF), uint8(0xFF));
  33. #endif
  34. }
  35. for(size_t chn = 0; chn < chnState.size(); chn++)
  36. {
  37. if(chnState[chn].nPatternLoopCount)
  38. {
  39. static_assert(MAX_BASECHANNELS <= 256, "Channel index cannot be used as byte input for hash generator");
  40. static_assert(sizeof(chnState[0].nPatternLoopCount) <= sizeof(uint8), "Loop count cannot be used as byte input for hash generator");
  41. hash = (hash ^ chn) * FNV1a_PRIME;
  42. hash = (hash ^ chnState[chn].nPatternLoopCount) * FNV1a_PRIME;
  43. #ifdef MPT_VERIFY_ROWVISITOR_LOOPSTATE
  44. m_counts.emplace_back(static_cast<uint8>(chn), chnState[chn].nPatternLoopCount);
  45. #endif
  46. }
  47. }
  48. m_hash = hash;
  49. }
  50. RowVisitor::RowVisitor(const CSoundFile &sndFile, SEQUENCEINDEX sequence)
  51. : m_sndFile(sndFile)
  52. , m_sequence(sequence)
  53. {
  54. Initialize(true);
  55. }
  56. void RowVisitor::MoveVisitedRowsFrom(RowVisitor &other) noexcept
  57. {
  58. m_visitedRows = std::move(other.m_visitedRows);
  59. m_visitedLoopStates = std::move(other.m_visitedLoopStates);
  60. }
  61. const ModSequence &RowVisitor::Order() const
  62. {
  63. if(m_sequence >= m_sndFile.Order.GetNumSequences())
  64. return m_sndFile.Order();
  65. else
  66. return m_sndFile.Order(m_sequence);
  67. }
  68. // Resize / clear the row vector.
  69. // If reset is true, the vector is not only resized to the required dimensions, but also completely cleared (i.e. all visited rows are reset).
  70. void RowVisitor::Initialize(bool reset)
  71. {
  72. auto &order = Order();
  73. const ORDERINDEX endOrder = order.GetLengthTailTrimmed();
  74. m_visitedRows.resize(endOrder);
  75. if(reset)
  76. {
  77. m_visitedLoopStates.clear();
  78. m_rowsSpentInLoops = 0;
  79. }
  80. std::vector<uint8> loopCount;
  81. std::vector<ORDERINDEX> visitedPatterns(m_sndFile.Patterns.GetNumPatterns(), ORDERINDEX_INVALID);
  82. for(ORDERINDEX ord = 0; ord < endOrder; ord++)
  83. {
  84. const PATTERNINDEX pat = order[ord];
  85. const ROWINDEX numRows = VisitedRowsVectorSize(pat);
  86. auto &visitedRows = m_visitedRows[ord];
  87. if(reset)
  88. visitedRows.assign(numRows, false);
  89. else
  90. visitedRows.resize(numRows, false);
  91. if(!order.IsValidPat(ord))
  92. continue;
  93. const ROWINDEX startRow = std::min(static_cast<ROWINDEX>(reset ? 0 : visitedRows.size()), numRows);
  94. auto insertionHint = m_visitedLoopStates.end();
  95. if(visitedPatterns[pat] != ORDERINDEX_INVALID)
  96. {
  97. // We visited this pattern before, copy over the results
  98. const auto begin = m_visitedLoopStates.lower_bound({visitedPatterns[pat], startRow});
  99. const auto end = (begin != m_visitedLoopStates.end()) ? m_visitedLoopStates.lower_bound({visitedPatterns[pat], numRows}) : m_visitedLoopStates.end();
  100. for(auto pos = begin; pos != end; ++pos)
  101. {
  102. LoopStateSet loopStates;
  103. loopStates.reserve(pos->second.capacity());
  104. insertionHint = ++m_visitedLoopStates.insert_or_assign(insertionHint, {ord, pos->first.second}, std::move(loopStates));
  105. }
  106. continue;
  107. }
  108. // Pre-allocate loop count state
  109. const auto &pattern = m_sndFile.Patterns[pat];
  110. loopCount.assign(pattern.GetNumChannels(), 0);
  111. for(ROWINDEX i = numRows; i != startRow; i--)
  112. {
  113. const ROWINDEX row = i - 1;
  114. uint32 maxLoopStates = 1;
  115. auto m = pattern.GetRow(row);
  116. // Break condition: If it's more than 16, it's probably wrong :) exact loop count depends on how loops overlap.
  117. for(CHANNELINDEX chn = 0; chn < pattern.GetNumChannels() && maxLoopStates < 16; chn++, m++)
  118. {
  119. auto count = loopCount[chn];
  120. if((m->command == CMD_S3MCMDEX && (m->param & 0xF0) == 0xB0) || (m->command == CMD_MODCMDEX && (m->param & 0xF0) == 0x60))
  121. {
  122. loopCount[chn] = (m->param & 0x0F);
  123. if(loopCount[chn])
  124. count = loopCount[chn];
  125. }
  126. if(count)
  127. maxLoopStates *= (count + 1);
  128. }
  129. if(maxLoopStates > 1)
  130. {
  131. LoopStateSet loopStates;
  132. loopStates.reserve(maxLoopStates);
  133. insertionHint = m_visitedLoopStates.insert_or_assign(insertionHint, {ord, row}, std::move(loopStates));
  134. }
  135. }
  136. // Only use this order as a blueprint for other orders using the same pattern if we fully parsed the pattern.
  137. if(startRow == 0)
  138. visitedPatterns[pat] = ord;
  139. }
  140. }
  141. // Mark an order/row combination as visited and returns true if it was visited before.
  142. bool RowVisitor::Visit(ORDERINDEX ord, ROWINDEX row, const ChannelStates &chnState, bool ignoreRow)
  143. {
  144. auto &order = Order();
  145. if(ord >= order.size() || row >= VisitedRowsVectorSize(order[ord]))
  146. return false;
  147. // The module might have been edited in the meantime - so we have to extend this a bit.
  148. if(ord >= m_visitedRows.size() || row >= m_visitedRows[ord].size())
  149. {
  150. Initialize(false);
  151. // If it's still past the end of the vector, this means that ord >= order.GetLengthTailTrimmed(), i.e. we are trying to play an empty order.
  152. if(ord >= m_visitedRows.size())
  153. return false;
  154. }
  155. MPT_ASSERT(chnState.size() >= m_sndFile.GetNumChannels());
  156. LoopState newState{chnState.first(m_sndFile.GetNumChannels()), ignoreRow};
  157. const auto rowLoopState = m_visitedLoopStates.find({ord, row});
  158. const bool oldHadLoops = (rowLoopState != m_visitedLoopStates.end() && !rowLoopState->second.empty());
  159. const bool newHasLoops = newState.HasLoops();
  160. const bool wasVisited = m_visitedRows[ord][row];
  161. // Check if new state is part of row state already. If so, we visited this row already and thus the module must be looping
  162. if(!oldHadLoops && !newHasLoops && wasVisited)
  163. return true;
  164. if(oldHadLoops && mpt::contains(rowLoopState->second, newState))
  165. return true;
  166. if(newHasLoops)
  167. m_rowsSpentInLoops++;
  168. if(oldHadLoops || newHasLoops)
  169. {
  170. // Convert to set representation if it isn't already
  171. if(!oldHadLoops && wasVisited)
  172. m_visitedLoopStates[{ord, row}].emplace_back();
  173. m_visitedLoopStates[{ord, row}].emplace_back(std::move(newState));
  174. }
  175. m_visitedRows[ord][row] = true;
  176. return false;
  177. }
  178. // Get the needed vector size for a given pattern.
  179. ROWINDEX RowVisitor::VisitedRowsVectorSize(PATTERNINDEX pattern) const noexcept
  180. {
  181. if(m_sndFile.Patterns.IsValidPat(pattern))
  182. return m_sndFile.Patterns[pattern].GetNumRows();
  183. else
  184. return 1; // Non-existing patterns consist of a "fake" row.
  185. }
  186. // Find the first row that has not been played yet.
  187. // The order and row is stored in the order and row variables on success, on failure they contain invalid values.
  188. // If onlyUnplayedPatterns is true (default), only completely unplayed patterns are considered, otherwise a song can start on any unplayed row.
  189. // Function returns true on success.
  190. bool RowVisitor::GetFirstUnvisitedRow(ORDERINDEX &ord, ROWINDEX &row, bool onlyUnplayedPatterns) const
  191. {
  192. const auto &order = Order();
  193. const ORDERINDEX endOrder = order.GetLengthTailTrimmed();
  194. for(ord = 0; ord < endOrder; ord++)
  195. {
  196. if(!order.IsValidPat(ord))
  197. continue;
  198. if(ord >= m_visitedRows.size())
  199. {
  200. // Not yet initialized => unvisited
  201. row = 0;
  202. return true;
  203. }
  204. const auto &visitedRows = m_visitedRows[ord];
  205. const auto firstUnplayedRow = std::find(visitedRows.begin(), visitedRows.end(), onlyUnplayedPatterns);
  206. if(onlyUnplayedPatterns && firstUnplayedRow == visitedRows.end())
  207. {
  208. // No row of this pattern has been played yet.
  209. row = 0;
  210. return true;
  211. } else if(!onlyUnplayedPatterns)
  212. {
  213. // Return the first unplayed row in this pattern
  214. if(firstUnplayedRow != visitedRows.end())
  215. {
  216. row = static_cast<ROWINDEX>(std::distance(visitedRows.begin(), firstUnplayedRow));
  217. return true;
  218. }
  219. if(visitedRows.size() < m_sndFile.Patterns[order[ord]].GetNumRows())
  220. {
  221. // History is not fully initialized
  222. row = static_cast<ROWINDEX>(visitedRows.size());
  223. return true;
  224. }
  225. }
  226. }
  227. // Didn't find anything :(
  228. ord = ORDERINDEX_INVALID;
  229. row = ROWINDEX_INVALID;
  230. return false;
  231. }
  232. OPENMPT_NAMESPACE_END