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@206
|
145 cerr << "ERROR: Matcher::getColRangeForRow(" << 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@206
|
157 Matcher::getRowRangeForCol(int i)
|
Chris@87
|
158 {
|
Chris@204
|
159 if (m_firstPM) {
|
Chris@204
|
160
|
Chris@206
|
161 if (i < 0 || i >= int(m_otherMatcher->m_first.size())) {
|
Chris@206
|
162 cerr << "ERROR: Matcher::getRowRangeForCol(" << i << "): Index out of range"
|
Chris@204
|
163 << endl;
|
Chris@204
|
164 throw "Index out of range";
|
Chris@204
|
165 } else {
|
Chris@206
|
166 return pair<int, int>(m_otherMatcher->m_first[i],
|
Chris@206
|
167 m_otherMatcher->m_last[i]);
|
Chris@204
|
168 }
|
Chris@204
|
169
|
Chris@204
|
170 } else {
|
Chris@206
|
171 return m_otherMatcher->getColRangeForRow(i);
|
Chris@204
|
172 }
|
Chris@87
|
173 }
|
Chris@87
|
174
|
Chris@182
|
175 distance_t
|
Chris@87
|
176 Matcher::getDistance(int i, int j)
|
Chris@87
|
177 {
|
Chris@87
|
178 if (m_firstPM) {
|
Chris@87
|
179 if (!isInRange(i, j)) {
|
Chris@87
|
180 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): "
|
Chris@87
|
181 << "Location is not in range" << endl;
|
Chris@87
|
182 throw "Distance not available";
|
Chris@87
|
183 }
|
Chris@182
|
184 distance_t dist = m_distance[i][j - m_first[i]];
|
Chris@183
|
185 if (dist == InvalidDistance) {
|
Chris@87
|
186 cerr << "ERROR: Matcher::getDistance(" << i << ", " << j << "): "
|
Chris@87
|
187 << "Location is in range, but distance ("
|
Chris@191
|
188 << distance_print_t(dist)
|
Chris@191
|
189 << ") is invalid or has not been set" << endl;
|
Chris@87
|
190 throw "Distance not available";
|
Chris@87
|
191 }
|
Chris@87
|
192 return dist;
|
Chris@87
|
193 } else {
|
Chris@87
|
194 return m_otherMatcher->getDistance(j, i);
|
Chris@87
|
195 }
|
Chris@87
|
196 }
|
Chris@87
|
197
|
Chris@87
|
198 void
|
Chris@182
|
199 Matcher::setDistance(int i, int j, distance_t distance)
|
Chris@87
|
200 {
|
Chris@87
|
201 if (m_firstPM) {
|
Chris@87
|
202 if (!isInRange(i, j)) {
|
Chris@87
|
203 cerr << "ERROR: Matcher::setDistance(" << i << ", " << j << ", "
|
Chris@191
|
204 << distance_print_t(distance)
|
Chris@191
|
205 << "): Location is out of range" << endl;
|
Chris@87
|
206 throw "Indices out of range";
|
Chris@87
|
207 }
|
Chris@87
|
208 m_distance[i][j - m_first[i]] = distance;
|
Chris@87
|
209 } else {
|
Chris@87
|
210 m_otherMatcher->setDistance(j, i, distance);
|
Chris@87
|
211 }
|
Chris@87
|
212 }
|
Chris@87
|
213
|
Chris@191
|
214 normpathcost_t
|
Chris@135
|
215 Matcher::getNormalisedPathCost(int i, int j)
|
Chris@135
|
216 {
|
Chris@135
|
217 // normalised for path length. 1+ prevents division by zero here
|
Chris@191
|
218 return normpathcost_t(getPathCost(i, j)) / normpathcost_t(1 + i + j);
|
Chris@135
|
219 }
|
Chris@135
|
220
|
Chris@182
|
221 pathcost_t
|
Chris@87
|
222 Matcher::getPathCost(int i, int j)
|
Chris@87
|
223 {
|
Chris@87
|
224 if (m_firstPM) {
|
Chris@203
|
225 #ifdef PERFORM_ERROR_CHECKS
|
Chris@87
|
226 if (!isAvailable(i, j)) {
|
Chris@87
|
227 if (!isInRange(i, j)) {
|
Chris@87
|
228 cerr << "ERROR: Matcher::getPathCost(" << i << ", " << j << "): "
|
Chris@87
|
229 << "Location is not in range" << endl;
|
Chris@87
|
230 } else {
|
Chris@87
|
231 cerr << "ERROR: Matcher::getPathCost(" << i << ", " << j << "): "
|
Chris@87
|
232 << "Location is in range, but pathCost ("
|
Chris@87
|
233 << m_bestPathCost[i][j - m_first[i]]
|
Chris@87
|
234 << ") is invalid or has not been set" << endl;
|
Chris@87
|
235 }
|
Chris@87
|
236 throw "Path cost not available";
|
Chris@87
|
237 }
|
Chris@203
|
238 #endif
|
Chris@87
|
239 return m_bestPathCost[i][j - m_first[i]];
|
Chris@87
|
240 } else {
|
Chris@87
|
241 return m_otherMatcher->getPathCost(j, i);
|
Chris@87
|
242 }
|
Chris@87
|
243 }
|
Chris@87
|
244
|
Chris@87
|
245 void
|
Chris@182
|
246 Matcher::setPathCost(int i, int j, advance_t dir, pathcost_t pathCost)
|
Chris@87
|
247 {
|
Chris@87
|
248 if (m_firstPM) {
|
Chris@87
|
249 if (!isInRange(i, j)) {
|
Chris@87
|
250 cerr << "ERROR: Matcher::setPathCost(" << i << ", " << j << ", "
|
Chris@87
|
251 << dir << ", " << pathCost
|
Chris@87
|
252 << "): Location is out of range" << endl;
|
Chris@87
|
253 throw "Indices out of range";
|
Chris@87
|
254 }
|
Chris@87
|
255 m_advance[i][j - m_first[i]] = dir;
|
Chris@87
|
256 m_bestPathCost[i][j - m_first[i]] = pathCost;
|
Chris@87
|
257 } else {
|
Chris@87
|
258 if (dir == AdvanceThis) {
|
Chris@87
|
259 dir = AdvanceOther;
|
Chris@87
|
260 } else if (dir == AdvanceOther) {
|
Chris@87
|
261 dir = AdvanceThis;
|
Chris@87
|
262 }
|
Chris@87
|
263 m_otherMatcher->setPathCost(j, i, dir, pathCost);
|
Chris@87
|
264 }
|
Chris@87
|
265 }
|
Chris@87
|
266
|
cannam@0
|
267 void
|
Chris@41
|
268 Matcher::size()
|
cannam@0
|
269 {
|
Chris@43
|
270 m_first.resize(m_distXSize, 0);
|
Chris@43
|
271 m_last.resize(m_distXSize, 0);
|
Chris@206
|
272
|
Chris@206
|
273 if (m_firstPM) {
|
Chris@206
|
274 int distSize = (m_params.maxRunCount + 1) * m_blockSize;
|
Chris@206
|
275 m_bestPathCost.resize(m_distXSize, pathcostvec_t(distSize, InvalidPathCost));
|
Chris@206
|
276 m_distance.resize(m_distXSize, distancevec_t(distSize, InvalidDistance));
|
Chris@206
|
277 m_advance.resize(m_distXSize, advancevec_t(distSize, AdvanceNone));
|
Chris@206
|
278 }
|
Chris@38
|
279 }
|
cannam@0
|
280
|
Chris@23
|
281 void
|
Chris@182
|
282 Matcher::consumeFeatureVector(const feature_t &feature)
|
Chris@23
|
283 {
|
Chris@43
|
284 if (!m_initialised) init();
|
Chris@43
|
285 int frameIndex = m_frameCount % m_blockSize;
|
Chris@182
|
286 m_features[frameIndex] = feature;
|
Chris@23
|
287 calcAdvance();
|
Chris@21
|
288 }
|
Chris@21
|
289
|
Chris@21
|
290 void
|
Chris@21
|
291 Matcher::calcAdvance()
|
Chris@21
|
292 {
|
Chris@43
|
293 int frameIndex = m_frameCount % m_blockSize;
|
Chris@21
|
294
|
Chris@43
|
295 if (m_frameCount >= m_distXSize) {
|
Chris@43
|
296 m_distXSize *= 2;
|
Chris@41
|
297 size();
|
cannam@0
|
298 }
|
cannam@0
|
299
|
Chris@43
|
300 if (m_firstPM && (m_frameCount >= m_blockSize)) {
|
cannam@0
|
301
|
Chris@43
|
302 int len = m_last[m_frameCount - m_blockSize] -
|
Chris@43
|
303 m_first[m_frameCount - m_blockSize];
|
cannam@0
|
304
|
Chris@43
|
305 // We need to copy distance[m_frameCount-m_blockSize] to
|
Chris@43
|
306 // distance[m_frameCount], and then truncate
|
Chris@43
|
307 // distance[m_frameCount-m_blockSize] to its first len elements.
|
cannam@0
|
308 // Same for bestPathCost.
|
cannam@0
|
309
|
Chris@182
|
310 distancevec_t dOld(m_distance[m_frameCount - m_blockSize]);
|
Chris@183
|
311 distancevec_t dNew(len, InvalidDistance);
|
cannam@0
|
312
|
Chris@182
|
313 pathcostvec_t bpcOld(m_bestPathCost[m_frameCount - m_blockSize]);
|
Chris@183
|
314 pathcostvec_t bpcNew(len, InvalidPathCost);
|
Chris@69
|
315
|
Chris@182
|
316 advancevec_t adOld(m_advance[m_frameCount - m_blockSize]);
|
Chris@182
|
317 advancevec_t adNew(len, AdvanceNone);
|
Chris@69
|
318
|
Chris@69
|
319 for (int i = 0; i < len; ++i) {
|
Chris@69
|
320 dNew[i] = dOld[i];
|
Chris@69
|
321 bpcNew[i] = bpcOld[i];
|
Chris@69
|
322 adNew[i] = adOld[i];
|
Chris@69
|
323 }
|
Chris@45
|
324
|
Chris@69
|
325 m_distance[m_frameCount] = dOld;
|
Chris@69
|
326 m_distance[m_frameCount - m_blockSize] = dNew;
|
Chris@69
|
327
|
Chris@69
|
328 m_bestPathCost[m_frameCount] = bpcOld;
|
Chris@69
|
329 m_bestPathCost[m_frameCount - m_blockSize] = bpcNew;
|
Chris@69
|
330
|
Chris@69
|
331 m_advance[m_frameCount] = adOld;
|
Chris@69
|
332 m_advance[m_frameCount - m_blockSize] = adNew;
|
cannam@0
|
333 }
|
cannam@0
|
334
|
Chris@43
|
335 int stop = m_otherMatcher->m_frameCount;
|
Chris@43
|
336 int index = stop - m_blockSize;
|
Chris@86
|
337 if (index < 0) index = 0;
|
Chris@86
|
338
|
Chris@43
|
339 m_first[m_frameCount] = index;
|
Chris@43
|
340 m_last[m_frameCount] = stop;
|
cannam@0
|
341
|
cannam@0
|
342 for ( ; index < stop; index++) {
|
Chris@26
|
343
|
Chris@188
|
344 distance_t distance = m_metric.calcDistance
|
Chris@182
|
345 (m_features[frameIndex],
|
Chris@182
|
346 m_otherMatcher->m_features[index % m_blockSize]);
|
Chris@26
|
347
|
Chris@188
|
348 pathcost_t straightIncrement(distance);
|
Chris@188
|
349 pathcost_t diagIncrement = pathcost_t(distance * m_params.diagonalWeight);
|
Chris@89
|
350
|
Chris@86
|
351 if ((m_frameCount == 0) && (index == 0)) { // first element
|
Chris@86
|
352
|
Chris@86
|
353 updateValue(0, 0, AdvanceNone,
|
Chris@86
|
354 0,
|
Chris@86
|
355 distance);
|
Chris@86
|
356
|
Chris@86
|
357 } else if (m_frameCount == 0) { // first row
|
Chris@86
|
358
|
Chris@71
|
359 updateValue(0, index, AdvanceOther,
|
Chris@86
|
360 getPathCost(0, index-1),
|
Chris@86
|
361 distance);
|
Chris@86
|
362
|
Chris@86
|
363 } else if (index == 0) { // first column
|
Chris@86
|
364
|
Chris@71
|
365 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
366 getPathCost(m_frameCount - 1, 0),
|
Chris@86
|
367 distance);
|
Chris@86
|
368
|
Chris@86
|
369 } else if (index == m_otherMatcher->m_frameCount - m_blockSize) {
|
Chris@86
|
370
|
cannam@0
|
371 // missing value(s) due to cutoff
|
cannam@0
|
372 // - no previous value in current row (resp. column)
|
cannam@0
|
373 // - no diagonal value if prev. dir. == curr. dirn
|
Chris@86
|
374
|
Chris@182
|
375 pathcost_t min2 = getPathCost(m_frameCount - 1, index);
|
Chris@86
|
376
|
Chris@91
|
377 // cerr << "NOTE: missing value at i = " << m_frameCount << ", j = "
|
Chris@91
|
378 // << index << " (first = " << m_firstPM << ")" << endl;
|
Chris@86
|
379
|
Chris@43
|
380 // if ((m_firstPM && (first[m_frameCount - 1] == index)) ||
|
Chris@43
|
381 // (!m_firstPM && (m_last[index-1] < m_frameCount)))
|
Chris@86
|
382 if (m_first[m_frameCount - 1] == index) {
|
Chris@86
|
383
|
Chris@86
|
384 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
385 min2, distance);
|
Chris@86
|
386
|
Chris@86
|
387 } else {
|
Chris@86
|
388
|
Chris@182
|
389 pathcost_t min1 = getPathCost(m_frameCount - 1, index - 1);
|
Chris@188
|
390 if (min1 + diagIncrement <= min2 + straightIncrement) {
|
Chris@86
|
391 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
392 min1, distance);
|
Chris@86
|
393 } else {
|
Chris@86
|
394 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
395 min2, distance);
|
Chris@86
|
396 }
|
cannam@0
|
397 }
|
Chris@86
|
398
|
cannam@0
|
399 } else {
|
Chris@86
|
400
|
Chris@182
|
401 pathcost_t min1 = getPathCost(m_frameCount, index - 1);
|
Chris@182
|
402 pathcost_t min2 = getPathCost(m_frameCount - 1, index);
|
Chris@182
|
403 pathcost_t min3 = getPathCost(m_frameCount - 1, index - 1);
|
Chris@87
|
404
|
Chris@188
|
405 pathcost_t cost1 = min1 + straightIncrement;
|
Chris@188
|
406 pathcost_t cost2 = min2 + straightIncrement;
|
Chris@188
|
407 pathcost_t cost3 = min3 + diagIncrement;
|
Chris@93
|
408
|
Chris@93
|
409 // Choosing is easy if there is a strict cheapest of the
|
Chris@93
|
410 // three. If two or more share the lowest cost, we choose
|
Chris@93
|
411 // in order of preference: cost3 (AdvanceBoth), cost2
|
Chris@94
|
412 // (AdvanceThis), cost1 (AdvanceOther) if we are the first
|
Chris@94
|
413 // matcher; and cost3 (AdvanceBoth), cost1 (AdvanceOther),
|
Chris@94
|
414 // cost2 (AdvanceThis) if we are the second matcher. That
|
Chris@94
|
415 // is, we always prioritise the diagonal followed by the
|
Chris@94
|
416 // first matcher.
|
Chris@94
|
417
|
Chris@94
|
418 if (( m_firstPM && (cost1 < cost2)) ||
|
Chris@94
|
419 (!m_firstPM && (cost1 <= cost2))) {
|
Chris@89
|
420 if (cost3 <= cost1) {
|
Chris@86
|
421 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
422 min3, distance);
|
Chris@86
|
423 } else {
|
Chris@86
|
424 updateValue(m_frameCount, index, AdvanceOther,
|
Chris@86
|
425 min1, distance);
|
Chris@86
|
426 }
|
cannam@0
|
427 } else {
|
Chris@89
|
428 if (cost3 <= cost2) {
|
Chris@86
|
429 updateValue(m_frameCount, index, AdvanceBoth,
|
Chris@86
|
430 min3, distance);
|
Chris@86
|
431 } else {
|
Chris@86
|
432 updateValue(m_frameCount, index, AdvanceThis,
|
Chris@86
|
433 min2, distance);
|
Chris@86
|
434 }
|
cannam@0
|
435 }
|
cannam@0
|
436 }
|
Chris@86
|
437
|
Chris@43
|
438 m_otherMatcher->m_last[index]++;
|
cannam@0
|
439 } // loop for row (resp. column)
|
cannam@0
|
440
|
Chris@43
|
441 m_frameCount++;
|
Chris@43
|
442 m_runCount++;
|
cannam@0
|
443
|
Chris@43
|
444 m_otherMatcher->m_runCount = 0;
|
Chris@21
|
445 }
|
cannam@0
|
446
|
cannam@0
|
447 void
|
Chris@182
|
448 Matcher::updateValue(int i, int j, advance_t dir, pathcost_t value, distance_t distance)
|
cannam@0
|
449 {
|
Chris@188
|
450 pathcost_t increment = distance;
|
Chris@83
|
451 if (dir == AdvanceBoth) {
|
Chris@188
|
452 increment = pathcost_t(increment * m_params.diagonalWeight);
|
Chris@188
|
453 }
|
Chris@188
|
454
|
Chris@188
|
455 pathcost_t newValue = value + increment;
|
Chris@188
|
456 if (MaxPathCost - increment < value) {
|
Chris@188
|
457 cerr << "ERROR: Path cost overflow at i=" << i << ", j=" << j << ": "
|
Chris@188
|
458 << value << " + " << increment << " > " << MaxPathCost << endl;
|
Chris@188
|
459 newValue = MaxPathCost;
|
Chris@83
|
460 }
|
Chris@83
|
461
|
Chris@43
|
462 if (m_firstPM) {
|
Chris@45
|
463
|
Chris@96
|
464 setDistance(i, j, distance);
|
Chris@188
|
465 setPathCost(i, j, dir, newValue);
|
Chris@45
|
466
|
cannam@0
|
467 } else {
|
Chris@45
|
468
|
Chris@86
|
469 if (dir == AdvanceThis) dir = AdvanceOther;
|
Chris@86
|
470 else if (dir == AdvanceOther) dir = AdvanceThis;
|
Chris@84
|
471
|
Chris@43
|
472 int idx = i - m_otherMatcher->m_first[j];
|
Chris@45
|
473
|
Chris@188
|
474 if (idx < 0 || size_t(idx) == m_otherMatcher->m_distance[j].size()) {
|
cannam@0
|
475 // This should never happen, but if we allow arbitrary
|
cannam@0
|
476 // pauses in either direction, and arbitrary lengths at
|
cannam@0
|
477 // end, it is better than a segmentation fault.
|
Chris@72
|
478 cerr << "Emergency resize: " << idx << " -> " << idx * 2 << endl;
|
Chris@183
|
479 m_otherMatcher->m_bestPathCost[j].resize(idx * 2, InvalidPathCost);
|
Chris@183
|
480 m_otherMatcher->m_distance[j].resize(idx * 2, InvalidDistance);
|
Chris@46
|
481 m_otherMatcher->m_advance[j].resize(idx * 2, AdvanceNone);
|
cannam@0
|
482 }
|
Chris@45
|
483
|
Chris@96
|
484 m_otherMatcher->setDistance(j, i, distance);
|
Chris@188
|
485 m_otherMatcher->setPathCost(j, i, dir, newValue);
|
cannam@0
|
486 }
|
Chris@71
|
487 }
|
cannam@0
|
488
|
Chris@181
|
489 advance_t
|
Chris@72
|
490 Matcher::getAdvance(int i, int j)
|
Chris@72
|
491 {
|
Chris@72
|
492 if (m_firstPM) {
|
Chris@72
|
493 if (!isInRange(i, j)) {
|
Chris@72
|
494 cerr << "ERROR: Matcher::getAdvance(" << i << ", " << j << "): "
|
Chris@72
|
495 << "Location is not in range" << endl;
|
Chris@72
|
496 throw "Advance not available";
|
Chris@72
|
497 }
|
Chris@72
|
498 return m_advance[i][j - m_first[i]];
|
Chris@72
|
499 } else {
|
Chris@72
|
500 return m_otherMatcher->getAdvance(j, i);
|
Chris@72
|
501 }
|
Chris@72
|
502 }
|
Chris@202
|
503
|
Chris@202
|
504 static double k(size_t sz)
|
Chris@202
|
505 {
|
Chris@202
|
506 return double(sz) / 1024.0;
|
Chris@202
|
507 }
|
Chris@202
|
508
|
Chris@202
|
509 void
|
Chris@202
|
510 Matcher::printStats()
|
Chris@202
|
511 {
|
Chris@202
|
512 if (m_firstPM) cerr << endl;
|
Chris@202
|
513
|
Chris@202
|
514 cerr << "Matcher[" << this << "] (" << (m_firstPM ? "first" : "second") << "):" << endl;
|
Chris@202
|
515 cerr << "- block size " << m_blockSize << ", frame count " << m_frameCount << ", dist x size " << m_distXSize << ", initialised " << m_initialised << endl;
|
Chris@202
|
516
|
Chris@202
|
517 if (m_features.empty()) {
|
Chris@202
|
518 cerr << "- have no features yet" << endl;
|
Chris@202
|
519 } else {
|
Chris@202
|
520 cerr << "- have " << m_features.size() << " features of " << m_features[0].size() << " bins each (= "
|
Chris@202
|
521 << k(m_features.size() * m_features[0].size() * sizeof(featurebin_t)) << "K)" << endl;
|
Chris@202
|
522 }
|
Chris@202
|
523
|
Chris@202
|
524 size_t cells = 0;
|
Chris@202
|
525 for (const auto &d: m_distance) {
|
Chris@202
|
526 cells += d.size();
|
Chris@202
|
527 }
|
Chris@202
|
528 if (m_distance.empty()) {
|
Chris@202
|
529 cerr << "- have no cells in matrix" << endl;
|
Chris@202
|
530 } else {
|
Chris@202
|
531 cerr << "- have " << m_distance.size() << " cols in matrix with avg "
|
Chris@203
|
532 << double(cells) / double(m_distance.size()) << " rows, total "
|
Chris@202
|
533 << cells << " cells" << endl;
|
Chris@202
|
534 cerr << "- path costs " << k(cells * sizeof(pathcost_t))
|
Chris@202
|
535 << "K, distances " << k(cells * sizeof(distance_t))
|
Chris@202
|
536 << "K, advances " << k(cells * sizeof(advance_t)) << "K" << endl;
|
Chris@202
|
537 }
|
Chris@202
|
538
|
Chris@202
|
539 if (m_firstPM && m_otherMatcher) {
|
Chris@202
|
540 m_otherMatcher->printStats();
|
Chris@202
|
541 cerr << endl;
|
Chris@202
|
542 }
|
Chris@202
|
543 }
|
Chris@202
|
544
|