Commit f2d87ed5c3809192d36366452b36905e1c63ff59

Authored by Charles Otto
1 parent a8340241

Proper support for usign the same Stream transform more than once

Showing 1 changed file with 65 additions and 4 deletions
openbr/plugins/stream.cpp
... ... @@ -162,6 +162,7 @@ public:
162 162 {
163 163 final_frame = -1;
164 164 last_issued = -2;
  165 + last_received = -3;
165 166 for (int i=0; i < maxFrames;i++)
166 167 {
167 168 allFrames.addItem(new FrameData());
... ... @@ -194,30 +195,43 @@ public:
194 195 // The datasource broke.
195 196 if (!res) {
196 197 allFrames.addItem(aFrame);
197   - QMutexLocker lock(&last_frame_update);
198 198  
  199 + QMutexLocker lock(&last_frame_update);
  200 + // Did we already receive the last frame?
199 201 final_frame = last_issued;
200   - if (final_frame == last_received)
  202 +
  203 + // We got the last frame before the data source broke,
  204 + // better pulse lastReturned
  205 + if (final_frame == last_received) {
201 206 lastReturned.wakeAll();
  207 + }
202 208 else if (final_frame < last_received)
203 209 std::cout << "Bad last frame " << final_frame << " but received " << last_received << std::endl;
  210 +
204 211 return NULL;
205 212 }
206 213 last_issued = aFrame->sequenceNumber;
207 214 return aFrame;
208 215 }
209 216  
  217 + // Returns true if the frame returned was the last
  218 + // frame issued, false otherwise
210 219 bool returnFrame(FrameData * inputFrame)
211 220 {
212 221 allFrames.addItem(inputFrame);
213 222  
  223 + bool rval = false;
  224 +
214 225 QMutexLocker lock(&last_frame_update);
215 226 last_received = inputFrame->sequenceNumber;
  227 +
216 228 if (inputFrame->sequenceNumber == final_frame) {
  229 + // We just received the last frame, better pulse
217 230 lastReturned.wakeAll();
  231 + rval = true;
218 232 }
219 233  
220   - return this->final_frame != -1;
  234 + return rval;
221 235 }
222 236  
223 237 void waitLast()
... ... @@ -252,6 +266,7 @@ public:
252 266 {
253 267 final_frame = -1;
254 268 last_issued = -2;
  269 + last_received = -3;
255 270  
256 271 next_idx = 0;
257 272 basis = input;
... ... @@ -312,6 +327,7 @@ public:
312 327 next_sequence = 0;
313 328 final_frame = -1;
314 329 last_issued = -2;
  330 + last_received = -3;
315 331  
316 332 data_ok = current_idx < basis.size();
317 333 return data_ok;
... ... @@ -380,6 +396,7 @@ public:
380 396 bool open_res = false;
381 397 final_frame = -1;
382 398 last_issued = -2;
  399 + last_received = -3;
383 400  
384 401 // Input has no matrices? Its probably a video that hasn't been loaded yet
385 402 if (input.empty()) {
... ... @@ -439,6 +456,8 @@ public:
439 456  
440 457 int stage_id;
441 458  
  459 + virtual void reset()=0;
  460 +
442 461 protected:
443 462 int thread_count;
444 463  
... ... @@ -490,6 +509,11 @@ public:
490 509 (void) input;
491 510 return true;
492 511 }
  512 +
  513 + void reset()
  514 + {
  515 + // nothing to do.
  516 + }
493 517 };
494 518  
495 519  
... ... @@ -512,6 +536,14 @@ public:
512 536 delete inputBuffer;
513 537 }
514 538  
  539 + void reset()
  540 + {
  541 + QWriteLocker writeLock(&statusLock);
  542 + currentStatus = STOPPING;
  543 + next_target = 0;
  544 + }
  545 +
  546 +
515 547 int next_target;
516 548 enum Status
517 549 {
... ... @@ -631,8 +663,11 @@ public:
631 663 // Calledfrom a different thread than run.
632 664 bool tryAcquireNextStage(FrameData *& input)
633 665 {
634   - dataSource.returnFrame(input);
  666 + bool was_last = dataSource.returnFrame(input);
635 667 input = NULL;
  668 + if (was_last) {
  669 + return false;
  670 + }
636 671  
637 672 if (!dataSource.isOpen())
638 673 return false;
... ... @@ -673,6 +708,12 @@ public:
673 708 private:
674 709 TemplateList collectedOutput;
675 710 public:
  711 + void reset()
  712 + {
  713 + collectedOutput.clear();
  714 + SingleThreadStage::reset();
  715 + }
  716 +
676 717 FrameData * run(FrameData * input, bool & should_continue)
677 718 {
678 719 if (input == NULL) {
... ... @@ -767,8 +808,28 @@ public:
767 808 readStage->dataSource.waitLast();
768 809 QThreadPool::globalInstance()->reserveThread();
769 810  
  811 + TemplateList final_output;
  812 +
  813 + // Push finalize through the stages
  814 + for (int i=0; i < this->transforms.size(); i++)
  815 + {
  816 + TemplateList output_set;
  817 + transforms[i]->finalize(output_set);
  818 +
  819 + for (int j=i+1; j < transforms.size();j++)
  820 + {
  821 + transforms[j]->projectUpdate(output_set);
  822 + }
  823 + final_output.append(output_set);
  824 + }
  825 +
770 826 // dst is set to all output received by the final stage
771 827 dst = collectionStage->getOutput();
  828 + dst.append(final_output);
  829 +
  830 + foreach(ProcessingStage * stage, processingStages) {
  831 + stage->reset();
  832 + }
772 833 }
773 834  
774 835 virtual void finalize(TemplateList & output)
... ...