-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Deinterlace.cpp
359 lines (327 loc) · 9.45 KB
/
Deinterlace.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
#include "Deinterlace.h"
#include <QElapsedTimer>
#include <alloca.h>
#include <cstdint>
#include <functional>
#include <memory>
#include <omp.h>
#include <string.h>
#include <vector>
namespace {
inline int diff(int a, int b)
{
int d = a - b;
return d * d;
}
void process_row(int w, int stride, const uint8_t *prev, const uint8_t *curr, const uint8_t *next, uint8_t *dst, bool multiframe, bool alternateframe)
{
uint16_t *tmpbuf = (uint16_t *)alloca(sizeof(uint16_t) * w * 5);
uint16_t *scoreN2 = tmpbuf + w * 0; // -2
uint16_t *scoreN1 = tmpbuf + w * 1; // -1
uint16_t *score_0 = tmpbuf + w * 2; // 0
uint16_t *scoreP1 = tmpbuf + w * 3; // +1
uint16_t *scoreP2 = tmpbuf + w * 4; // +2
auto CalcScore = [&](uint16_t *score, int shift){
uint8_t const *a = curr + 2 - stride + shift;
uint8_t const *b = curr + 2 + stride - shift;
uint16_t u = 0;
uint16_t v = diff(a[0], b[0]);
for (int x = 0; x < w - 4; x++) {
uint16_t t = u;
u = v;
v = diff(a[x + 1], b[x + 1]);
score[x + 2] = t + u + v;
}
};
CalcScore(scoreN2, -2);
CalcScore(scoreN1, -1);
CalcScore(score_0, 0);
CalcScore(scoreP1, +1);
CalcScore(scoreP2, +2);
const int stride2 = stride * 2;
uint8_t const *prevframe = !alternateframe ? prev : curr;
uint8_t const *nextframe = !alternateframe ? curr : next;
for (int x = 2; x < w - 2; x++) {
uint16_t score = score_0[x];
int shift = 0;
if (score > scoreN1[x] || score > scoreP1[x]) {
if (scoreN1[x] < scoreP1[x]) {
score = scoreN1[x];
shift = -1;
} else {
score = scoreP1[x];
shift = +1;
}
}
if (score > scoreN2[x] || score > scoreP2[x]) {
if (scoreN2[x] < scoreP2[x]) {
score = scoreN2[x];
shift = -2;
} else {
score = scoreP2[x];
shift = +2;
}
}
int value = (curr[x - stride + shift] + curr[x + stride - shift]) / 2;
if (multiframe) {
const int a = curr[x - stride];
const int b = curr[x + stride];
const int c = diff(prev[x], next[x]) / 2;
const int d = diff(prev[x - stride], a) + diff(prev[x + stride], b) / 2;
const int e = diff(next[x - stride], a) + diff(next[x + stride], b) / 2;
const int f = (prevframe[x] + nextframe[x]) / 2;
int g = (prevframe[x - stride2] + nextframe[x - stride2]) / 2 - a;
int h = (prevframe[x + stride2] + nextframe[x + stride2]) / 2 - b;
if (g > h) { std::swap(g, h); }
const int i = +std::min(std::min(f - b, f - a), g);
const int j = -std::max(std::max(f - b, f - a), h);
const int k = std::max(std::max(c, std::max(d, e)), std::max(i, j));
const int min = f - k;
const int max = f + k;
value = std::min(std::max(value, min), max);
}
dst[x] = value;
}
dst[0] = dst[1] = dst[2];
dst[w - 2] = dst[w - 1] = dst[w - 3];
}
void process_channel(int w, int h, int stride, const uint8_t *src_prev, const uint8_t *src_curr, const uint8_t *src_next, uint8_t *dst)
{
#pragma omp parallel for
for (int y = 0; y < h; y++) {
uint8_t *d = dst + stride * y;
uint8_t const *curr = src_curr + stride * y;
uint8_t const *prev = src_prev + stride * y;
uint8_t const *next = src_next + stride * y;
if ((y & 1) && y >= 1 && y < h - 1) {
bool multiframe = (y >= 2 && y < h - 2);
bool alternateframe = false;
process_row(w, stride, prev, curr, next, d, multiframe, alternateframe);
} else {
memcpy(d, curr, w);
}
}
}
struct DeintRGB {
Image::Format format;
int w;
int h;
int w4;
int h3;
int stride;
int offsetR;
int offsetG;
int offsetB;
Image prepare(Image const &input)
{
format = input.format();
w = input.width();
h = input.height();
w4 = w + 4;
h3 = h * 3;
stride = w4;
offsetR = stride * h * 0;
offsetG = stride * h * 1;
offsetB = stride * h * 2;
Image planar(w4, h3, Image::Format::UINT8);
for (int y = 0; y < h; y++) {
uint8_t const *s = input.scanLine(y);
uint8_t *dR = planar.bits() + offsetR + stride * y;
uint8_t *dG = planar.bits() + offsetG + stride * y;
uint8_t *dB = planar.bits() + offsetB + stride * y;
for (int x = 0; x < w; x++) {
dR[2 + x] = s[3 * x + 0];
dG[2 + x] = s[3 * x + 1];
dB[2 + x] = s[3 * x + 2];
}
dR[0] = dR[1] = dR[2];
dG[0] = dG[1] = dG[2];
dB[0] = dB[1] = dB[2];
dR[w + 2] = dR[w + 3] = dR[w + 1];
dG[w + 2] = dG[w + 3] = dG[w + 1];
dB[w + 2] = dB[w + 3] = dB[w + 1];
}
return planar;
}
Image perform(Image const &prev, Image const &curr, Image const &next)
{
Image ret;
auto ValidImage = [&](Image const &img){
return img.width() == w4 && img.height() == h3 && img.format() == Image::Format::UINT8;
};
if (ValidImage(prev) && ValidImage(curr) && ValidImage(next)) {
Image dest(w4, h3, Image::Format::UINT8);
process_channel(w4, h, stride, prev.bits() + offsetR, curr.bits() + offsetR, next.bits() + offsetR, dest.bits() + offsetR);
process_channel(w4, h, stride, prev.bits() + offsetG, curr.bits() + offsetG, next.bits() + offsetG, dest.bits() + offsetG);
process_channel(w4, h, stride, prev.bits() + offsetB, curr.bits() + offsetB, next.bits() + offsetB, dest.bits() + offsetB);
ret = Image(w, h, format);
for (int y = 0; y < h; y++) {
uint8_t const *sR = dest.bits() + offsetR + stride * y + 2;
uint8_t const *sG = dest.bits() + offsetG + stride * y + 2;
uint8_t const *sB = dest.bits() + offsetB + stride * y + 2;
uint8_t *d = ret.scanLine(y);
for (int x = 0; x < w; x++) {
d[3 * x + 0] = sR[x];
d[3 * x + 1] = sG[x];
d[3 * x + 2] = sB[x];
}
}
}
return ret;
}
};
struct DeintYUV {
Image::Format format;
int w;
int h;
int w4;
int h3;
int stride;
int offsetY;
int offsetU;
int offsetV;
Image prepare(Image const &input)
{
format = input.format();
w = input.width();
h = input.height();
w4 = w + 4;
h3 = h * 3;
stride = w4;
offsetY = stride * h * 0;
offsetU = stride * h * 1;
offsetV = stride * h * 2;
Image planar(w4, h3, Image::Format::UINT8);
for (int y = 0; y < h; y++) {
uint8_t const *s = input.scanLine(y);
uint8_t *dY = planar.bits() + offsetY + stride * y;
uint8_t *dU = planar.bits() + offsetU + stride * y;
uint8_t *dV = planar.bits() + offsetV + stride * y;
switch (format) {
case Image::Format::YUYV8:
for (int x = 0; x < w / 2; x++) {
dY[2 + 2 * x + 0] = s[4 * x + 0];
dU[2 + x] = s[4 * x + 1];
dY[2 + 2 * x + 1] = s[4 * x + 2];
dV[2 + x] = s[4 * x + 3];
}
break;
case Image::Format::UYVY8:
for (int x = 0; x < w / 2; x++) {
dY[2 + 2 * x + 0] = s[4 * x + 1];
dU[2 + x] = s[4 * x + 0];
dY[2 + 2 * x + 1] = s[4 * x + 3];
dV[2 + x] = s[4 * x + 2];
}
break;
}
dY[0] = dY[1] = dY[2];
dU[0] = dU[1] = dU[2];
dV[0] = dV[1] = dV[2];
dY[w + 2] = dY[w + 3] = dY[w + 1];
dU[w / 2 + 2] = dU[w / 2 + 3] = dU[w / 2 + 1];
dV[w / 2 + 2] = dV[w / 2 + 3] = dV[w / 2 + 1];
}
return planar;
}
Image perform(Image const &prev, Image const &curr, Image const &next)
{
Image ret;
auto ValidImage = [&](Image const &img){
return img.width() == w4 && img.height() == h3 && img.format() == Image::Format::UINT8;
};
if (ValidImage(prev) && ValidImage(curr) && ValidImage(next)) {
Image dest = curr.copy();
process_channel(w4, h, stride, prev.bits() + offsetY, curr.bits() + offsetY, next.bits() + offsetY, dest.bits() + offsetY);
ret = Image(w, h, format);
for (int y = 0; y < h; y++) {
uint8_t const *sY = dest.bits() + offsetY + stride * y + 2;
uint8_t const *sU = dest.bits() + offsetU + stride * (y & ~1) + 2;
uint8_t const *sV = dest.bits() + offsetV + stride * (y & ~1) + 2;
uint8_t *d = ret.scanLine(y);
switch (format) {
case Image::Format::YUYV8:
for (int x = 0; x < w / 2; x++) {
d[4 * x + 0] = sY[2 * x + 0];
d[4 * x + 1] = sU[x];
d[4 * x + 2] = sY[2 * x + 1];
d[4 * x + 3] = sV[x];
}
break;
case Image::Format::UYVY8:
for (int x = 0; x < w / 2; x++) {
d[4 * x + 1] = sY[2 * x + 0];
d[4 * x + 0] = sU[x];
d[4 * x + 3] = sY[2 * x + 1];
d[4 * x + 2] = sV[x];
}
break;
}
}
}
return ret;
}
};
}
template <typename T> Image Deinterlace::process(Image const &input)
{
const int w = input.width();
const int h = input.height();
if (w < 1 || h < 1) return {};
// QElapsedTimer t;
// t.start();
T d;
Image planar = d.prepare(input);
int prev_i;
int curr_i;
int next_i;
{
std::lock_guard lock(mutex_);
if (image_count_ < NBUF) {
next_i = (image_index_ + image_count_) % NBUF;
image_buffer_[next_i] = planar;
image_count_++;
if (image_count_ == 1) {
next_i = (next_i + 1) % NBUF;
image_buffer_[next_i] = planar;
image_count_++;
}
if (image_count_ == 2) {
next_i = (next_i + 1) % NBUF;
image_buffer_[next_i] = planar;
image_count_++;
}
curr_i = (next_i + NBUF - 1) % NBUF;
prev_i = (next_i + NBUF - 2) % NBUF;
} else {
return {};
}
}
Image const &prev = image_buffer_[prev_i];
Image const &curr = image_buffer_[curr_i];
Image const &next = image_buffer_[next_i];
Image ret = d.perform(prev, curr, next);
// qDebug() << t.elapsed();
while (1) {
std::unique_lock lock(mutex_);
if (prev_i == image_index_) {
image_index_ = (image_index_ + 1) % NBUF;
image_count_--;
cond_.notify_all();
return ret;
}
cond_.wait(lock);
}
}
Image Deinterlace::deinterlace(Image const &input)
{
switch (input.format()) {
case Image::Format::YUYV8:
case Image::Format::UYVY8:
return process<DeintYUV>(input);
case Image::Format::RGB8:
return process<DeintRGB>(input);
default:
return process<DeintRGB>(input.convertToFormat(Image::Format::RGB8));
}
}