-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLinearStackCompressor.cpp
More file actions
345 lines (285 loc) · 9.76 KB
/
LinearStackCompressor.cpp
File metadata and controls
345 lines (285 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* File: LinearStackCompressor.cpp
* Author: Marc
*
* Created on October 24, 2010, 3:29 PM
*
* (C) Marc Gershow; licensed under the Creative Commons Attribution Share Alike 3.0 United States License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
* Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
*
*/
#include "LinearStackCompressor.h"
#include "tictoc/Timer.h"
#include <vector>
#include <string>
#include <sstream>
#include <queue>
using namespace std;
LinearStackCompressor::LinearStackCompressor() {
init();
}
LinearStackCompressor::LinearStackCompressor(const LinearStackCompressor& orig) {
}
LinearStackCompressor::~LinearStackCompressor() {
closeOutputFile();
while (!imageStacks.empty()) {
delete imageStacks.front();
imageStacks.pop();
}
while (!compressedStacks.empty()) {
delete compressedStacks.front();
compressedStacks.pop();
}
while (!stacksToWrite.empty()) {
delete stacksToWrite.front();
stacksToWrite.pop();
}
if (activeStack != NULL) {
delete activeStack;
}
}
void LinearStackCompressor::init() {
keyframeInterval = 64;
backgroundUpdateInterval = 1;
recordingState = idle;
framesToRecord = 0;
outfile = NULL;
activeStack = NULL;
stackBeingCompressed = NULL;
stackBeingWritten = NULL;
frameRate = 30;
threshBelowBackground = 5;
threshAboveBackground = 5;
lgDimMinSize = 2;
smallDimMinSize = 3;
processing = false; //really should be a mutex, but whatever
lockActiveStack = false; //really should be a mutex, but whatever
currentFileSize = 0;
numStacksToMerge = 12;
memoryUsedByCompressedStacks = 0;
}
void LinearStackCompressor::newFrame(const IplImage* im, ImageMetaData *metadata) {
int maxCycles = (int) 1E9; //just so it doesn't hang
Timer tim = Timer();
tim.start();
IplImage *imcpy = cvCloneImage(im);
while (lockActiveStack && --maxCycles > 0) {
//intentionally blank
}
// cout << "lockActiveStack = " << lockActiveStack << "; processing = " << processing << "\n";
lockActiveStack = true;
addFrameToStack(&imcpy, metadata);
lockActiveStack = false;
if (recordingState == recording) {
if(--framesToRecord <= 0) {
finishRecording();
return;
}
}
if (!processing) {
processing = true;
while (tim.getElapsedTimeInSec() < 0.95/frameRate && compressStack()) {
//intentionally blank
}
while (tim.getElapsedTimeInSec() < 0.95/frameRate && writeFinishedStack()) {
//intentionally blank
}
processing = false;
}
}
void LinearStackCompressor::addFrameToStack(IplImage **im, ImageMetaData *metadata) {
if (activeStack == NULL) {
createStack();
}
if (activeStack->numToProccess() >= keyframeInterval) {
imageStacks.push(activeStack);
createStack();
}
if (recordingState == recording) {
activeStack->addFrame(im, metadata);
} else {
if (recordingState == updatingBackground) {
activeStack->updateBackground(*im);
}
if (metadata != NULL) {
delete metadata;
}
cvReleaseImage(im);
}
}
void LinearStackCompressor::startRecording(int numFramesToRecord) {
recordingState = recording;
this->framesToRecord = numFramesToRecord;
}
void LinearStackCompressor::finishRecording() {
int maxCycles = (int) 1E9; //just so it doesn't hang
while (processing && --maxCycles > 0) {
//intentionally blank
}
processing = true;
lockActiveStack = true;
recordingState = idle;
if (activeStack != NULL) {
if (activeStack->numToProccess() > 0) {
imageStacks.push(activeStack);
} else {
delete activeStack;
}
activeStack = NULL;
}
while (compressStack()) {
//intentionally blank
}
while (writeFinishedStack()) {
//intentionally blank
}
processing = false;
lockActiveStack = false;
}
void LinearStackCompressor::createStack() {
activeStack = new StaticBackgroundCompressor();
activeStack->setAutomaticUpdateInterval(backgroundUpdateInterval);
activeStack->setThresholds(threshBelowBackground, threshAboveBackground, smallDimMinSize, lgDimMinSize);
}
//returns true if there may be images remaining to compress
bool LinearStackCompressor::compressStack() {
setCompressionStack();
if (stackBeingCompressed != NULL) {
stackBeingCompressed->processFrame();
return true;
}
return false;
}
void LinearStackCompressor::numStacksWaiting(int& numToCompress, int& numToWrite) {
numToCompress = imageStacks.size();
numToWrite = compressedStacks.size() + stacksToWrite.size();
}
void LinearStackCompressor::setCompressionStack() {
if (stackBeingCompressed != NULL && stackBeingCompressed->numToProccess() <= 0) {
compressedStacks.push(stackBeingCompressed);
memoryUsedByCompressedStacks += stackBeingCompressed->sizeInMemory();
stackBeingCompressed = NULL;
}
if (stackBeingCompressed == NULL && !imageStacks.empty()) {
stackBeingCompressed = imageStacks.front();
imageStacks.pop();
}
}
//returns true if there may be stacks remaining to write
bool LinearStackCompressor::writeFinishedStack() {
mergeCompressedStacks();
setWritingStack();
if (stackBeingWritten != NULL) {
if (outfile == NULL) {
openOutputFile();
}
if (outfile == NULL) {
return false;
}
if (stacksavedescription.empty()) {
stacksavedescription = stackBeingWritten->saveDescription();
}
stackBeingWritten->toDisk(*outfile);
currentFileSize = outfile->tellp();
delete stackBeingWritten;
stackBeingWritten = NULL;
return true;
}
return false;
}
ofstream::pos_type LinearStackCompressor::numBytesWritten() {
return currentFileSize;
}
void LinearStackCompressor::openOutputFile() {
if (outfile != NULL) {
LinearStackCompressor::closeOutputFile();
}
if (!fname.empty()) {
outfile = new ofstream (fname.c_str(),ofstream::binary);
writeHeader();
}
}
void LinearStackCompressor::closeOutputFile() {
if (outfile != NULL) {
outfile->seekp(0);
writeHeader(); //now that we have an updated save description
outfile->close();
delete outfile;
outfile = NULL;
}
}
void LinearStackCompressor::setOutputFileName(const char* fname) {
if (fname != NULL) {
this->fname = string(fname);
}
}
void LinearStackCompressor::stopRecording() {
//recordingState = idle;
finishRecording();
}
void LinearStackCompressor::goIdle() {
stopRecording();
}
void LinearStackCompressor::startUpdatingBackground() {
recordingState = updatingBackground;
}
void LinearStackCompressor::writeHeader() {
if (outfile == NULL) {
return;
}
char zero[headerSizeInBytes] = {0};
ofstream::pos_type cloc = outfile->tellp();
outfile->write(zero, headerSizeInBytes);
ofstream::pos_type eloc = outfile->tellp();
outfile->seekp(cloc);
string sd = saveDescription();
outfile->write(sd.c_str(), sd.length() + 1);
uint32_t idcode = idCode();
outfile->write((char *) &idcode, sizeof(idcode));
int info[10] = {0};
info[0] = headerSizeInBytes;
info[1] = keyframeInterval;
info[2] = threshBelowBackground;
info[3] = threshAboveBackground;
outfile->write((char *) info, sizeof(info));
outfile->seekp(eloc);
}
string LinearStackCompressor::headerDescription() {
stringstream os;
os << headerSizeInBytes << " byte zero padded header beginning with a textual description of the file, followed by \\0 then the following fields (all ints, except idcode)\n";
os << sizeof(uint32_t) << " byte uint32_t idcode = " << hex << idCode() << dec << ", header size in bytes, key frame interval, threshold below background, threshold above background\n";
return os.str();
}
string LinearStackCompressor::saveDescription() {
stringstream os;
os << "Set of Image Stacks representing a movie. Beginning of file is a header, with this format:\n" << headerDescription();
os << "Header is followed by a set of common background image stacks, with the following format:\n" << stacksavedescription;
return os.str();
}
void LinearStackCompressor::mergeCompressedStacks() {
if ((recordingState != recording && !compressedStacks.empty()) || compressedStacks.size() >= numStacksToMerge) {
cout << "merging stacks " << endl << flush;
StaticBackgroundCompressor *sbc = compressedStacks.front();
compressedStacks.pop();
vector<StaticBackgroundCompressor *> stacksToMerge;
for (int j = 1; !compressedStacks.empty() && j < numStacksToMerge; ++j) {
stacksToMerge.push_back(compressedStacks.front());
compressedStacks.pop();
}
memoryUsedByCompressedStacks *= (compressedStacks.size() / (compressedStacks.size() + numStacksToMerge)); //estimates, in the unusual case compressedStacks wasn't emptied
sbc->mergeStacks(stacksToMerge);
for (vector<StaticBackgroundCompressor *>::iterator it = stacksToMerge.begin(); it != stacksToMerge.end(); ++it) {
delete (*it);
*it = NULL;
}
stacksToWrite.push(sbc);
cout << "done merging stacks " << endl << flush;
}
}
void LinearStackCompressor::setWritingStack() {
if (stackBeingWritten == NULL && !stacksToWrite.empty()) {
stackBeingWritten = stacksToWrite.front();
stacksToWrite.pop();
}
}