-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
FrameProcessThread.cpp
178 lines (159 loc) · 3.94 KB
/
FrameProcessThread.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
#include "FrameProcessThread.h"
#include "ImageUtil.h"
#include <mutex>
#include <condition_variable>
#include <thread>
#include <deque>
#include <QDebug>
#include "Deinterlace.h"
extern "C" {
#include <libswscale/swscale.h>
}
struct FrameProcessThread::Private {
std::mutex mutex;
std::condition_variable cond;
bool interrupted = false;
std::vector<std::thread> thread;
std::deque<std::shared_ptr<VideoFrameData>> requested_frames;
QSize scaled_size;
Deinterlace di;
bool deinterlace_enabled = true;
};
FrameProcessThread::FrameProcessThread()
: m(new Private)
{
}
FrameProcessThread::~FrameProcessThread()
{
stop();
delete m;
}
static QImage scale(Image const &srcimg, int w, int h, QImage::Format f)
{
AVPixelFormat sf = AV_PIX_FMT_NONE;
AVPixelFormat df = AV_PIX_FMT_NONE;
switch (srcimg.format()) {
case Image::Format::RGB8:
sf = AV_PIX_FMT_RGB24;
break;
case Image::Format::UYVY8:
sf = AV_PIX_FMT_UYVY422;
break;
case Image::Format::YUYV8:
sf = AV_PIX_FMT_YUYV422;
break;
case Image::Format::UINT8:
sf = AV_PIX_FMT_GRAY8;
break;
default:
return {};
}
switch (f) {
case QImage::Format_RGB888:
df = AV_PIX_FMT_RGB24;
break;
case QImage::Format_Grayscale8:
df = AV_PIX_FMT_GRAY8;
break;
default:
return {};
}
QImage newimg(w, h, f);
SwsContext *c = sws_getContext(srcimg.width(), srcimg.height(), sf, w, h, df, SWS_POINT, nullptr, nullptr, nullptr);
uint8_t const *srcdata[] = { srcimg.bits() };
uint8_t *dstdata[] = { newimg.bits() };
int srclines[] = { srcimg.bytesPerLine() };
int dstlines[] = { newimg.bytesPerLine() };
sws_scale(c, srcdata, srclines, 0, srcimg.height(), dstdata, dstlines);
sws_freeContext(c);
return newimg;
}
void FrameProcessThread::run()
{
while (1) {
std::shared_ptr<VideoFrameData> frame;
{
std::unique_lock lock(m->mutex);
if (m->interrupted) break;
for (size_t i = 0; i < m->requested_frames.size(); i++) {
if (m->requested_frames[i]->d->state == VideoFrameData::Idle) {
frame = m->requested_frames[i];
frame->d->state = VideoFrameData::Busy;
break;
}
}
if (!frame) {
m->cond.wait(lock);
}
}
if (frame) {
if (m->deinterlace_enabled) {
frame->d->image = m->di.deinterlace(frame->d->image);
}
// 画面表示用画像
#if 0
frame->d->image_for_view = ImageUtil::qimage(frame->d->image).scaled(m->scaled_size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
#else
frame->d->image_for_view = scale(frame->d->image, m->scaled_size.width(), m->scaled_size.height(), QImage::Format_RGB888);
#endif
frame->d->state = VideoFrameData::Ready;
}
std::deque<std::shared_ptr<VideoFrameData>> results;
{
std::unique_lock lock(m->mutex);
while (!m->requested_frames.empty()) {
if (m->requested_frames.front()->d->state != VideoFrameData::Ready) break;
frame = m->requested_frames.front();
m->requested_frames.pop_front();
results.push_back(frame);
}
}
while (!results.empty()) {
frame = results.front();
results.pop_front();
emit ready(*frame);
}
}
}
void FrameProcessThread::start()
{
stop();
m->thread.resize(4);
for (size_t i = 0; i < m->thread.size(); i++) {
m->thread[i] = std::thread([&](){
run();
});
}
}
void FrameProcessThread::stop()
{
{
std::lock_guard lock(m->mutex);
m->interrupted = true;
m->requested_frames = {};
}
m->cond.notify_all();
for (size_t i = 0; i < m->thread.size(); i++) {
if (m->thread[i].joinable()) {
m->thread[i].join();
}
}
m->thread.clear();
m->interrupted = false;
}
void FrameProcessThread::request(VideoFrameData const &image, const QSize &size)
{
if (image && size.width() > 0 && size.height() > 0) {
std::lock_guard lock(m->mutex);
while (m->requested_frames.size() > 4) {
m->requested_frames.pop_back(); // drop
}
m->requested_frames.push_back(std::make_shared<VideoFrameData>(image));
m->scaled_size = size;
m->cond.notify_all();
}
}
void FrameProcessThread::enableDeinterlace(bool enable)
{
m->deinterlace_enabled = enable;
}