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
;
44 static void block_diffs_MMX(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
47 short out
[24]; // output buffer for the partial metrics from the mmx code
51 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
52 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
53 "pxor %%mm7, %%mm7 \n\t" // all zeros
59 "movq (%%"REG_S
"), %%mm0 \n\t"
60 "movq (%%"REG_S
"), %%mm2 \n\t"
61 "add %%"REG_a
", %%"REG_S
" \n\t"
62 "movq (%%"REG_D
"), %%mm1 \n\t"
63 "add %%"REG_b
", %%"REG_D
" \n\t"
64 "psubusb %%mm1, %%mm2 \n\t"
65 "psubusb %%mm0, %%mm1 \n\t"
66 "movq %%mm2, %%mm0 \n\t"
67 "movq %%mm1, %%mm3 \n\t"
68 "punpcklbw %%mm7, %%mm0 \n\t"
69 "punpcklbw %%mm7, %%mm1 \n\t"
70 "punpckhbw %%mm7, %%mm2 \n\t"
71 "punpckhbw %%mm7, %%mm3 \n\t"
72 "paddw %%mm0, %%mm4 \n\t"
73 "paddw %%mm1, %%mm4 \n\t"
74 "paddw %%mm2, %%mm4 \n\t"
75 "paddw %%mm3, %%mm4 \n\t"
78 "movq (%%"REG_S
"), %%mm0 \n\t"
79 "movq (%%"REG_S
"), %%mm2 \n\t"
80 "add %%"REG_a
", %%"REG_S
" \n\t"
81 "movq (%%"REG_D
"), %%mm1 \n\t"
82 "add %%"REG_b
", %%"REG_D
" \n\t"
83 "psubusb %%mm1, %%mm2 \n\t"
84 "psubusb %%mm0, %%mm1 \n\t"
85 "movq %%mm2, %%mm0 \n\t"
86 "movq %%mm1, %%mm3 \n\t"
87 "punpcklbw %%mm7, %%mm0 \n\t"
88 "punpcklbw %%mm7, %%mm1 \n\t"
89 "punpckhbw %%mm7, %%mm2 \n\t"
90 "punpckhbw %%mm7, %%mm3 \n\t"
91 "paddw %%mm0, %%mm5 \n\t"
92 "paddw %%mm1, %%mm5 \n\t"
93 "paddw %%mm2, %%mm5 \n\t"
94 "paddw %%mm3, %%mm5 \n\t"
98 "movq %%mm4, (%%"REG_d
") \n\t"
99 "movq %%mm5, 8(%%"REG_d
") \n\t"
101 : "S" (old
), "D" (new), "a" (os
), "b" (ns
), "d" (out
)
104 m
->e
= out
[0]+out
[1]+out
[2]+out
[3];
105 m
->o
= out
[4]+out
[5]+out
[6]+out
[7];
109 // First loop to measure first four columns
110 "movl $4, %%ecx \n\t"
111 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
112 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
113 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
118 "movq (%%"REG_S
"), %%mm0 \n\t"
119 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
120 "add %%"REG_a
", %%"REG_S
" \n\t"
121 "add %%"REG_a
", %%"REG_S
" \n\t"
122 "movq (%%"REG_D
"), %%mm2 \n\t"
123 "movq (%%"REG_D
",%%"REG_b
"), %%mm3 \n\t"
124 "add %%"REG_b
", %%"REG_D
" \n\t"
125 "add %%"REG_b
", %%"REG_D
" \n\t"
126 "punpcklbw %%mm7, %%mm0 \n\t"
127 "punpcklbw %%mm7, %%mm1 \n\t"
128 "punpcklbw %%mm7, %%mm2 \n\t"
129 "punpcklbw %%mm7, %%mm3 \n\t"
130 "paddw %%mm1, %%mm4 \n\t"
131 "paddw %%mm1, %%mm5 \n\t"
132 "paddw %%mm3, %%mm6 \n\t"
133 "psubw %%mm0, %%mm4 \n\t"
134 "psubw %%mm2, %%mm5 \n\t"
135 "psubw %%mm2, %%mm6 \n\t"
140 "movq %%mm0, %%mm1 \n\t"
141 "movq %%mm0, %%mm2 \n\t"
142 "movq %%mm0, %%mm3 \n\t"
143 "pcmpgtw %%mm4, %%mm1 \n\t"
144 "pcmpgtw %%mm5, %%mm2 \n\t"
145 "pcmpgtw %%mm6, %%mm3 \n\t"
146 "pxor %%mm1, %%mm4 \n\t"
147 "pxor %%mm2, %%mm5 \n\t"
148 "pxor %%mm3, %%mm6 \n\t"
149 "psubw %%mm1, %%mm4 \n\t"
150 "psubw %%mm2, %%mm5 \n\t"
151 "psubw %%mm3, %%mm6 \n\t"
152 "movq %%mm4, (%%"REG_d
") \n\t"
153 "movq %%mm5, 16(%%"REG_d
") \n\t"
154 "movq %%mm6, 32(%%"REG_d
") \n\t"
156 "mov %%"REG_a
", %%"REG_c
" \n\t"
157 "shl $3, %%"REG_c
" \n\t"
158 "sub %%"REG_c
", %%"REG_S
" \n\t"
159 "mov %%"REG_b
", %%"REG_c
" \n\t"
160 "shl $3, %%"REG_c
" \n\t"
161 "sub %%"REG_c
", %%"REG_D
" \n\t"
163 // Second loop for the last four columns
164 "movl $4, %%ecx \n\t"
165 "pxor %%mm4, %%mm4 \n\t"
166 "pxor %%mm5, %%mm5 \n\t"
167 "pxor %%mm6, %%mm6 \n\t"
172 "movq (%%"REG_S
"), %%mm0 \n\t"
173 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
174 "add %%"REG_a
", %%"REG_S
" \n\t"
175 "add %%"REG_a
", %%"REG_S
" \n\t"
176 "movq (%%"REG_D
"), %%mm2 \n\t"
177 "movq (%%"REG_D
",%%"REG_b
"), %%mm3 \n\t"
178 "add %%"REG_b
", %%"REG_D
" \n\t"
179 "add %%"REG_b
", %%"REG_D
" \n\t"
180 "punpckhbw %%mm7, %%mm0 \n\t"
181 "punpckhbw %%mm7, %%mm1 \n\t"
182 "punpckhbw %%mm7, %%mm2 \n\t"
183 "punpckhbw %%mm7, %%mm3 \n\t"
184 "paddw %%mm1, %%mm4 \n\t"
185 "paddw %%mm1, %%mm5 \n\t"
186 "paddw %%mm3, %%mm6 \n\t"
187 "psubw %%mm0, %%mm4 \n\t"
188 "psubw %%mm2, %%mm5 \n\t"
189 "psubw %%mm2, %%mm6 \n\t"
194 "movq %%mm0, %%mm1 \n\t"
195 "movq %%mm0, %%mm2 \n\t"
196 "movq %%mm0, %%mm3 \n\t"
197 "pcmpgtw %%mm4, %%mm1 \n\t"
198 "pcmpgtw %%mm5, %%mm2 \n\t"
199 "pcmpgtw %%mm6, %%mm3 \n\t"
200 "pxor %%mm1, %%mm4 \n\t"
201 "pxor %%mm2, %%mm5 \n\t"
202 "pxor %%mm3, %%mm6 \n\t"
203 "psubw %%mm1, %%mm4 \n\t"
204 "psubw %%mm2, %%mm5 \n\t"
205 "psubw %%mm3, %%mm6 \n\t"
206 "movq %%mm4, 8(%%"REG_d
") \n\t"
207 "movq %%mm5, 24(%%"REG_d
") \n\t"
208 "movq %%mm6, 40(%%"REG_d
") \n\t"
212 : "S" (old
), "D" (new), "a" ((long)os
), "b" ((long)ns
), "d" (out
)
215 m
->p
= m
->t
= m
->s
= 0;
216 for (i
=0; i
<8; i
++) {
221 //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);
225 //#define MAG(a) ((a)*(a))
226 //#define MAG(a) (abs(a))
227 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
229 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
230 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
231 #define LOWPASS(s) ((s)[0])
234 static void block_diffs_C(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
236 int x
, y
, e
=0, o
=0, s
=0, p
=0, t
=0;
237 unsigned char *oldp
, *newp
;
238 m
->s
= m
->p
= m
->t
= 0;
239 for (x
= 8; x
; x
--) {
243 for (y
= 4; y
; y
--) {
244 e
+= MAG(newp
[0]-oldp
[0]);
245 o
+= MAG(newp
[ns
]-oldp
[os
]);
246 s
+= newp
[ns
]-newp
[0];
247 p
+= oldp
[os
]-oldp
[0];
248 t
+= oldp
[os
]-newp
[0];
261 static void (*block_diffs
)(struct metrics
*, unsigned char *, unsigned char *, int, int);
263 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
265 static void diff_planes(struct frameinfo
*fi
,
266 unsigned char *old
, unsigned char *new, int w
, int h
, int os
, int ns
)
270 struct metrics
*peak
=&fi
->p
, *rel
=&fi
->r
, *mean
=&fi
->m
;
271 memset(peak
, 0, sizeof(struct metrics
));
272 memset(rel
, 0, sizeof(struct metrics
));
273 memset(mean
, 0, sizeof(struct metrics
));
274 for (y
= 0; y
< h
-7; y
+= 8) {
275 for (x
= 8; x
< w
-8-7; x
+= 8) {
276 block_diffs(&l
, old
+x
+y
*os
, new+x
+y
*ns
, os
, ns
);
289 MAXUP(rel
->e
, l
.e
-l
.o
);
290 MAXUP(rel
->o
, l
.o
-l
.e
);
291 MAXUP(rel
->s
, l
.s
-l
.t
);
292 MAXUP(rel
->p
, l
.p
-l
.t
);
293 MAXUP(rel
->t
, l
.t
-l
.p
);
294 MAXUP(rel
->d
, l
.t
-l
.s
); /* hack */
306 static void diff_fields(struct frameinfo
*fi
, mp_image_t
*old
, mp_image_t
*new)
308 diff_planes(fi
, old
->planes
[0], new->planes
[0],
309 new->w
, new->h
, old
->stride
[0], new->stride
[0]);
312 static void stats(struct frameinfo
*f
)
314 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",
315 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
);
318 static int foo(struct vf_priv_s
*p
, mp_image_t
*new, mp_image_t
*cur
)
320 struct frameinfo
*f
= p
->fi
;
323 diff_fields(&f
[1], cur
, new);
326 // Immediately drop this frame if it's already been used.
332 // Sometimes a pulldown frame comes all by itself, so both
333 // its top and bottom field are duplicates from the adjacent
334 // two frames. We can just drop such a frame, but we
335 // immediately show the next frame instead to keep the frame
336 // drops evenly spaced during normal 3:2 pulldown sequences.
337 if ((3*f
[1].r
.o
< f
[1].r
.e
) && (f
[1].r
.s
< f
[1].r
.d
)) {
342 // If none of these conditions hold, we will consider the frame
343 // progressive and just show it as-is.
344 if (!( (3*f
[0].r
.e
< f
[0].r
.o
) ||
345 ((2*f
[0].r
.d
< f
[0].r
.s
) && (f
[0].r
.s
> 1200)) ||
346 ((2*f
[1].r
.t
< f
[1].r
.p
) && (f
[1].r
.p
> 1200)) ))
349 // Otherwise, we have to decide whether to merge or drop.
350 // If the noise metric only increases minimally, we're off
351 // to a good start...
352 if (((2*f
[1].r
.t
< 3*f
[1].r
.p
) && (f
[1].r
.t
< 3600)) ||
353 (f
[1].r
.t
< 900) || (f
[1].r
.d
< 900)) {
354 // ...and if noise decreases or the duplicate even field
355 // is detected, we go ahead with the merge.
356 if ((3*f
[0].r
.e
< f
[0].r
.o
) || (2*f
[1].r
.t
< f
[1].r
.p
)) {
366 static void copy_image(mp_image_t
*dmpi
, mp_image_t
*mpi
, int field
)
370 my_memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
/2,
371 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
372 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
373 my_memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
374 mpi
->chroma_width
, mpi
->chroma_height
/2,
375 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
376 my_memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
377 mpi
->chroma_width
, mpi
->chroma_height
/2,
378 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
382 my_memcpy_pic(dmpi
->planes
[0]+dmpi
->stride
[0],
383 mpi
->planes
[0]+mpi
->stride
[0], mpi
->w
, mpi
->h
/2,
384 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
385 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
386 my_memcpy_pic(dmpi
->planes
[1]+dmpi
->stride
[1],
387 mpi
->planes
[1]+mpi
->stride
[1],
388 mpi
->chroma_width
, mpi
->chroma_height
/2,
389 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
390 my_memcpy_pic(dmpi
->planes
[2]+dmpi
->stride
[2],
391 mpi
->planes
[2]+mpi
->stride
[2],
392 mpi
->chroma_width
, mpi
->chroma_height
/2,
393 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
397 memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
,
398 dmpi
->stride
[0], mpi
->stride
[0]);
399 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
400 memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
401 mpi
->chroma_width
, mpi
->chroma_height
,
402 dmpi
->stride
[1], mpi
->stride
[1]);
403 memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
404 mpi
->chroma_width
, mpi
->chroma_height
,
405 dmpi
->stride
[2], mpi
->stride
[2]);
411 static int do_put_image(struct vf_instance_s
* vf
, mp_image_t
*dmpi
)
413 struct vf_priv_s
*p
= vf
->priv
;
416 if (!p
->dropnext
) switch (p
->drop
) {
421 dropflag
= (++p
->lastdrop
>= 5);
424 dropflag
= (++p
->lastdrop
>= 5) && (4*p
->inframes
<= 5*p
->outframes
);
429 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
430 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
431 mp_msg(MSGT_VFILTER
, MSGL_V
, "!");
437 return vf_next_put_image(vf
, dmpi
, MP_NOPTS_VALUE
);
440 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
)
443 struct vf_priv_s
*p
= vf
->priv
;
447 if (p
->first
) { /* hack */
452 if (!p
->dmpi
) p
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
453 MP_IMGTYPE_STATIC
, MP_IMGFLAG_ACCEPT_STRIDE
|
454 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
455 mpi
->width
, mpi
->height
);
456 /* FIXME -- not correct, off by one frame! */
457 p
->dmpi
->qscale
= mpi
->qscale
;
458 p
->dmpi
->qstride
= mpi
->qstride
;
459 p
->dmpi
->qscale_type
= mpi
->qscale_type
;
461 switch (foo(p
, mpi
, p
->dmpi
)) {
463 copy_image(p
->dmpi
, mpi
, 2);
466 mp_msg(MSGT_VFILTER
, MSGL_V
, "DROP\n");
469 copy_image(p
->dmpi
, mpi
, 0);
470 ret
= do_put_image(vf
, p
->dmpi
);
471 copy_image(p
->dmpi
, mpi
, 1);
472 mp_msg(MSGT_VFILTER
, MSGL_V
, "MERGE\n");
476 copy_image(p
->dmpi
, mpi
, 2);
477 ret
= do_put_image(vf
, p
->dmpi
);
478 mp_msg(MSGT_VFILTER
, MSGL_V
, "NEXT\n");
482 ret
= do_put_image(vf
, p
->dmpi
);
483 copy_image(p
->dmpi
, mpi
, 2);
484 mp_msg(MSGT_VFILTER
, MSGL_V
, "OK\n");
491 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
)
497 return vf_next_query_format(vf
, fmt
);
502 static void uninit(struct vf_instance_s
* vf
)
507 static int open(vf_instance_t
*vf
, char* args
)
510 vf
->put_image
= put_image
;
511 vf
->query_format
= query_format
;
513 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
514 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
517 if (args
) sscanf(args
, "%d", &p
->drop
);
518 block_diffs
= block_diffs_C
;
520 if(gCpuCaps
.hasMMX
) block_diffs
= block_diffs_MMX
;
525 const vf_info_t vf_info_ivtc
= {
526 "inverse telecine, take 2",