-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagesBuffer.cpp
550 lines (479 loc) · 11.9 KB
/
ImagesBuffer.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include <QMessageBox>
#include <QPainter>
#include "ImagesBuffer.h"
/*! \brief Create and setup the images buffer
*
* Create and setup the images buffer
*
* @param maxsize max size of the buffer
*/
ImagesBuffer::ImagesBuffer(const unsigned maxsize = 1) : _maxsize(maxsize)
{
if (maxsize <= 0) {
_maxsize = 1;
}
_mid = (_maxsize - 1) / 2;
}
/*! \brief Destroyer
*
* Destroyer
*/
ImagesBuffer::~ImagesBuffer()
{
_buffer.clear();
}
/*! \brief retrieve the frame with the given frame number.
*
* Checks if the frame is already in the buffer, if not, reseek and refill the buffer
*
* @param p where the image will be stored
* @param num number of the frame
* @return success or not
*/
bool ImagesBuffer::getFrame(Frame &f, const qint64 num)
{
if (!isVideoLoaded())
return false;
if (num < 0)
return false;
// already in the buffer?
int index = isFrameLoaded(num);
if (index != -1) {
f = _buffer[index];
return true;
}
if (!seekToFrame(num)) {
// QMessageBox::critical(NULL, "Error", "Error seeking and decoding the frame");
return false;
}
f = _buffer[_mid];
return true;
}
/*! \brief retrieve the frame with the given frame number.
*
* Checks if the frame is already in the buffer, if not, retrieve
* the frame BUT DO NOT UPDATE THE BUFFER.
* The buffer will be updated only when calling seekToFrame().
* This is usefull when we the video is in playback and we want just one image.
*
* @param p where the image will be stored
* @param num number of the frame
* @return success or not
*/
bool ImagesBuffer::getSingleFrame(Frame &f, const qint64 num)
{
if (!isVideoLoaded())
return false;
if (num < 0)
return false;
// already in the buffer?
int index = isFrameLoaded(num);
if (index != -1) {
f = _buffer[index];
return true;
}
// go and get that
QImage img;
if (!_decoder.seekToAndGetFrame(num, img, &f.pts, &f.time)) {
QMessageBox::critical(NULL, "Error", "Error seeking and decoding the frame");
return false;
}
image2Pixmap(img, f.img);
f.num = num;
return true;
}
/*! \brief seek to frame number
*
* Seek the buffer to the given frame number.
*
* @param num frame number
* @return succes or not
*/
bool ImagesBuffer::seekToFrame(const qint64 num)
{
if (!isVideoLoaded())
return false;
if (_buffer.size() != 0) {
if (_buffer[_mid].num == num) // already set
return true;
}
qint64 startFrameNumber = num - ((_maxsize - 1) / 2);
int numElements = _maxsize;
bool addBack = true;
// no overlap if the buffer is empty
if (_buffer.size() != 0) {
// It may happen that the mid element is a non valid frame
if (_buffer[_mid].num != -1) {
qint64 dist = num - _buffer[_mid].num; // distance between old and new buffer
unsigned distAbs = abs(dist);
bool overlap = distAbs < _maxsize; // overlap between old and new buffer
if (overlap) {
qint64 numOverlap = _maxsize - distAbs; // num elements overlapping
if (dist > 0) { // seeking forward so erase first elements of the buffer
startFrameNumber += numOverlap; // update start frame number
_buffer.erase(_buffer.begin(), _buffer.begin() + distAbs);
}
else {
_buffer.erase(_buffer.end() - distAbs, _buffer.end());
addBack = false;
}
numElements -= numOverlap;
}
else { // no overlap, clear
_buffer.clear();
}
}
else { // not valid mid, reset
_buffer.clear();
}
}
// fill the buffer from startNumber with numElements elements
if (!fillBuffer(startFrameNumber, numElements, addBack)) {
// QMessageBox::critical(NULL, "Error", "Seek failed");
// TODO: buffer inconsistent, what to do?
return false;
}
return true;
// emit
}
/*! \brief fill the buffer
*
* Fill the buffer starting from the gieven start fram number and adding
* numElements elements in front or back based on addBack.
* @param startFrameNumber start frame number
* @param numElements num elements to add
* @param addBack put elements back or in front
* @return succes or not
*/
bool ImagesBuffer::fillBuffer(
const qint64 startFrameNumber,
const int numElements,
const bool addBack
)
{
bool endofstream = false;
// Seek to the first frame
if (!_decoder.seekFrame((startFrameNumber >= 0) ? startFrameNumber : 0)) {
return false;
}
for (int i = 0; i < numElements; ++i) {
int actualFrameNumber = startFrameNumber + i;
Frame f;
// if out of bound retrieve and fill the image
if (actualFrameNumber >= 0 && actualFrameNumber < numFrames && !endofstream) {
// Decode the frame
QImage img;
if (!_decoder.getFrame(img, &f.pts, &f.time)) {
QMessageBox::critical(NULL, "Error", "Error decoding the frame");
// TODO: buffer inconsistent, what to do?
return false;
}
// Update the buffer with this Frame
image2Pixmap(img, f.img);
f.num = actualFrameNumber;
// Seek next
endofstream = !_decoder.seekNextFrame();
}
if (addBack)
_buffer.push_back(f);
else
_buffer.emplace(_buffer.begin() + i, f);
}
if (_buffer[_mid].num == -1) {
return false;
}
// dumpBuffer();
return true;
}
/*! \brief get the frame located at given time
*
* Get the frame located at the given time.
* @param p where the image will be stored
* @param num related frame number
* @param ms time in milliseconds
* @return success or not
* @see seekToFrame()
*/
bool ImagesBuffer::getFrameByTime(Frame &f, const qint64 ms)
{
if (!isVideoLoaded())
return false;
qint64 num = _decoder.getNumFrameByTime(ms);
if (!getFrame(f, num)) {
QMessageBox::critical(NULL,"Error","Seek failed, invalid time");
return false;
}
return true;
}
/*! \brief get the frame located at given time percentage
*
* Get the frame located at the given percentage of the entire video length.
* @param p where the image will be stored
* @param num related frame number
* @param perc double value from 0 to 1
* @return success or not
* @see seekToTime()
*/
bool ImagesBuffer::getFrameByTimePercentage(Frame &f, const double perc)
{
if (!isVideoLoaded())
return false;
qint64 ms = videoLength * perc;
bool ok = getFrameByTime(f, ms);
return ok;
}
/**************************************
************ HELPERS ************
***************************************/
/*! \brief the frame is in the buffer?
*
* Checks if the frame has been loaded before.
* @param num number of the frame
* @return the index of the element or -1
*/
const int ImagesBuffer::isFrameLoaded(const qint64 num)
{
if (num >= 0) {
for (int i = 0; i < _buffer.size(); ++i) {
if (_buffer[i].num == num) {
return i;
}
}
}
return -1;
}
/*! \brief from QImage to QPixmap.
*
* Convert a QImage to a QPixmap.
* @param img as QImage
* @param pixmap as QPixmap
*/
void ImagesBuffer::image2Pixmap(QImage &img, QPixmap &pixmap)
{
// Convert the QImage to a QPixmap for display
pixmap = QPixmap(img.size());
QPainter painter;
painter.begin(&pixmap);
painter.drawImage(0, 0, img);
painter.end();
}
void ImagesBuffer::dumpBuffer()
{
qDebug() << "Dump del buffer:";
for (int i = 0; i < _buffer.size(); ++i) {
if (_buffer[i].num == -1)
qDebug() << "\t" << QString("%1 - - -").arg(i);
else
qDebug() << "\t" << QString("%1 %2 %3 %4 %5").arg(i).arg(_buffer[i].num).arg(_buffer[i].pts).arg(_buffer[i].time).arg((i == _mid) ? " <-" : "");
}
}
/**************************************
********* VIDEO ACTIONS *********
***************************************/
/*! \brief load a video.
*
* Open and load a video by using ffmpeg's decoder.
* @param fileName path to the video
*/
bool ImagesBuffer::loadVideo(const QString fileName)
{
_buffer.clear();
_decoder.openFile(fileName);
numFrames = _decoder.getNumFrames();
videoLength = _decoder.getVideoLengthMs();
frameMs = _decoder.getFrameMsec();
if (!_decoder.isOk()) {
return false;
}
// Seek to the first frame
if (!seekToFrame(0)) {
QMessageBox::critical(NULL, "Error", "Seek to the first frame failed");
//nextFrame(); try to get the next frame instead of showing nothing
}
return true;
}
/**************************************
********* GETTERS *********
***************************************/
/*! \brief get num images centered on mid
*
* Retrieve "num" images centered on "mid"
* @param v where Frames will be stored
* @param mid number of the middle elements
* @param num number of elements to retrieve
*/
void ImagesBuffer::getImagesBuffer(std::vector<Frame> &v, const int mid, const int num)
{
bool endofstream = false;
qint64 startFrameNumber = mid - ((num - 1) / 2);
Frame f;
for (int i = 0; i < num; ++i) {
int actualFrameNumber = startFrameNumber + i;
// if out of bound retrieve and fill the image
if (actualFrameNumber >= 0 && actualFrameNumber < numFrames && !endofstream) {
int index = isFrameLoaded(actualFrameNumber);
if (index <= -1) {// not in the buffer? must update the buffer
if (endofstream = !seekToFrame(actualFrameNumber)) { // fake frame
v.push_back(f);
}
else {
index = _mid;
v.push_back(_buffer[index]);
}
}
else { // already in the buffer
v.push_back(_buffer[index]);
}
}
else { // fake frame
v.push_back(f);
}
}//for
}
/*! \brief retrieve the middle (current) frame
*
* Retrieve the middle frame image
* @param p where the image will be stored
* @return success or not
*/
bool ImagesBuffer::getMidFrame(Frame &f) {
f = _buffer[_mid];
return true;
}
/*! \brief a video was loaded?
*
* Checks if a video has been previously loaded.
*/
bool ImagesBuffer::isVideoLoaded()
{
return _decoder.isOk();
}
/*! \brief Get number of frames
*
* Retrieve the number of frames
*/
qint64 ImagesBuffer::getNumFrames() {
return numFrames;
}
/*! \brief Get number of frames
*
* Retrieve the number of frames
*/
qint64 ImagesBuffer::getVideoLengthMs() {
return videoLength;
}
/*! \brief Get frame's dimensions
*
* Retrieve information about frame's dimensions
* @param ratio frame's ratio as width / height
* @param w frame's width
* @param h frame's height
* @return success or not
*/
bool ImagesBuffer::getDimensions(double &ratio, int *w, int *h)
{
if (!isVideoLoaded() || _buffer.size()==0)
return false;
int wi = _buffer[_mid].img.width();
int he = _buffer[_mid].img.height();
ratio = wi / (double) he;
if (w)
*w = wi;
if (h)
*h = he;
return true;
}
/*! \brief Get video path
*
* Retrieve video path
*/
QString ImagesBuffer::getPath() {
return _decoder.getPath();
}
/*! \brief Get video path
*
* Retrieve video path
*/
QString ImagesBuffer::getType() {
return _decoder.getType();
}
/*! \brief Get video duration
*
* Retrieve video duration
*/
QString ImagesBuffer::getDuration() {
int hours, mins, secs, us;
secs = videoLength / 1000;
us = videoLength % 1000;
mins = secs / 60;
secs %= 60;
hours = mins / 60;
mins %= 60;
QString s = QString("%1h %2m %3s %4").arg(hours).arg(mins).arg(secs).arg((100 * us) / 1000);
return s;
}
/*! \brief Get video time base
*
* Retrieve video time base
*/
double ImagesBuffer::getTimeBase() {
return _decoder.getTimeBase();
}
/*! \brief Get video frame rate
*
* Retrieve video frame rate
*/
double ImagesBuffer::getFrameRate() {
return _decoder.getFrameRate();
}
/*! \brief Get video frame ms (theorycal)
*
* Retrieve video frame ms (theorycal)
*/
double ImagesBuffer::getFrameMsec() {
return _decoder.getFrameMsec();
}
/*! \brief Get video frame ms (real)
*
* Retrieve video frame ms (real)
*/
double ImagesBuffer::getFrameMsecReal() {
return _decoder.getFrameMsecReal();
}
/*! \brief Get frame width
*
* Get frame width
*/
int ImagesBuffer::getFrameWidth() {
return _decoder.getFrameWidth();
}
/*! \brief Get frame height
*
* Get frame height
*/
int ImagesBuffer::getFrameHeight() {
return _decoder.getFrameHeight();
}
/*! \brief Get video bitrate
*
* Get video bitrate
*/
QString ImagesBuffer::getBitrate() {
return _decoder.getBitrate();
}
/*! \brief Get string of programs used to make the video
*
* Get string of programs used to make the video
*/
QString ImagesBuffer::getProgramsString()
{
return _decoder.getProgramsString();
}
/*! \brief Get string of metadata
*
* Get string of metadata
*/
QString ImagesBuffer::getMetadataString()
{
return _decoder.getMetadataString();
}