123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- #include "stdafx.h"
- #include "RowVisitor.h"
- #include "Sndfile.h"
- OPENMPT_NAMESPACE_BEGIN
- RowVisitor::LoopState::LoopState(const ChannelStates &chnState, const bool ignoreRow)
- {
-
-
-
-
-
- uint64 hash = FNV1a_BASIS;
- if(ignoreRow)
- {
- hash = (hash ^ 0xFFu) * FNV1a_PRIME;
- #ifdef MPT_VERIFY_ROWVISITOR_LOOPSTATE
- m_counts.emplace_back(uint8(0xFF), uint8(0xFF));
- #endif
- }
- for(size_t chn = 0; chn < chnState.size(); chn++)
- {
- if(chnState[chn].nPatternLoopCount)
- {
- static_assert(MAX_BASECHANNELS <= 256, "Channel index cannot be used as byte input for hash generator");
- static_assert(sizeof(chnState[0].nPatternLoopCount) <= sizeof(uint8), "Loop count cannot be used as byte input for hash generator");
- hash = (hash ^ chn) * FNV1a_PRIME;
- hash = (hash ^ chnState[chn].nPatternLoopCount) * FNV1a_PRIME;
- #ifdef MPT_VERIFY_ROWVISITOR_LOOPSTATE
- m_counts.emplace_back(static_cast<uint8>(chn), chnState[chn].nPatternLoopCount);
- #endif
- }
- }
- m_hash = hash;
- }
- RowVisitor::RowVisitor(const CSoundFile &sndFile, SEQUENCEINDEX sequence)
- : m_sndFile(sndFile)
- , m_sequence(sequence)
- {
- Initialize(true);
- }
- void RowVisitor::MoveVisitedRowsFrom(RowVisitor &other) noexcept
- {
- m_visitedRows = std::move(other.m_visitedRows);
- m_visitedLoopStates = std::move(other.m_visitedLoopStates);
- }
- const ModSequence &RowVisitor::Order() const
- {
- if(m_sequence >= m_sndFile.Order.GetNumSequences())
- return m_sndFile.Order();
- else
- return m_sndFile.Order(m_sequence);
- }
- void RowVisitor::Initialize(bool reset)
- {
- auto &order = Order();
- const ORDERINDEX endOrder = order.GetLengthTailTrimmed();
- m_visitedRows.resize(endOrder);
- if(reset)
- {
- m_visitedLoopStates.clear();
- m_rowsSpentInLoops = 0;
- }
- std::vector<uint8> loopCount;
- std::vector<ORDERINDEX> visitedPatterns(m_sndFile.Patterns.GetNumPatterns(), ORDERINDEX_INVALID);
- for(ORDERINDEX ord = 0; ord < endOrder; ord++)
- {
- const PATTERNINDEX pat = order[ord];
- const ROWINDEX numRows = VisitedRowsVectorSize(pat);
- auto &visitedRows = m_visitedRows[ord];
- if(reset)
- visitedRows.assign(numRows, false);
- else
- visitedRows.resize(numRows, false);
- if(!order.IsValidPat(ord))
- continue;
- const ROWINDEX startRow = std::min(static_cast<ROWINDEX>(reset ? 0 : visitedRows.size()), numRows);
- auto insertionHint = m_visitedLoopStates.end();
- if(visitedPatterns[pat] != ORDERINDEX_INVALID)
- {
-
- const auto begin = m_visitedLoopStates.lower_bound({visitedPatterns[pat], startRow});
- const auto end = (begin != m_visitedLoopStates.end()) ? m_visitedLoopStates.lower_bound({visitedPatterns[pat], numRows}) : m_visitedLoopStates.end();
- for(auto pos = begin; pos != end; ++pos)
- {
- LoopStateSet loopStates;
- loopStates.reserve(pos->second.capacity());
- insertionHint = ++m_visitedLoopStates.insert_or_assign(insertionHint, {ord, pos->first.second}, std::move(loopStates));
- }
- continue;
- }
-
- const auto &pattern = m_sndFile.Patterns[pat];
- loopCount.assign(pattern.GetNumChannels(), 0);
- for(ROWINDEX i = numRows; i != startRow; i--)
- {
- const ROWINDEX row = i - 1;
- uint32 maxLoopStates = 1;
- auto m = pattern.GetRow(row);
-
- for(CHANNELINDEX chn = 0; chn < pattern.GetNumChannels() && maxLoopStates < 16; chn++, m++)
- {
- auto count = loopCount[chn];
- if((m->command == CMD_S3MCMDEX && (m->param & 0xF0) == 0xB0) || (m->command == CMD_MODCMDEX && (m->param & 0xF0) == 0x60))
- {
- loopCount[chn] = (m->param & 0x0F);
- if(loopCount[chn])
- count = loopCount[chn];
- }
- if(count)
- maxLoopStates *= (count + 1);
- }
- if(maxLoopStates > 1)
- {
- LoopStateSet loopStates;
- loopStates.reserve(maxLoopStates);
- insertionHint = m_visitedLoopStates.insert_or_assign(insertionHint, {ord, row}, std::move(loopStates));
- }
- }
-
- if(startRow == 0)
- visitedPatterns[pat] = ord;
- }
- }
- bool RowVisitor::Visit(ORDERINDEX ord, ROWINDEX row, const ChannelStates &chnState, bool ignoreRow)
- {
- auto &order = Order();
- if(ord >= order.size() || row >= VisitedRowsVectorSize(order[ord]))
- return false;
-
- if(ord >= m_visitedRows.size() || row >= m_visitedRows[ord].size())
- {
- Initialize(false);
-
- if(ord >= m_visitedRows.size())
- return false;
- }
- MPT_ASSERT(chnState.size() >= m_sndFile.GetNumChannels());
- LoopState newState{chnState.first(m_sndFile.GetNumChannels()), ignoreRow};
- const auto rowLoopState = m_visitedLoopStates.find({ord, row});
- const bool oldHadLoops = (rowLoopState != m_visitedLoopStates.end() && !rowLoopState->second.empty());
- const bool newHasLoops = newState.HasLoops();
- const bool wasVisited = m_visitedRows[ord][row];
-
-
- if(!oldHadLoops && !newHasLoops && wasVisited)
- return true;
- if(oldHadLoops && mpt::contains(rowLoopState->second, newState))
- return true;
- if(newHasLoops)
- m_rowsSpentInLoops++;
- if(oldHadLoops || newHasLoops)
- {
-
- if(!oldHadLoops && wasVisited)
- m_visitedLoopStates[{ord, row}].emplace_back();
- m_visitedLoopStates[{ord, row}].emplace_back(std::move(newState));
- }
- m_visitedRows[ord][row] = true;
- return false;
- }
- ROWINDEX RowVisitor::VisitedRowsVectorSize(PATTERNINDEX pattern) const noexcept
- {
- if(m_sndFile.Patterns.IsValidPat(pattern))
- return m_sndFile.Patterns[pattern].GetNumRows();
- else
- return 1;
- }
- bool RowVisitor::GetFirstUnvisitedRow(ORDERINDEX &ord, ROWINDEX &row, bool onlyUnplayedPatterns) const
- {
- const auto &order = Order();
- const ORDERINDEX endOrder = order.GetLengthTailTrimmed();
- for(ord = 0; ord < endOrder; ord++)
- {
- if(!order.IsValidPat(ord))
- continue;
- if(ord >= m_visitedRows.size())
- {
-
- row = 0;
- return true;
- }
- const auto &visitedRows = m_visitedRows[ord];
- const auto firstUnplayedRow = std::find(visitedRows.begin(), visitedRows.end(), onlyUnplayedPatterns);
- if(onlyUnplayedPatterns && firstUnplayedRow == visitedRows.end())
- {
-
- row = 0;
- return true;
- } else if(!onlyUnplayedPatterns)
- {
-
- if(firstUnplayedRow != visitedRows.end())
- {
- row = static_cast<ROWINDEX>(std::distance(visitedRows.begin(), firstUnplayedRow));
- return true;
- }
- if(visitedRows.size() < m_sndFile.Patterns[order[ord]].GetNumRows())
- {
-
- row = static_cast<ROWINDEX>(visitedRows.size());
- return true;
- }
- }
- }
-
- ord = ORDERINDEX_INVALID;
- row = ROWINDEX_INVALID;
- return false;
- }
- OPENMPT_NAMESPACE_END
|