-
Notifications
You must be signed in to change notification settings - Fork 3
/
VideoProcessor.cpp
325 lines (263 loc) · 6.88 KB
/
VideoProcessor.cpp
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
#include "VideoProcessor.h"
#include "impressionistUI.h"
#include "impressionistDoc.h"
#include "bitmap.h"
#include <regex>
VideoProcessor* VideoProcessor::singletonPtr = nullptr;
ImpressionistUI* VideoProcessor::uiPtr = nullptr;
ImpressionistDoc* VideoProcessor::docPtr = nullptr;
std::function<void()> VideoProcessor::methodAutoFill = []()->void
{
uiPtr->m_paintView->willAutoFill = true;
uiPtr->m_paintView->draw();
continueWriteStream();
};
std::function<void()> VideoProcessor::methodPaintly = []()->void
{
uiPtr->m_paintView->willPainterly = true;
uiPtr->m_paintView->draw();
continueWriteStream();
};
VideoProcessor::VideoProcessor()
{
AVIFileInit(); // Must be called to use AVI library
}
void VideoProcessor::saveImage()
{
//Save image to stream
auto *t = docPtr->m_ucPainting;
reverseMapColor(t, bmpInfo.biWidth, bmpInfo.biHeight, cinepak_color_mapping);
repackBmp(t, bmpInfo.biWidth, bmpInfo.biHeight, padding.pad);
errorCode = AVIStreamWrite(aviCStreamPtr, streamStartingIndex + frameIndex, 1, t, byteLength, 0, nullptr, nullptr);
if (errorCode == AVIERR_UNSUPPORTED)
{
fl_alert("Compression method unsupported. But this should never happen.");
close();
}
else if (errorCode == AVIERR_NOCOMPRESSOR)
{
fl_alert("A suitable compressor cannot be found.");
close();
}
else if (errorCode != AVIERR_OK)
{
fl_alert("Cannot write data to stream.");
close();
}
frameIndex++;
next();
}
void VideoProcessor::openVideoFromUser()
{
// ICINFO info{};
//
// ICInfo(streamtypeVIDEO, mmioFOURCC('C', 'V', 'I', 'D'), &info);
auto* path = fl_file_chooser("Open AVI", "*.avi", "");
if (path == nullptr)
{
fl_alert("Path is not chosen!");
return;
}
openFile(path);
if (errorCode)
{
fl_alert("Error opening file.");
return;
}
startStream();
if (errorCode) return;
next();
}
void VideoProcessor::cbVideoAutoFill(Fl_Widget* o, void* v)
{
getInstancePtr()->setManipulationMethod(methodAutoFill);
cbPreparation(o);
}
void VideoProcessor::cbVideoPaintly(Fl_Widget* o, void* v)
{
getInstancePtr()->setManipulationMethod(methodPaintly);
cbPreparation(o);
}
void VideoProcessor::cbPreparation(Fl_Widget* o)
{
uiPtr = static_cast<ImpressionistUI*>(o->parent()->user_data());
docPtr = uiPtr->getDocument();
auto selfPtr = getInstancePtr();
selfPtr->openVideoFromUser();
}
void VideoProcessor::continueWriteStream()
{
if (singletonPtr != nullptr && uiPtr && docPtr && !getInstancePtr()->isEnded())
{
getInstancePtr()->saveImage();
}
}
VideoProcessor::~VideoProcessor()
{
close();
AVIFileExit();
}
bool VideoProcessor::openFile(const std::string& filename)
{
const auto& newFilename = std::regex_replace(filename, std::regex("\\.avi"), "_new.avi");;
// const bool copySucceeded = CopyFile(filename.c_str(), newFilename.c_str(), false);
//
// if (!copySucceeded)
// {
// fl_alert("Save file cannot be copied.");
// }
errorCode = AVIFileOpen(&aviReadPtr, filename.c_str(), OF_READ, nullptr);
errorCode = AVIFileOpen(&aviPtr, newFilename.c_str(), OF_CREATE|OF_WRITE, nullptr);
if (errorCode != AVIERR_OK)
{
AVIFileRelease(aviReadPtr);
AVIFileRelease(aviPtr);
}
else
{
AVIFileInfo(aviReadPtr, &aviInfo, sizeof(AVIFILEINFO));
}
return errorCode;
}
bool VideoProcessor::startStream()
{
errorCode = AVIFileGetStream(aviReadPtr, &aviReadStreamPtr, streamtypeVIDEO, 0);
if (errorCode != AVIERR_OK)
{
fl_alert("Unable to get stream from video.");
close();
}
streamStartingIndex = AVIStreamStart(aviReadStreamPtr);
totalFrames = AVIStreamLength(aviReadStreamPtr);
if (streamStartingIndex == -1 || totalFrames == -1)
{
fl_alert("Unable to start stream from video!");
close();
errorCode = -1;
}
AVISTREAMINFO info;
AVIStreamInfo(aviReadStreamPtr, &info, sizeof(AVISTREAMINFO));
info.dwQuality = 10000;
info.fccHandler = mmioFOURCC('c','v','i','d');
info.dwLength = 0;
errorCode = AVIFileCreateStream(aviPtr, &aviStreamPtr, &info);
AVICOMPRESSOPTIONS compressOptions{};
ZeroMemory(&compressOptions, sizeof(AVICOMPRESSOPTIONS));
compressOptions.fccType = streamtypeVIDEO;
compressOptions.fccHandler = info.fccHandler;
compressOptions.dwFlags = AVICOMPRESSF_KEYFRAMES | AVICOMPRESSF_VALID;
compressOptions.dwKeyFrameEvery = 1;
errorCode = AVIMakeCompressedStream(&aviCStreamPtr, aviStreamPtr, &compressOptions, nullptr);
if (errorCode)
{
fl_alert("Codec unsupported.");
return errorCode;
}
getFramePtr = AVIStreamGetFrameOpen(aviReadStreamPtr, nullptr); //Prepares to get frame
if (getFramePtr == nullptr)
{
fl_alert("Video codec is too new for VFW!");
close();
errorCode = -1;
}
return errorCode;
}
bool VideoProcessor::hasError() const
{
return errorCode != 0;
}
void VideoProcessor::setManipulationMethod(const std::function<void()>& callbackFunction)
{
this->perImageFunction = callbackFunction;
}
void VideoProcessor::next()
{
if (isEnded())
{
docPtr->clearCanvas();
fl_alert("Conversion done!");
close();
destroy();
return;
}
bitmapDibPtr = static_cast<BYTE*>(AVIStreamGetFrame(getFramePtr, streamStartingIndex + frameIndex));
//Process bitmap DIB
memcpy(&bmpInfo.biSize, bitmapDibPtr, sizeof(BITMAPINFOHEADER));
if (!hasSetFormat)
{
AVIStreamSetFormat(aviCStreamPtr, 0, &bmpInfo, bmpInfo.biSize);
}
const auto offset = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
padding = calculatePadding(bmpInfo.biWidth);
byteLength = bmpInfo.biHeight * padding.padWidth;
imageDataPtr = new unsigned char[padding.padWidth * bmpInfo.biHeight]; //Entrusts this array is deleted by ImpressionistDoc
memcpy(imageDataPtr, bitmapDibPtr + offset, byteLength);
mapColor(imageDataPtr, bmpInfo.biWidth, bmpInfo.biHeight, padding.pad, cinepak_color_mapping);
if (docPtr != nullptr)
{
docPtr->loadImageFromData(imageDataPtr, bmpInfo.biWidth, bmpInfo.biHeight);
}
else
{
fl_alert("docPtr is nullptr but this should not happen.");
}
perImageFunction();
}
bool VideoProcessor::isEnded()
{
if (singletonPtr != nullptr)
{
const auto* selfPtr = getInstancePtr();
if (selfPtr->aviReadStreamPtr != nullptr)
{
return selfPtr->frameIndex + selfPtr->streamStartingIndex >= selfPtr->totalFrames;
}
}
return true;
}
void VideoProcessor::close()
{
if (getFramePtr != nullptr)
{
AVIStreamGetFrameClose(getFramePtr);
}
if (aviCStreamPtr != nullptr)
{
AVIStreamRelease(aviCStreamPtr);
}
if (aviStreamPtr != nullptr)
{
AVIStreamRelease(aviStreamPtr);
}
if (aviPtr != nullptr)
{
AVIFileRelease(aviPtr);
}
if (aviReadStreamPtr != nullptr)
{
AVIStreamRelease(aviReadStreamPtr);
}
if (aviReadPtr != nullptr)
{
AVIFileRelease(aviReadPtr);
}
aviPtr = nullptr;
aviCStreamPtr = nullptr;
aviStreamPtr = nullptr;
aviReadPtr = nullptr;
aviReadStreamPtr = nullptr;
getFramePtr = nullptr;
}
VideoProcessor* VideoProcessor::getInstancePtr()
{
if (singletonPtr == nullptr)
{
singletonPtr = new VideoProcessor();
}
return singletonPtr;
}
void VideoProcessor::destroy()
{
delete singletonPtr;
singletonPtr = nullptr;
}