cannam@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
cannam@0
|
2
|
cannam@0
|
3 /*
|
cannam@0
|
4 Vamp feature extraction plugin using the MATCH audio alignment
|
cannam@0
|
5 algorithm.
|
cannam@0
|
6
|
cannam@0
|
7 Centre for Digital Music, Queen Mary, University of London.
|
cannam@0
|
8 This file copyright 2007 Simon Dixon, Chris Cannam and QMUL.
|
cannam@0
|
9
|
cannam@0
|
10 This program is free software; you can redistribute it and/or
|
cannam@0
|
11 modify it under the terms of the GNU General Public License as
|
cannam@0
|
12 published by the Free Software Foundation; either version 2 of the
|
cannam@0
|
13 License, or (at your option) any later version. See the file
|
cannam@0
|
14 COPYING included with this distribution for more information.
|
cannam@0
|
15 */
|
cannam@0
|
16
|
cannam@0
|
17 #include "Matcher.h"
|
cannam@0
|
18
|
cannam@0
|
19 #include <iostream>
|
cannam@0
|
20
|
cannam@4
|
21 #include <cstdlib>
|
Chris@16
|
22 #include <cassert>
|
cannam@4
|
23
|
Chris@72
|
24 using namespace std;
|
Chris@72
|
25
|
Chris@10
|
26 //#define DEBUG_MATCHER 1
|
Chris@10
|
27
|
Chris@143
|
28 Matcher::Matcher(Parameters parameters, DistanceMetric::Parameters dparams,
|
Chris@143
|
29 Matcher *p) :
|
Chris@43
|
30 m_params(parameters),
|
Chris@143
|
31 m_metric(dparams)
|
Chris@23
|
32 {
|
Chris@23
|
33 #ifdef DEBUG_MATCHER
|
Chris@143
|
34 cerr << "*** Matcher: hopTime = " << parameters.hopTime
|
Chris@140
|
35 << ", blockTime = " << parameters.blockTime
|
Chris@140
|
36 << ", maxRunCount = " << parameters.maxRunCount
|
Chris@140
|
37 << ", diagonalWeight = " << parameters.diagonalWeight << endl;
|
Chris@23
|
38 #endif
|
Chris@140
|
39
|
Chris@43
|
40 m_otherMatcher = p; // the first matcher will need this to be set later
|
Chris@43
|
41 m_firstPM = (!p);
|
Chris@43
|
42 m_frameCount = 0;
|
Chris@43
|
43 m_runCount = 0;
|
Chris@43
|
44 m_blockSize = 0;
|
Chris@202
|
45 m_distXSize = 0;
|
cannam@0
|
46
|
Chris@180
|
47 m_blockSize = int(m_params.blockTime / m_params.hopTime + 0.5);
|
Chris@15
|
48 #ifdef DEBUG_MATCHER
|
Chris@43
|
49 cerr << "Matcher: m_blockSize = " << m_blockSize << endl;
|
Chris@15
|
50 #endif
|
cannam@0
|
51
|
Chris@43
|
52 m_initialised = false;
|
Chris@23
|
53 }
|
cannam@0
|
54
|
cannam@0
|
55 Matcher::~Matcher()
|
cannam@0
|
56 {
|
Chris@10
|
57 #ifdef DEBUG_MATCHER
|
Chris@15
|
58 cerr << "Matcher(" << this << ")::~Matcher()" << endl;
|
Chris@10
|
59 #endif
|
cannam@0
|
60 }
|
cannam@0
|
61
|
cannam@0
|
62 void
|
cannam@0
|
63 Matcher::init()
|
cannam@0
|
64 {
|
Chris@43
|
65 if (m_initialised) return;
|
cannam@0
|
66
|
Chris@182
|
67 m_features = featureseq_t(m_blockSize);
|
cannam@0
|
68
|
Chris@43
|
69 m_distXSize = m_blockSize * 2;
|
Chris@45
|
70
|
Chris@41
|
71 size();
|
cannam@0
|
72
|
Chris@43
|
73 m_frameCount = 0;
|
Chris@43
|
74 m_runCount = 0;
|
Chris@38
|
75
|
Chris@43
|
76 m_initialised = true;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@87
|
79 bool
|
Chris@203
|
80 Matcher::isAvailable(int i, int j)
|
Chris@154
|
81 {
|
Chris@203
|
82 if (m_firstPM) {
|
Chris@203
|
83 if (isInRange(i, j)) {
|
Chris@203
|
84 return (m_bestPathCost[i][j - m_first[i]] != InvalidPathCost);
|
Chris@203
|
85 } else {
|
Chris@203
|
86 return false;
|
Chris@154
|
87 }
|
Chris@203
|
88 } else {
|
Chris@203
|
89 return m_otherMatcher->isAvailable(j, i);
|
Chris@154
|
90 }
|
Chris@154
|
91 }
|
Chris@154
|
92
|
Chris@154
|
93 bool
|
Chris@203
|
94 Matcher::isRowAvailable(int i)
|
Chris@154
|
95 {
|
Chris@203
|
96 if (m_firstPM) {
|
Chris@203
|
97
|
Chris@203
|
98 if (i < 0 || i >= int(m_first.size())) return false;
|
Chris@203
|
99 for (auto c: m_bestPathCost[i]) {
|
Chris@203
|
100 if (c != InvalidPathCost) return true;
|
Chris@203
|
101 }
|
Chris@203
|
102 return false;
|
Chris@203
|
103
|
Chris@203
|
104 } else {
|
Chris@203
|
105 return m_otherMatcher->isColAvailable(i);
|
Chris@203
|
106 }
|
Chris@203
|
107 }
|
Chris@203
|
108
|
Chris@203
|
109 bool
|
Chris@203
|
110 Matcher::isColAvailable(int j)
|
Chris@203
|
111 {
|
Chris@203
|
112 if (m_firstPM) {
|
Chris@203
|
113 for (int i = 0; i < int(m_first.size()); ++i) {
|
Chris@203
|
114 if (j >= m_first[i] &&
|
Chris@203
|
115 j < int(m_first[i] + m_bestPathCost[i].size())) {//!!! m_last[i]?
|
Chris@203
|
116 if (m_bestPathCost[i][j - m_first[i]] != InvalidPathCost) {
|
Chris@203
|
117 return true;
|
Chris@203
|
118 }
|
Chris@203
|
119 }
|
Chris@203
|
120 }
|
Chris@203
|
121 return false;
|
Chris@203
|
122 } else {
|
Chris@203
|
123 return m_otherMatcher->isRowAvailable(j);
|
Chris@203
|
124 }
|
Chris@154
|
125 }
|
Chris@154
|
126
|
Chris@154
|
127 bool
|
Chris@87
|
128 Matcher::isInRange(int i, int j)
|
Chris@87
|
129 {
|
Chris@87
|
130 if (m_firstPM) {
|
Chris@87
|
131 return ((i >= 0) &&
|
Chris@87
|
132 (i < int(m_first.size())) &&
|
Chris@87
|
133 (j >= m_first[i]) &&
|
Chris@87
|
134 (j < int(m_first[i] + m_bestPathCost[i].size())));
|
Chris@87
|
135 } else {
|
Chris@87
|
136 return m_otherMatcher->isInRange(j, i);
|
Chris@87
|
137 }
|
Chris@87
|
138 }
|
Chris@87
|
139
|
Chris@87
|
140 pair<int, int>
|
Chris@205
|
141 Matcher::getColRangeForRow(int i)
|
Chris@87
|
142 {
|
Chris@204
|
143 if (m_firstPM) {
|
Chris@204
|
144 if (i < 0 || i >= int(m_first.size())) {
|
Chris@204
|
145 cerr << "ERROR: Matcher::getColRange(" << i << "): Index out of range"
|
Chris@204
|
146 << endl;
|
Chris@204
|
147 throw "Index out of range";
|
Chris@204
|
148 } else {
|
Chris@204
|
149 return pair<int, int>(m_first[i], m_last[i]);
|
Chris@204
|
150 }
|
Chris@87
|
151 } else {
|
Chris@205
|
152 return m_otherMatcher->getRowRangeForCol(i);
|
Chris@87
|
153 }
|
Chris@87
|
154 }
|
Chris@87
|
155
|
Chris@87
|
156 pair<int, int>
|
Chris@205
|
157 Matcher::getRowRangeForCol(int j)
|
Chris@87
|
158 {
|
Chris@204
|
159 if (m_firstPM) {
|
Chris@204
|
160
|
Chris@204
|
161 //!!! tedious, examine uses (& consider restoring use of
|
Chris@204
|
162 //!!! first/last in "other" matcher)
|
Chris@204
|
163 int a = -1, b = -1;
|
Chris@204
|
164 for (int i = 0; i < int(m_first.size()); ++i) {
|
Chris@204
|
165 if (j >= m_first[i] && j < m_last[i]) {
|
Chris@204
|
166 if (a == -1) a = i;
|
Chris@204
|
167 b = i;
|
Chris@204
|
168 }
|
Chris@204
|
169 }
|
Chris@204
|
170 if (a == -1) {
|
Chris@204
|
171 cerr << "ERROR: Matcher::getRowRange(" << j << "): Index out of range"
|
Chris@204
|
172 << endl;
|
Chris@204
|
173 throw "Index out of range";
|
Chris@204
|
174 } else {
|
Chris@204
|
175 return pair<int, int>(a, b + 1);
|
Chris@204
|
176 }
|
Chris@204
|
177
|
Chris@204
|
178 } else {
|
Chris@205
|
179 return m_otherMatcher->getColRangeForRow(j);
|
Chris@204
|
180 }
|
Chris@87
|
181 }
|
Chris@87
|
182
|
Chris@182
|
183 distance_t
|
Chris@87
|
184 Matcher::getDistance(int i, int j)
|
Chris@87
|
185 {
|
Chris@87
|
186 if (m_firstPM) {
|
Chris@87
|
187 if (!isInRange(i, j)) {
|
Chris@87
|
188 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): "
|
Chris@87
|
189 << "Location is not in range" << endl;
|
Chris@87
|
190 throw "Distance not available";
|
Chris@87
|
191 }
|
Chris@182
|
192 distance_t dist = m_distance[i][j - m_first[i]];
|
Chris@183
|
193 if (dist == InvalidDistance) {
|
Chris@87
|
194 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): "
|
Chris@87
|
195 << "Location is in range, but distance ("
|
Chris@191
|
196 << distance_print_t(dist)
|
Chris@191
|
197 << ") is invalid or has not been set" << endl;
|
Chris@87
|
198 throw "Distance not available";
|
Chris@87
|
199 }
|
Chris@87
|
200 return dist;
|
Chris@87
|
201 } else {
|
Chris@87
|
202 return m_otherMatcher->getDistance(j, i);
|
Chris@87
|
203 }
|
Chris@87
|
204 }
|
Chris@87
|
205
|
Chris@87
|
206 void
|
Chris@182
|
207 Matcher::setDistance(int i, int j, distance_t distance)
|
Chris@87
|
208 {
|
Chris@87
|
209 if (m_firstPM) {
|
Chris@87
|
210 if (!isInRange(i, j)) {
|
Chris@87
|
211 cerr << "ERROR: Matcher::setDistance(" << i << ", " << j << ", "
|
Chris@191
|
212 << distance_print_t(distance)
|
Chris@191
|
213 << "): Location is out of range" << endl;
|
Chris@87
|
214 throw "Indices out of range";
|
Chris@87
|
215 }
|
Chris@87
|
216 m_distance[i][j - m_first[i]] = distance;
|
Chris@87
|
217 } else {
|
Chris@87
|
218 m_otherMatcher->setDistance(j, i, distance);
|
Chris@87
|
219 }
|
Chris@87
|
220 }
|
Chris@87
|
221
|
Chris@191
|
222 normpathcost_t
|
Chris@135
|
223 Matcher::getNormalisedPathCost(int i, int j)
|
Chris@135
|
224 {
|
Chris@135
|
225 // normalised for path length. 1+ prevents division by zero here
|
Chris@191
|
226 return normpathcost_t(getPathCost(i, j)) / normpathcost_t(1 + i + j);
|
Chris@135
|
227 }
|
Chris@135
|
228
|
Chris@182
|
229 pathcost_t
|
Chris@87
|
230 Matcher::getPathCost(int i, int j)
|
Chris@87
|
231 {
|
Chris@87
|
232 if (m_firstPM) {
|
Chris@203
|
233 #ifdef PERFORM_ERROR_CHECKS
|
Chris@87
|
234 if (!isAvailable(i, j)) {
|
Chris@87
|
235 if (!isInRange(i, j)) {
|
Chris@87
|
236 cerr << "ERROR: Matcher::getPathCost(" << i << ", " << j << "): "
|
Chris@87
|
237 << "Location is not in range" << endl;
|
Chris@87
|
238 } else {
|
Chris@87
|
239 cerr << "ERROR: Matcher::getPathCost(" << i << ", " << j << "): "
|
Chris@87
|
240 << "Location is in range, but pathCost ("
|
Chris@87
|
241 << m_bestPathCost[i][j - m_first[i]]
|
Chris@87
|
242 << ") is invalid or has not been set" << endl;
|
Chris@87
|
243 }
|
Chris@87
|
244 throw "Path cost not available";
|
Chris@87
|
245 }
|
Chris@203
|
246 #endif
|
Chris@87
|
247 return m_bestPathCost[i][j - m_first[i]];
|
Chris@87
|
248 } else {
|
Chris@87
|
249 return m_otherMatcher->getPathCost(j, i);
|
Chris@87
|
250 }
|
Chris@87
|
251 }
|
Chris@87
|
252
|
Chris@87
|
253 void
|
Chris@182
|
254 Matcher::setPathCost(int i, int j, advance_t dir, pathcost_t pathCost)
|
Chris@87
|
255 {
|
Chris@87
|
256 if (m_firstPM) {
|
Chris@87
|
257 if (!isInRange(i, j)) {
|
Chris@87
|
258 cerr << "ERROR: Matcher::setPathCost(" << i << ", " << j << ", "
|
Chris@87
|
259 << dir << ", " << pathCost
|
Chris@87
|
260 << "): Location is out of range" << endl;
|
Chris@87
|
261 throw "Indices out of range";
|
Chris@87
|
262 }
|
Chris@87
|
263 m_advance[i][j - m_first[i]] = dir;
|
Chris@87
|
264 m_bestPathCost[i][j - m_first[i]] = pathCost;
|
Chris@87
|
265 } else {
|
Chris@87
|
266 if (dir == AdvanceThis) {
|
Chris@87
|
267 dir = AdvanceOther;
|
Chris@87
|
268 } else if (dir == AdvanceOther) {
|
Chris@87
|
269 dir = AdvanceThis;
|
Chris@87
|
270 }
|
Chris@87
|
271 m_otherMatcher->setPathCost(j, i, dir, pathCost);
|
Chris@87
|
272 }
|
Chris@87
|
273 }
|
Chris@87
|
274
|
cannam@0
|
275 void
|
Chris@41
|
276 Matcher::size()
|
cannam@0
|
277 {
|
Chris@43
|
278 int distSize = (m_params.maxRunCount + 1) * m_blockSize;
|
Chris@183
|
279 m_bestPathCost.resize(m_distXSize, pathcostvec_t(distSize, InvalidPathCost));
|
Chris@183
|
280 m_distance.resize(m_distXSize, distancevec_t(distSize, InvalidDistance));
|
Chris@182
|
281 m_advance.resize(m_distXSize, advancevec_t(distSize, AdvanceNone));
|
Chris@43
|
282 m_first.resize(m_distXSize, 0);
|
Chris@43
|
283 m_last.resize(m_distXSize, 0);
|
Chris@38
|
284 }
|
cannam@0
|
285
|
Chris@23
|
286 void
|
Chris@182
|
287 Matcher::consumeFeatureVector(const feature_t &feature)
|
Chris@23
|
288 {
|
Chris@43
|
289 if (!m_initialised) init();
|
Chris@43
|
290 int frameIndex = m_frameCount % m_blockSize;
|
Chris@182
|
291 m_features[frameIndex] = feature;
|
Chris@23
|
292 calcAdvance();
|
Chris@21
|
293 }
|
Chris@21
|
294
|
Chris@21
|
295 void
|
Chris@21
|
296 Matcher::calcAdvance()
|
Chris@21
|
297 {
|
Chris@43
|
298 int frameIndex = m_frameCount % m_blockSize;
|
Chris@21
|
299
|
Chris@43
|
300 if (m_frameCount >= m_distXSize) {
|
Chris@43
|
301 m_distXSize *= 2;
|
Chris@41
|
302 size();
|
cannam@0
|
303 }
|
cannam@0
|
304
|
Chris@43
|
305 if (m_firstPM && (m_frameCount >= m_blockSize)) {
|
cannam@0
|
306
|
Chris@43
|
307 int len = m_last[m_frameCount - m_blockSize] -
|
Chris@43
|
308 m_first[m_frameCount - m_blockSize];
|
cannam@0
|
309
|
Chris@43
|
310 // We need to copy distance[m_frameCount-m_blockSize] to
|
Chris@43
|
311 // distance[m_frameCount], and then truncate
|
Chris@43
|
312 // distance[m_frameCount-m_blockSize] to its first len elements.
|
cannam@0
|
313 // Same for bestPathCost.
|
cannam@0
|
314
|
Chris@182
|
315 distancevec_t dOld(m_distance[m_frameCount - m_blockSize]);
|
Chris@183
|
316 distancevec_t dNew(len, InvalidDistance);
|
cannam@0
|
317
|
Chris@182
|
318 pathcostvec_t bpcOld(m_bestPathCost[m_frameCount - m_blockSize]);
|
Chris@183
|
319 pathcostvec_t bpcNew(len, InvalidPathCost);
|
Chris@69
|
320
|
Chris@182
|
321 advancevec_t adOld(m_advance[m_frameCount - m_blockSize]);
|
Chris@182
|
322 advancevec_t adNew(len, AdvanceNone);
|
Chris@69
|
323
|
Chris@69
|
324 for (int i = 0; i < len; ++i) {
|
Chris@69
|
325 dNew[i] = dOld[i];
|
Chris@69
|
326 bpcNew[i] = bpcOld[i];
|
Chris@69
|
327 adNew[i] = adOld[i];
|
Chris@69
|
328 }
|
Chris@45
|
329
|
Chris@69
|
330 m_distance[m_frameCount] = dOld;
|
Chris@69
|
331 m_distance[m_frameCount - m_blockSize] = dNew;
|
Chris@69
|
332
|
Chris@69
|
333 m_bestPathCost[m_frameCount] = bpcOld;
|
Chris@69
|
334 m_bestPathCost[m_frameCount - m_blockSize] = bpcNew;
|
Chris@69
|
335
|
Chris@69
|
336 m_advance[m_frameCount] = adOld;
|
Chris@69
|
337 m_advance[m_frameCount - m_blockSize] = adNew;
|
cannam@0
|
338 }
|
cannam@0
|
339
|
Chris@43
|
340 int stop = m_otherMatcher->m_frameCount;
|
Chris@43
|
341 int index = stop - m_blockSize;
|
Chris@86
|
342 if (index < 0) index = 0;
|
Chris@86
|
343
|
Chris@43
|
344 m_first[m_frameCount] = index;
|
Chris@43
|
345 m_last[m_frameCount] = stop;
|
cannam@0
|
346
|
cannam@0
|
347 for ( ; index < stop; index++) {
|
Chris@26
|
348
|
Chris@188
|
349 distance_t distance = m_metric.calcDistance
|
Chris@182
|
350 (m_features[frameIndex],
|
Chris@182
|
351 m_otherMatcher->m_features[index % m_blockSize]);
|
Chris@26
|
352
|
Chris@188
|
353 pathcost_t straightIncrement(distance);
|
Chris@188
|
354 pathcost_t diagIncrement = pathcost_t(distance * m_params.diagonalWeight);
|
Chris@89
|
355
|
Chris@86
|
356 if ((m_frameCount == 0) && (index == 0)) { // first element
|
Chris@86
|
357
|
Chris@86
|
358 updateValue(0, 0, AdvanceNone,
|
Chris@86
|
359 0,
|
Chris@86
|
360 distance);
|
Chris@86
|
361
|
Chris@86
|
362 } else if (m_frameCount == 0) { // first row
|
Chris@86
|
363
|
Chris@71
|
364 updateValue(0, index, AdvanceOther,
|
Chris@86
|
365 getPathCost(0, index-1),
|
Chris@86
|
366 distance);
|
Chris@86
|
367
|
Chris@86
|
368 } else if (index == 0) { // first column
|
Chris@86
|
369
|
Chris@71
|
370 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
371 getPathCost(m_frameCount - 1, 0),
|
Chris@86
|
372 distance);
|
Chris@86
|
373
|
Chris@86
|
374 } else if (index == m_otherMatcher->m_frameCount - m_blockSize) {
|
Chris@86
|
375
|
cannam@0
|
376 // missing value(s) due to cutoff
|
cannam@0
|
377 // - no previous value in current row (resp. column)
|
cannam@0
|
378 // - no diagonal value if prev. dir. == curr. dirn
|
Chris@86
|
379
|
Chris@182
|
380 pathcost_t min2 = getPathCost(m_frameCount - 1, index);
|
Chris@86
|
381
|
Chris@91
|
382 // cerr << "NOTE: missing value at i = " << m_frameCount << ", j = "
|
Chris@91
|
383 // << index << " (first = " << m_firstPM << ")" << endl;
|
Chris@86
|
384
|
Chris@43
|
385 // if ((m_firstPM && (first[m_frameCount - 1] == index)) ||
|
Chris@43
|
386 // (!m_firstPM && (m_last[index-1] < m_frameCount)))
|
Chris@86
|
387 if (m_first[m_frameCount - 1] == index) {
|
Chris@86
|
388
|
Chris@86
|
389 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
390 min2, distance);
|
Chris@86
|
391
|
Chris@86
|
392 } else {
|
Chris@86
|
393
|
Chris@182
|
394 pathcost_t min1 = getPathCost(m_frameCount - 1, index - 1);
|
Chris@188
|
395 if (min1 + diagIncrement <= min2 + straightIncrement) {
|
Chris@86
|
396 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
397 min1, distance);
|
Chris@86
|
398 } else {
|
Chris@86
|
399 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
400 min2, distance);
|
Chris@86
|
401 }
|
cannam@0
|
402 }
|
Chris@86
|
403
|
cannam@0
|
404 } else {
|
Chris@86
|
405
|
Chris@182
|
406 pathcost_t min1 = getPathCost(m_frameCount, index - 1);
|
Chris@182
|
407 pathcost_t min2 = getPathCost(m_frameCount - 1, index);
|
Chris@182
|
408 pathcost_t min3 = getPathCost(m_frameCount - 1, index - 1);
|
Chris@87
|
409
|
Chris@188
|
410 pathcost_t cost1 = min1 + straightIncrement;
|
Chris@188
|
411 pathcost_t cost2 = min2 + straightIncrement;
|
Chris@188
|
412 pathcost_t cost3 = min3 + diagIncrement;
|
Chris@93
|
413
|
Chris@93
|
414 // Choosing is easy if there is a strict cheapest of the
|
Chris@93
|
415 // three. If two or more share the lowest cost, we choose
|
Chris@93
|
416 // in order of preference: cost3 (AdvanceBoth), cost2
|
Chris@94
|
417 // (AdvanceThis), cost1 (AdvanceOther) if we are the first
|
Chris@94
|
418 // matcher; and cost3 (AdvanceBoth), cost1 (AdvanceOther),
|
Chris@94
|
419 // cost2 (AdvanceThis) if we are the second matcher. That
|
Chris@94
|
420 // is, we always prioritise the diagonal followed by the
|
Chris@94
|
421 // first matcher.
|
Chris@94
|
422
|
Chris@94
|
423 if (( m_firstPM && (cost1 < cost2)) ||
|
Chris@94
|
424 (!m_firstPM && (cost1 <= cost2))) {
|
Chris@89
|
425 if (cost3 <= cost1) {
|
Chris@86
|
426 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
427 min3, distance);
|
Chris@86
|
428 } else {
|
Chris@86
|
429 updateValue(m_frameCount, index, AdvanceOther,
|
Chris@86
|
430 min1, distance);
|
Chris@86
|
431 }
|
cannam@0
|
432 } else {
|
Chris@89
|
433 if (cost3 <= cost2) {
|
Chris@86
|
434 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
435 min3, distance);
|
Chris@86
|
436 } else {
|
Chris@86
|
437 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
438 min2, distance);
|
Chris@86
|
439 }
|
cannam@0
|
440 }
|
cannam@0
|
441 }
|
Chris@86
|
442
|
Chris@43
|
443 m_otherMatcher->m_last[index]++;
|
cannam@0
|
444 } // loop for row (resp. column)
|
cannam@0
|
445
|
Chris@43
|
446 m_frameCount++;
|
Chris@43
|
447 m_runCount++;
|
cannam@0
|
448
|
Chris@43
|
449 m_otherMatcher->m_runCount = 0;
|
Chris@21
|
450 }
|
cannam@0
|
451
|
cannam@0
|
452 void
|
Chris@182
|
453 Matcher::updateValue(int i, int j, advance_t dir, pathcost_t value, distance_t distance)
|
cannam@0
|
454 {
|
Chris@188
|
455 pathcost_t increment = distance;
|
Chris@83
|
456 if (dir == AdvanceBoth) {
|
Chris@188
|
457 increment = pathcost_t(increment * m_params.diagonalWeight);
|
Chris@188
|
458 }
|
Chris@188
|
459
|
Chris@188
|
460 pathcost_t newValue = value + increment;
|
Chris@188
|
461 if (MaxPathCost - increment < value) {
|
Chris@188
|
462 cerr << "ERROR: Path cost overflow at i=" << i << ", j=" << j << ": "
|
Chris@188
|
463 << value << " + " << increment << " > " << MaxPathCost << endl;
|
Chris@188
|
464 newValue = MaxPathCost;
|
Chris@83
|
465 }
|
Chris@83
|
466
|
Chris@43
|
467 if (m_firstPM) {
|
Chris@45
|
468
|
Chris@96
|
469 setDistance(i, j, distance);
|
Chris@188
|
470 setPathCost(i, j, dir, newValue);
|
Chris@45
|
471
|
cannam@0
|
472 } else {
|
Chris@45
|
473
|
Chris@86
|
474 if (dir == AdvanceThis) dir = AdvanceOther;
|
Chris@86
|
475 else if (dir == AdvanceOther) dir = AdvanceThis;
|
Chris@84
|
476
|
Chris@43
|
477 int idx = i - m_otherMatcher->m_first[j];
|
Chris@45
|
478
|
Chris@188
|
479 if (idx < 0 || size_t(idx) == m_otherMatcher->m_distance[j].size()) {
|
cannam@0
|
480 // This should never happen, but if we allow arbitrary
|
cannam@0
|
481 // pauses in either direction, and arbitrary lengths at
|
cannam@0
|
482 // end, it is better than a segmentation fault.
|
Chris@72
|
483 cerr << "Emergency resize: " << idx << " -> " << idx * 2 << endl;
|
Chris@183
|
484 m_otherMatcher->m_bestPathCost[j].resize(idx * 2, InvalidPathCost);
|
Chris@183
|
485 m_otherMatcher->m_distance[j].resize(idx * 2, InvalidDistance);
|
Chris@46
|
486 m_otherMatcher->m_advance[j].resize(idx * 2, AdvanceNone);
|
cannam@0
|
487 }
|
Chris@45
|
488
|
Chris@96
|
489 m_otherMatcher->setDistance(j, i, distance);
|
Chris@188
|
490 m_otherMatcher->setPathCost(j, i, dir, newValue);
|
cannam@0
|
491 }
|
Chris@71
|
492 }
|
cannam@0
|
493
|
Chris@181
|
494 advance_t
|
Chris@72
|
495 Matcher::getAdvance(int i, int j)
|
Chris@72
|
496 {
|
Chris@72
|
497 if (m_firstPM) {
|
Chris@72
|
498 if (!isInRange(i, j)) {
|
Chris@72
|
499 cerr << "ERROR: Matcher::getAdvance(" << i << ", " << j << "): "
|
Chris@72
|
500 << "Location is not in range" << endl;
|
Chris@72
|
501 throw "Advance not available";
|
Chris@72
|
502 }
|
Chris@72
|
503 return m_advance[i][j - m_first[i]];
|
Chris@72
|
504 } else {
|
Chris@72
|
505 return m_otherMatcher->getAdvance(j, i);
|
Chris@72
|
506 }
|
Chris@72
|
507 }
|
Chris@202
|
508
|
Chris@202
|
509 static double k(size_t sz)
|
Chris@202
|
510 {
|
Chris@202
|
511 return double(sz) / 1024.0;
|
Chris@202
|
512 }
|
Chris@202
|
513
|
Chris@202
|
514 void
|
Chris@202
|
515 Matcher::printStats()
|
Chris@202
|
516 {
|
Chris@202
|
517 if (m_firstPM) cerr << endl;
|
Chris@202
|
518
|
Chris@202
|
519 cerr << "Matcher[" << this << "] (" << (m_firstPM ? "first" : "second") << "):" << endl;
|
Chris@202
|
520 cerr << "- block size " << m_blockSize << ", frame count " << m_frameCount << ", dist x size " << m_distXSize << ", initialised " << m_initialised << endl;
|
Chris@202
|
521
|
Chris@202
|
522 if (m_features.empty()) {
|
Chris@202
|
523 cerr << "- have no features yet" << endl;
|
Chris@202
|
524 } else {
|
Chris@202
|
525 cerr << "- have " << m_features.size() << " features of " << m_features[0].size() << " bins each (= "
|
Chris@202
|
526 << k(m_features.size() * m_features[0].size() * sizeof(featurebin_t)) << "K)" << endl;
|
Chris@202
|
527 }
|
Chris@202
|
528
|
Chris@202
|
529 size_t cells = 0;
|
Chris@202
|
530 for (const auto &d: m_distance) {
|
Chris@202
|
531 cells += d.size();
|
Chris@202
|
532 }
|
Chris@202
|
533 if (m_distance.empty()) {
|
Chris@202
|
534 cerr << "- have no cells in matrix" << endl;
|
Chris@202
|
535 } else {
|
Chris@202
|
536 cerr << "- have " << m_distance.size() << " cols in matrix with avg "
|
Chris@203
|
537 << double(cells) / double(m_distance.size()) << " rows, total "
|
Chris@202
|
538 << cells << " cells" << endl;
|
Chris@202
|
539 cerr << "- path costs " << k(cells * sizeof(pathcost_t))
|
Chris@202
|
540 << "K, distances " << k(cells * sizeof(distance_t))
|
Chris@202
|
541 << "K, advances " << k(cells * sizeof(advance_t)) << "K" << endl;
|
Chris@202
|
542 }
|
Chris@202
|
543
|
Chris@202
|
544 if (m_firstPM && m_otherMatcher) {
|
Chris@202
|
545 m_otherMatcher->printStats();
|
Chris@202
|
546 cerr << endl;
|
Chris@202
|
547 }
|
Chris@202
|
548 }
|
Chris@202
|
549
|