7 #include "../cpudetect.h"
9 #include "img_format.h"
13 #include "../libvo/fastmemcpy.h"
17 /* difference: total, even lines, odd lines */
19 /* noise: temporal, spacial (current), spacial (past) */
24 /* peak, relative, mean */
25 struct metrics p
, r
, m
;
29 struct frameinfo fi
[2];
32 int drop
, lastdrop
, dropnext
;
33 int inframes
, outframes
;
43 static inline void *my_memcpy_pic(void * dst
, void * src
, int bytesPerLine
, int height
, int dstStride
, int srcStride
)
48 for(i
=0; i
<height
; i
++)
50 memcpy(dst
, src
, bytesPerLine
);
59 static void block_diffs_MMX(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
62 short out
[24]; // output buffer for the partial metrics from the mmx code
66 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
67 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
68 "pxor %%mm7, %%mm7 \n\t" // all zeros
74 "movq (%%esi), %%mm0 \n\t"
75 "movq (%%esi), %%mm2 \n\t"
76 "addl %%eax, %%esi \n\t"
77 "movq (%%edi), %%mm1 \n\t"
78 "addl %%ebx, %%edi \n\t"
79 "psubusb %%mm1, %%mm2 \n\t"
80 "psubusb %%mm0, %%mm1 \n\t"
81 "movq %%mm2, %%mm0 \n\t"
82 "movq %%mm1, %%mm3 \n\t"
83 "punpcklbw %%mm7, %%mm0 \n\t"
84 "punpcklbw %%mm7, %%mm1 \n\t"
85 "punpckhbw %%mm7, %%mm2 \n\t"
86 "punpckhbw %%mm7, %%mm3 \n\t"
87 "paddw %%mm0, %%mm4 \n\t"
88 "paddw %%mm1, %%mm4 \n\t"
89 "paddw %%mm2, %%mm4 \n\t"
90 "paddw %%mm3, %%mm4 \n\t"
93 "movq (%%esi), %%mm0 \n\t"
94 "movq (%%esi), %%mm2 \n\t"
95 "addl %%eax, %%esi \n\t"
96 "movq (%%edi), %%mm1 \n\t"
97 "addl %%ebx, %%edi \n\t"
98 "psubusb %%mm1, %%mm2 \n\t"
99 "psubusb %%mm0, %%mm1 \n\t"
100 "movq %%mm2, %%mm0 \n\t"
101 "movq %%mm1, %%mm3 \n\t"
102 "punpcklbw %%mm7, %%mm0 \n\t"
103 "punpcklbw %%mm7, %%mm1 \n\t"
104 "punpckhbw %%mm7, %%mm2 \n\t"
105 "punpckhbw %%mm7, %%mm3 \n\t"
106 "paddw %%mm0, %%mm5 \n\t"
107 "paddw %%mm1, %%mm5 \n\t"
108 "paddw %%mm2, %%mm5 \n\t"
109 "paddw %%mm3, %%mm5 \n\t"
113 "movq %%mm4, (%%edx) \n\t"
114 "movq %%mm5, 8(%%edx) \n\t"
116 : "S" (old
), "D" (new), "a" (os
), "b" (ns
), "d" (out
)
119 m
->e
= out
[0]+out
[1]+out
[2]+out
[3];
120 m
->o
= out
[4]+out
[5]+out
[6]+out
[7];
124 // First loop to measure first four columns
125 "movl $4, %%ecx \n\t"
126 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
127 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
128 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
133 "movq (%%esi), %%mm0 \n\t"
134 "movq (%%esi,%%eax), %%mm1 \n\t"
135 "addl %%eax, %%esi \n\t"
136 "addl %%eax, %%esi \n\t"
137 "movq (%%edi), %%mm2 \n\t"
138 "movq (%%edi,%%ebx), %%mm3 \n\t"
139 "addl %%ebx, %%edi \n\t"
140 "addl %%ebx, %%edi \n\t"
141 "punpcklbw %%mm7, %%mm0 \n\t"
142 "punpcklbw %%mm7, %%mm1 \n\t"
143 "punpcklbw %%mm7, %%mm2 \n\t"
144 "punpcklbw %%mm7, %%mm3 \n\t"
145 "paddw %%mm1, %%mm4 \n\t"
146 "paddw %%mm1, %%mm5 \n\t"
147 "paddw %%mm3, %%mm6 \n\t"
148 "psubw %%mm0, %%mm4 \n\t"
149 "psubw %%mm2, %%mm5 \n\t"
150 "psubw %%mm2, %%mm6 \n\t"
155 "movq %%mm0, %%mm1 \n\t"
156 "movq %%mm0, %%mm2 \n\t"
157 "movq %%mm0, %%mm3 \n\t"
158 "pcmpgtw %%mm4, %%mm1 \n\t"
159 "pcmpgtw %%mm5, %%mm2 \n\t"
160 "pcmpgtw %%mm6, %%mm3 \n\t"
161 "pxor %%mm1, %%mm4 \n\t"
162 "pxor %%mm2, %%mm5 \n\t"
163 "pxor %%mm3, %%mm6 \n\t"
164 "psubw %%mm1, %%mm4 \n\t"
165 "psubw %%mm2, %%mm5 \n\t"
166 "psubw %%mm3, %%mm6 \n\t"
167 "movq %%mm4, (%%edx) \n\t"
168 "movq %%mm5, 16(%%edx) \n\t"
169 "movq %%mm6, 32(%%edx) \n\t"
171 "movl %%eax, %%ecx \n\t"
172 "shll $3, %%ecx \n\t"
173 "subl %%ecx, %%esi \n\t"
174 "movl %%ebx, %%ecx \n\t"
175 "shll $3, %%ecx \n\t"
176 "subl %%ecx, %%edi \n\t"
178 // Second loop for the last four columns
179 "movl $4, %%ecx \n\t"
180 "pxor %%mm4, %%mm4 \n\t"
181 "pxor %%mm5, %%mm5 \n\t"
182 "pxor %%mm6, %%mm6 \n\t"
187 "movq (%%esi), %%mm0 \n\t"
188 "movq (%%esi,%%eax), %%mm1 \n\t"
189 "addl %%eax, %%esi \n\t"
190 "addl %%eax, %%esi \n\t"
191 "movq (%%edi), %%mm2 \n\t"
192 "movq (%%edi,%%ebx), %%mm3 \n\t"
193 "addl %%ebx, %%edi \n\t"
194 "addl %%ebx, %%edi \n\t"
195 "punpckhbw %%mm7, %%mm0 \n\t"
196 "punpckhbw %%mm7, %%mm1 \n\t"
197 "punpckhbw %%mm7, %%mm2 \n\t"
198 "punpckhbw %%mm7, %%mm3 \n\t"
199 "paddw %%mm1, %%mm4 \n\t"
200 "paddw %%mm1, %%mm5 \n\t"
201 "paddw %%mm3, %%mm6 \n\t"
202 "psubw %%mm0, %%mm4 \n\t"
203 "psubw %%mm2, %%mm5 \n\t"
204 "psubw %%mm2, %%mm6 \n\t"
209 "movq %%mm0, %%mm1 \n\t"
210 "movq %%mm0, %%mm2 \n\t"
211 "movq %%mm0, %%mm3 \n\t"
212 "pcmpgtw %%mm4, %%mm1 \n\t"
213 "pcmpgtw %%mm5, %%mm2 \n\t"
214 "pcmpgtw %%mm6, %%mm3 \n\t"
215 "pxor %%mm1, %%mm4 \n\t"
216 "pxor %%mm2, %%mm5 \n\t"
217 "pxor %%mm3, %%mm6 \n\t"
218 "psubw %%mm1, %%mm4 \n\t"
219 "psubw %%mm2, %%mm5 \n\t"
220 "psubw %%mm3, %%mm6 \n\t"
221 "movq %%mm4, 8(%%edx) \n\t"
222 "movq %%mm5, 24(%%edx) \n\t"
223 "movq %%mm6, 40(%%edx) \n\t"
227 : "S" (old
), "D" (new), "a" (os
), "b" (ns
), "d" (out
)
230 m
->p
= m
->t
= m
->s
= 0;
231 for (i
=0; i
<8; i
++) {
236 //printf("e=%d o=%d d=%d p=%d t=%d s=%d\n", m->e, m->o, m->d, m->p, m->t, m->s);
240 //#define MAG(a) ((a)*(a))
241 //#define MAG(a) (abs(a))
242 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
244 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
245 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
246 #define LOWPASS(s) ((s)[0])
249 static void block_diffs_C(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
251 int x
, y
, e
=0, o
=0, s
=0, p
=0, t
=0;
252 unsigned char *oldp
, *newp
;
253 m
->s
= m
->p
= m
->t
= 0;
254 for (x
= 8; x
; x
--) {
258 for (y
= 4; y
; y
--) {
259 e
+= MAG(newp
[0]-oldp
[0]);
260 o
+= MAG(newp
[ns
]-oldp
[os
]);
261 s
+= newp
[ns
]-newp
[0];
262 p
+= oldp
[os
]-oldp
[0];
263 t
+= oldp
[os
]-newp
[0];
276 static void (*block_diffs
)(struct metrics
*, unsigned char *, unsigned char *, int, int);
278 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
280 static void diff_planes(struct frameinfo
*fi
,
281 unsigned char *old
, unsigned char *new, int w
, int h
, int os
, int ns
)
285 struct metrics
*peak
=&fi
->p
, *rel
=&fi
->r
, *mean
=&fi
->m
;
286 memset(peak
, 0, sizeof(struct metrics
));
287 memset(rel
, 0, sizeof(struct metrics
));
288 memset(mean
, 0, sizeof(struct metrics
));
289 for (y
= 0; y
< h
-7; y
+= 8) {
290 for (x
= 8; x
< w
-8-7; x
+= 8) {
291 block_diffs(&l
, old
+x
+y
*os
, new+x
+y
*ns
, os
, ns
);
304 MAXUP(rel
->e
, l
.e
-l
.o
);
305 MAXUP(rel
->o
, l
.o
-l
.e
);
306 MAXUP(rel
->s
, l
.s
-l
.t
);
307 MAXUP(rel
->p
, l
.p
-l
.t
);
308 MAXUP(rel
->t
, l
.t
-l
.p
);
309 MAXUP(rel
->d
, l
.t
-l
.s
); /* hack */
321 static void diff_fields(struct frameinfo
*fi
, mp_image_t
*old
, mp_image_t
*new)
323 diff_planes(fi
, old
->planes
[0], new->planes
[0],
324 new->w
, new->h
, old
->stride
[0], new->stride
[0]);
327 static void stats(struct frameinfo
*f
)
329 mp_msg(MSGT_VFILTER
, MSGL_V
, " pd=%d re=%d ro=%d rp=%d rt=%d rs=%d rd=%d pp=%d pt=%d ps=%d\r",
330 f
->p
.d
, f
->r
.e
, f
->r
.o
, f
->r
.p
, f
->r
.t
, f
->r
.s
, f
->r
.d
, f
->p
.p
, f
->p
.t
, f
->p
.s
);
333 static int foo(struct vf_priv_s
*p
, mp_image_t
*new, mp_image_t
*cur
)
335 struct frameinfo
*f
= p
->fi
;
338 diff_fields(&f
[1], cur
, new);
341 // Immediately drop this frame if it's already been used.
347 // Sometimes a pulldown frame comes all by itself, so both
348 // its top and bottom field are duplicates from the adjacent
349 // two frames. We can just drop such a frame, but we
350 // immediately show the next frame instead to keep the frame
351 // drops evenly spaced during normal 3:2 pulldown sequences.
352 if ((3*f
[1].r
.o
< f
[1].r
.e
) && (f
[1].r
.s
< f
[1].r
.d
)) {
357 // If none of these conditions hold, we will consider the frame
358 // progressive and just show it as-is.
359 if (!( (3*f
[0].r
.e
< f
[0].r
.o
) ||
360 ((2*f
[0].r
.d
< f
[0].r
.s
) && (f
[0].r
.s
> 1200)) ||
361 ((2*f
[1].r
.t
< f
[1].r
.p
) && (f
[1].r
.p
> 1200)) ))
364 // Otherwise, we have to decide whether to merge or drop.
365 // If the noise metric only increases minimally, we're off
366 // to a good start...
367 if (((2*f
[1].r
.t
< 3*f
[1].r
.p
) && (f
[1].r
.t
< 3600)) ||
368 (f
[1].r
.t
< 900) || (f
[1].r
.d
< 900)) {
369 // ...and if noise decreases or the duplicate even field
370 // is detected, we go ahead with the merge.
371 if ((3*f
[0].r
.e
< f
[0].r
.o
) || (2*f
[1].r
.t
< f
[1].r
.p
)) {
381 static void copy_image(mp_image_t
*dmpi
, mp_image_t
*mpi
, int field
)
385 my_memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
/2,
386 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
387 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
388 my_memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
389 mpi
->chroma_width
, mpi
->chroma_height
/2,
390 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
391 my_memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
392 mpi
->chroma_width
, mpi
->chroma_height
/2,
393 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
397 my_memcpy_pic(dmpi
->planes
[0]+dmpi
->stride
[0],
398 mpi
->planes
[0]+mpi
->stride
[0], mpi
->w
, mpi
->h
/2,
399 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
400 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
401 my_memcpy_pic(dmpi
->planes
[1]+dmpi
->stride
[1],
402 mpi
->planes
[1]+mpi
->stride
[1],
403 mpi
->chroma_width
, mpi
->chroma_height
/2,
404 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
405 my_memcpy_pic(dmpi
->planes
[2]+dmpi
->stride
[2],
406 mpi
->planes
[2]+mpi
->stride
[2],
407 mpi
->chroma_width
, mpi
->chroma_height
/2,
408 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
412 memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
,
413 dmpi
->stride
[0], mpi
->stride
[0]);
414 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
415 memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
416 mpi
->chroma_width
, mpi
->chroma_height
,
417 dmpi
->stride
[1], mpi
->stride
[1]);
418 memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
419 mpi
->chroma_width
, mpi
->chroma_height
,
420 dmpi
->stride
[2], mpi
->stride
[2]);
426 static int do_put_image(struct vf_instance_s
* vf
, mp_image_t
*dmpi
)
428 struct vf_priv_s
*p
= vf
->priv
;
431 switch (p
->drop
&& !p
->dropnext
) {
436 dropflag
= (++p
->lastdrop
>= 5);
439 dropflag
= (++p
->lastdrop
>= 5) && (4*p
->inframes
<= 5*p
->outframes
);
444 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
445 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
446 mp_msg(MSGT_VFILTER
, MSGL_V
, "!");
452 return vf_next_put_image(vf
, dmpi
);
455 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
)
458 struct vf_priv_s
*p
= vf
->priv
;
462 if (p
->first
) { /* hack */
467 if (!p
->dmpi
) p
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
468 MP_IMGTYPE_STATIC
, MP_IMGFLAG_ACCEPT_STRIDE
|
469 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
470 mpi
->width
, mpi
->height
);
471 /* FIXME -- not correct, off by one frame! */
472 p
->dmpi
->qscale
= mpi
->qscale
;
473 p
->dmpi
->qstride
= mpi
->qstride
;
474 p
->dmpi
->qscale_type
= mpi
->qscale_type
;
476 switch (foo(p
, mpi
, p
->dmpi
)) {
478 copy_image(p
->dmpi
, mpi
, 2);
481 mp_msg(MSGT_VFILTER
, MSGL_V
, "DROP\n");
484 copy_image(p
->dmpi
, mpi
, 0);
485 ret
= do_put_image(vf
, p
->dmpi
);
486 copy_image(p
->dmpi
, mpi
, 1);
487 mp_msg(MSGT_VFILTER
, MSGL_V
, "MERGE\n");
491 copy_image(p
->dmpi
, mpi
, 2);
492 ret
= do_put_image(vf
, p
->dmpi
);
493 mp_msg(MSGT_VFILTER
, MSGL_V
, "NEXT\n");
497 ret
= do_put_image(vf
, p
->dmpi
);
498 copy_image(p
->dmpi
, mpi
, 2);
499 mp_msg(MSGT_VFILTER
, MSGL_V
, "OK\n");
506 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
)
512 return vf_next_query_format(vf
, fmt
);
517 static void uninit(struct vf_instance_s
* vf
)
522 static int open(vf_instance_t
*vf
, char* args
)
525 vf
->put_image
= put_image
;
526 vf
->query_format
= query_format
;
528 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
529 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
532 if (args
) sscanf(args
, "%d", &p
->drop
);
533 block_diffs
= block_diffs_C
;
535 if(gCpuCaps
.hasMMX
) block_diffs
= block_diffs_MMX
;
540 vf_info_t vf_info_ivtc
= {
541 "inverse telecine, take 2",