-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldDeinterlaceYUY2.cpp
More file actions
413 lines (386 loc) · 9.51 KB
/
FieldDeinterlaceYUY2.cpp
File metadata and controls
413 lines (386 loc) · 9.51 KB
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
/*
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.
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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
The author can be contacted at:
Donald Graft
neuron2@comcast.net
*/
#include <stdio.h>
#include <windows.h>
#include "internal.h"
#include "version.h"
#include "utilities.h"
#include "fielddeinterlace.h"
void DrawStringYUY2(PVideoFrame &dst, int x, int y, const char *s);
bool FieldDeinterlace::MotionMask_YUY2(int frame, IScriptEnvironment* env)
{
int x, y;
int *counts, box, boxy;
#ifdef DEINTERLACE_MMX_BUILD
void asm_deinterlaceYUY2(const unsigned char *srcp, const unsigned char *p,
const unsigned char *n, unsigned char *fmask,
unsigned char *dmask, int thresh, int dthresh, int row_size);
void asm_deinterlace_chromaYUY2(const unsigned char *srcp, const unsigned char *p,
const unsigned char *n, unsigned char *fmask,
unsigned char *dmask, int thresh, int dthresh, int row_size);
#else
int val, val2;
#endif
const int CT = 15;
int boxarraysize;
char buf[80];
p = srcp - pitch;
n = srcp + pitch;
memset(fmaskp, 0, w);
for (x = 0; x < w; x += 4) *(int *)(dmaskp+x) = 0x00ff00ff;
for (y = 1; y < h - 1; y++)
{
fmaskp += dpitch;
dmaskp += dpitch;
srcp += pitch;
p += pitch;
n += pitch;
#ifdef DEINTERLACE_MMX_BUILD
if (chroma)
asm_deinterlace_chromaYUY2(srcp, p, n, fmaskp, dmaskp, threshold+1, dthreshold+1, w/4);
else
asm_deinterlaceYUY2(srcp, p, n, fmaskp, dmaskp, threshold+1, dthreshold+1, w/4);
#else
for (x = 0; x < w; x += 4)
{
val = ((long)p[x] - (long)srcp[x]) * ((long)n[x] - (long)srcp[x]);
if (val > threshold)
*(int *)(fmaskp+x) = 0x000000ff;
else
*(int *)(fmaskp+x) = 0;
if (val > dthreshold)
*(int *)(dmaskp+x) = 0x000000ff;
else
*(int *)(dmaskp+x) = 0;
val = ((long)p[x+2] - (long)srcp[x+2]) * ((long)n[x+2] - (long)srcp[x+2]);
if (val > threshold)
*(int *)(fmaskp+x) |= 0x00ff0000;
if (val > dthreshold)
*(int *)(dmaskp+x) |= 0x00ff0000;
val = ((long)p[x+1] - (long)srcp[x+1]) * ((long)n[x+1] - (long)srcp[x+1]);
val2 = ((long)p[x+3] - (long)srcp[x+3]) * ((long)n[x+3] - (long)srcp[x+3]);
if (val > dthreshold || val2 > dthreshold)
*(int *)(dmaskp+x) |= 0x00ff00ff;
if (chroma)
{
if (val > threshold || val2 > threshold)
*(int *)(fmaskp+x) |= 0x00ff00ff;
}
}
#endif
}
#ifdef DEINTERLACE_MMX_BUILD
#if !defined(GCC__)
__asm emms;
#else
__asm("emms");
#endif
#endif
fmaskp += dpitch;
memset(fmaskp, 0, w);
dmaskp += dpitch;
for (x = 0; x < w; x += 4) *(int *)(dmaskp+x) = 0x00ff00ff;
if (full == true) return(true);
// interlaced frame detection
boxarraysize = ((h+8)>>3) * ((w+2)>>3);
if ((counts = (int *) malloc(boxarraysize << 2)) == 0)
{
env->ThrowError("FieldDeinterlace: malloc failure");
}
memset(counts, 0, boxarraysize << 2);
fmaskp = fmaskp_saved;
p = fmaskp - dpitch;
n = fmaskp + dpitch;
for (y = 1; y < h - 1; y++)
{
boxy = (y >> 3) * (w >> 3);
fmaskp += dpitch;
p += dpitch;
n += dpitch;
for (x = 0; x < w; x += 4)
{
box = boxy + (x >> 3);
if (fmaskp[x] == 0xff && p[x] == 0xff && n[x] == 0xff)
{
counts[box]++;
}
if (fmaskp[x+2] == 0xff && p[x+2] == 0xff && n[x+2] == 0xff)
{
counts[box]++;
}
}
}
for (x = 0; x < boxarraysize; x++)
{
if (counts[x] > CT)
{
if (debug == true)
{
sprintf(buf,"FieldDeinterlace: %d: Combed frame!\n", frame);
OutputDebugString(buf);
}
free(counts);
return(true);
}
}
free(counts);
return(false);
}
PVideoFrame __stdcall FieldDeinterlace::GetFrameYUY2(int n, IScriptEnvironment* env)
{
int x, y;
int stat;
#ifdef BLEND_MMX_BUILD
int blendYUY2(const unsigned char *dstp, const unsigned char *cprev,
const unsigned char *cnext, unsigned char *finalp,
unsigned char *dmaskp, int count);
#else
unsigned int p0;
#endif
#ifdef INTERPOLATE_MMX_BUILD
int interpolateYUY2(const unsigned char *dstp, const unsigned char *cprev,
const unsigned char *cnext, unsigned char *finalp,
unsigned char *dmaskp, int count);
#endif
unsigned int p1, p2;
const unsigned char *cprev, *cnext;
char buf[255];
unsigned int hint;
src = child->GetFrame(n, env);
srcp_saved = srcp = (unsigned char *) src->GetReadPtr();
full = full_saved;
// Take orders from Telecide if he's giving them.
if (GetHintingData((unsigned char *) srcp, &hint) == false)
{
if (hint & PROGRESSIVE) return src;
full = true;
}
pitch = src->GetPitch();
w = src->GetRowSize();
h = src->GetHeight();
fmask = env->NewVideoFrame(vi);
fmaskp_saved = fmaskp = fmask->GetWritePtr();
dpitch = fmask->GetPitch();
dmask = env->NewVideoFrame(vi);
dmaskp_saved = dmaskp = dmask->GetWritePtr();
/* Check for manual overrides of the deinterlacing. */
overrides_p = overrides;
stat = 0;
if (overrides_p != NULL)
{
while (*overrides_p < 0xffffffff)
{
if ((unsigned int) n >= *overrides_p && (unsigned int) n <= *(overrides_p+1) &&
(*(overrides_p+2) == '+' || *(overrides_p+2) == '-'))
{
overrides_p += 2;
stat = *overrides_p;
break;
}
overrides_p += 3;
}
if (stat == '-')
{
sprintf(buf,"FieldDeinterlace: frame %d: forcing frame as not combed\n", n);
OutputDebugString(buf);
}
else if (stat == '+')
{
sprintf(buf,"FieldDeinterlace: frame %d: forcing frame as combed\n", n);
OutputDebugString(buf);
}
}
if ((stat == '-') || (MotionMask_YUY2(n, env) == false && full == false && stat != '+'))
{
if (show == true)
{
#ifdef MAKEWRITABLEHACK
MakeWritable2(&src, env, vi);
#else
env->MakeWritable(&src);
#endif
sprintf(buf, "FieldDeinterlace %s", VERSION);
DrawStringYUY2(src, 0, 0, buf);
sprintf(buf, "Copyright 2003-2008 Donald Graft");
DrawStringYUY2(src, 0, 1, buf);
if (stat == '-')
sprintf(buf, "%d: Clean frame [forced]", n);
else
sprintf(buf, "%d: Clean frame", n);
DrawStringYUY2(src, 0, 3, buf);
}
return src; // clean frame; return original
}
if (map == true)
{
// User wants to see the motion map.
env->MakeWritable(&src);
srcp = (unsigned char *) src->GetWritePtr();
dmaskp = dmaskp_saved;
pitch = src->GetPitch();
dpitch = dmask->GetPitch();
for (y = 0; y < h; y++)
{
for (x = 0; x < w; x+=4)
{
if (!dmaskp[x])
{
srcp[x] = (3 * 0x80 + srcp[x]) >> 2;
srcp[x+2] = (3 * 0x80 + srcp[x+2]) >> 2;
}
else
{
srcp[x] = 235;
srcp[x+1] = 240;
srcp[x+2] = 235;
srcp[x+3] = 16;
}
}
srcp += pitch;
dmaskp += dpitch;
}
return src;
}
/* Frame is combed or user wants all frames, so deinterlace. */
PVideoFrame final = env->NewVideoFrame(vi);
/* GstAVSynth: preserve timestamps */
final->SetTimestamp (src->GetTimestamp());
srcp = srcp_saved;
dmaskp = dmaskp_saved;
finalp = final->GetWritePtr();
cprev = srcp - pitch;
cnext = srcp + pitch;
if (blend)
{
/* First line. */
for (x=0; x<w; x+=4)
{
p1 = *(int *)(srcp+x) & 0xfefefefe;
p2 = *(int *)(cnext+x) & 0xfefefefe;
*(int *)(finalp+x) = (p1>>1) + (p2>>1);
}
srcp += pitch;
dmaskp += dpitch;
finalp += dpitch;
cprev += pitch;
cnext += pitch;
for (y = 1; y < h - 1; y++)
{
#ifdef BLEND_MMX_BUILD
blendYUY2(srcp, cprev, cnext, finalp, dmaskp, w/8);
#else
for (x=0; x<w; x+=4)
{
if (dmaskp[x])
{
p0 = *(int *)(srcp+x) & 0xfefefefe;
p1 = *(int *)(cprev+x) & 0xfcfcfcfc;
p2 = *(int *)(cnext+x) & 0xfcfcfcfc;
*(int *)(finalp+x) = (p0>>1) + (p1>>2) + (p2>>2);
}
else
{
*(int *)(finalp+x)=*(int *)(srcp+x);
}
}
#endif
srcp += pitch;
dmaskp += dpitch;
finalp += dpitch;
cprev += pitch;
cnext += pitch;
}
/* Last line. */
for (x=0; x<w; x+=4)
{
p1 = *(int *)(srcp+x) & 0xfefefefe;
p2 = *(int *)(cprev+x) & 0xfefefefe;
*(int *)(finalp+x) = (p1>>1) + (p2>>1);
}
#ifdef BLEND_MMX_BUILD
#if !defined(GCC__)
__asm emms;
#else
__asm("emms");
#endif
#endif
}
else
{
for (y = 0; y < h - 1; y++)
{
if (!(y&1))
{
memcpy(finalp,srcp,w);
}
else
{
#ifdef INTERPOLATE_MMX_BUILD
interpolateYUY2(srcp, cprev, cnext, finalp, dmaskp, w/8);
#else
for (x = 0; x < w; x += 4)
{
if (dmaskp[x])
{
p1 = *(int *)(cprev+x) & 0xfefefefe;
p2 = *(int *)(cnext+x) & 0xfefefefe;
*(int *)(finalp+x) = (p1>>1) + (p2>>1);
}
else
{
*(int *)(finalp+x) = *(int *)(srcp+x);
}
}
#endif
}
srcp += pitch;
dmaskp += dpitch;
finalp += dpitch;
cprev += pitch;
cnext += pitch;
}
/* Last line. */
memcpy(finalp,srcp,w);
#ifdef INTERPOLATE_MMX_BUILD
#if !defined(GCC__)
__asm emms;
#else
__asm("emms");
#endif
#endif
}
if (show == true)
{
sprintf(buf, "FieldDeinterlace %s", VERSION);
DrawStringYUY2(final, 0, 0, buf);
sprintf(buf, "Copyright 2003-2008 Donald Graft");
DrawStringYUY2(final, 0, 1, buf);
if (full == true)
{
sprintf(buf, "Deinterlacing all frames");
DrawStringYUY2(final, 0, 3, buf);
}
else
{
if (stat == '+')
sprintf(buf,"%d: Combed frame! [forced]", n);
else
sprintf(buf,"%d: Combed frame!", n);
DrawStringYUY2(final, 0, 3, buf);
}
}
return final;
}