forked from gavrushkin/bldc-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
videocoder.cpp
98 lines (82 loc) · 2.56 KB
/
videocoder.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
/*
Copyright 2012-2014 Benjamin Vedder [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "videocoder.h"
#include <QtDebug>
#include "MatToQImage.h"
VideoCoder::VideoCoder(int w, int h, double fps, const char *file, QObject *parent) :
QThread(parent)
{
mAbort = false;
mFrameReady = false;
cv::Size S = cv::Size(w, h);
#ifdef Q_OS_WIN
// -1 open dialog with codec manual choose
mVideoWriter.open(file, -1, fps, S, true);
#elif defined(Q_OS_MAC)
mVideoWriter.open(file, CV_FOURCC('m', 'p', '4', 'v'), fps, S);
#else
mVideoWriter.open(file, CV_FOURCC('X','V','I','D'), fps, S);
#endif
// mVideoWriter.open(file, CV_FOURCC('H','2','6','4'), fps, S);
// mVideoWriter.open(file, CV_FOURCC('M','J','P','G'), fps, S);
// mVideoWriter.open(file, CV_FOURCC('I','Y','U','V'), fps, S);
if(!mVideoWriter.isOpened()) {
qWarning() << "Can't open video writer" << file;
}
}
void VideoCoder::setNextFrame(QImage img)
{
QMutexLocker lock(&mMutex);
if (mNextFrame.size() < 200) {
mNextFrame.append(img);
} else {
mNextFrame[mNextFrame.size() - 1] = img;
qWarning() << "Dropping frame to compress";
}
mFrameReady = true;
}
void VideoCoder::stopAndWait()
{
mAbort = true;
wait();
if(mVideoWriter.isOpened())
{
mVideoWriter.release();
}
}
void VideoCoder::run()
{
while (!mAbort || mFrameReady) {
while (!mFrameReady) {
if (mAbort) {
return;
}
QThread::msleep(1);
}
QImage img;
{
QMutexLocker lock(&mMutex);
img = mNextFrame[0];
}
//qDebug() << "Frames to compress:" << mNextFrame.size();
mVideoWriter << QImageToMat(img);
{
QMutexLocker lock(&mMutex);
mNextFrame.removeFirst();
if (mNextFrame.isEmpty()) {
mFrameReady = false;
}
}
}
}