10 #include "img_format.h"
14 #include "libvo/fastmemcpy.h"
18 /* difference: total, even lines, odd lines */
20 /* noise: temporal, spacial (current), spacial (past) */
25 /* peak, relative, mean */
26 struct metrics p
, r
, m
;
30 struct frameinfo fi
[2];
33 int drop
, lastdrop
, dropnext
;
34 int inframes
, outframes
;
44 static inline void *my_memcpy_pic(void * dst
, void * src
, int bytesPerLine
, int height
, int dstStride
, int srcStride
)
49 for(i
=0; i
<height
; i
++)
51 memcpy(dst
, src
, bytesPerLine
);
60 static void block_diffs_MMX(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
63 short out
[24]; // output buffer for the partial metrics from the mmx code
67 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
68 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
69 "pxor %%mm7, %%mm7 \n\t" // all zeros
75 "movq (%%"REG_S
"), %%mm0 \n\t"
76 "movq (%%"REG_S
"), %%mm2 \n\t"
77 "add %%"REG_a
", %%"REG_S
" \n\t"
78 "movq (%%"REG_D
"), %%mm1 \n\t"
79 "add %%"REG_b
", %%"REG_D
" \n\t"
80 "psubusb %%mm1, %%mm2 \n\t"
81 "psubusb %%mm0, %%mm1 \n\t"
82 "movq %%mm2, %%mm0 \n\t"
83 "movq %%mm1, %%mm3 \n\t"
84 "punpcklbw %%mm7, %%mm0 \n\t"
85 "punpcklbw %%mm7, %%mm1 \n\t"
86 "punpckhbw %%mm7, %%mm2 \n\t"
87 "punpckhbw %%mm7, %%mm3 \n\t"
88 "paddw %%mm0, %%mm4 \n\t"
89 "paddw %%mm1, %%mm4 \n\t"
90 "paddw %%mm2, %%mm4 \n\t"
91 "paddw %%mm3, %%mm4 \n\t"
94 "movq (%%"REG_S
"), %%mm0 \n\t"
95 "movq (%%"REG_S
"), %%mm2 \n\t"
96 "add %%"REG_a
", %%"REG_S
" \n\t"
97 "movq (%%"REG_D
"), %%mm1 \n\t"
98 "add %%"REG_b
", %%"REG_D
" \n\t"
99 "psubusb %%mm1, %%mm2 \n\t"
100 "psubusb %%mm0, %%mm1 \n\t"
101 "movq %%mm2, %%mm0 \n\t"
102 "movq %%mm1, %%mm3 \n\t"
103 "punpcklbw %%mm7, %%mm0 \n\t"
104 "punpcklbw %%mm7, %%mm1 \n\t"
105 "punpckhbw %%mm7, %%mm2 \n\t"
106 "punpckhbw %%mm7, %%mm3 \n\t"
107 "paddw %%mm0, %%mm5 \n\t"
108 "paddw %%mm1, %%mm5 \n\t"
109 "paddw %%mm2, %%mm5 \n\t"
110 "paddw %%mm3, %%mm5 \n\t"
114 "movq %%mm4, (%%"REG_d
") \n\t"
115 "movq %%mm5, 8(%%"REG_d
") \n\t"
117 : "S" (old
), "D" (new), "a" (os
), "b" (ns
), "d" (out
)
120 m
->e
= out
[0]+out
[1]+out
[2]+out
[3];
121 m
->o
= out
[4]+out
[5]+out
[6]+out
[7];
125 // First loop to measure first four columns
126 "movl $4, %%ecx \n\t"
127 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
128 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
129 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
134 "movq (%%"REG_S
"), %%mm0 \n\t"
135 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
136 "add %%"REG_a
", %%"REG_S
" \n\t"
137 "add %%"REG_a
", %%"REG_S
" \n\t"
138 "movq (%%"REG_D
"), %%mm2 \n\t"
139 "movq (%%"REG_D
",%%"REG_b
"), %%mm3 \n\t"
140 "add %%"REG_b
", %%"REG_D
" \n\t"
141 "add %%"REG_b
", %%"REG_D
" \n\t"
142 "punpcklbw %%mm7, %%mm0 \n\t"
143 "punpcklbw %%mm7, %%mm1 \n\t"
144 "punpcklbw %%mm7, %%mm2 \n\t"
145 "punpcklbw %%mm7, %%mm3 \n\t"
146 "paddw %%mm1, %%mm4 \n\t"
147 "paddw %%mm1, %%mm5 \n\t"
148 "paddw %%mm3, %%mm6 \n\t"
149 "psubw %%mm0, %%mm4 \n\t"
150 "psubw %%mm2, %%mm5 \n\t"
151 "psubw %%mm2, %%mm6 \n\t"
156 "movq %%mm0, %%mm1 \n\t"
157 "movq %%mm0, %%mm2 \n\t"
158 "movq %%mm0, %%mm3 \n\t"
159 "pcmpgtw %%mm4, %%mm1 \n\t"
160 "pcmpgtw %%mm5, %%mm2 \n\t"
161 "pcmpgtw %%mm6, %%mm3 \n\t"
162 "pxor %%mm1, %%mm4 \n\t"
163 "pxor %%mm2, %%mm5 \n\t"
164 "pxor %%mm3, %%mm6 \n\t"
165 "psubw %%mm1, %%mm4 \n\t"
166 "psubw %%mm2, %%mm5 \n\t"
167 "psubw %%mm3, %%mm6 \n\t"
168 "movq %%mm4, (%%"REG_d
") \n\t"
169 "movq %%mm5, 16(%%"REG_d
") \n\t"
170 "movq %%mm6, 32(%%"REG_d
") \n\t"
172 "mov %%"REG_a
", %%"REG_c
" \n\t"
173 "shl $3, %%"REG_c
" \n\t"
174 "sub %%"REG_c
", %%"REG_S
" \n\t"
175 "mov %%"REG_b
", %%"REG_c
" \n\t"
176 "shl $3, %%"REG_c
" \n\t"
177 "sub %%"REG_c
", %%"REG_D
" \n\t"
179 // Second loop for the last four columns
180 "movl $4, %%ecx \n\t"
181 "pxor %%mm4, %%mm4 \n\t"
182 "pxor %%mm5, %%mm5 \n\t"
183 "pxor %%mm6, %%mm6 \n\t"
188 "movq (%%"REG_S
"), %%mm0 \n\t"
189 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
190 "add %%"REG_a
", %%"REG_S
" \n\t"
191 "add %%"REG_a
", %%"REG_S
" \n\t"
192 "movq (%%"REG_D
"), %%mm2 \n\t"
193 "movq (%%"REG_D
",%%"REG_b
"), %%mm3 \n\t"
194 "add %%"REG_b
", %%"REG_D
" \n\t"
195 "add %%"REG_b
", %%"REG_D
" \n\t"
196 "punpckhbw %%mm7, %%mm0 \n\t"
197 "punpckhbw %%mm7, %%mm1 \n\t"
198 "punpckhbw %%mm7, %%mm2 \n\t"
199 "punpckhbw %%mm7, %%mm3 \n\t"
200 "paddw %%mm1, %%mm4 \n\t"
201 "paddw %%mm1, %%mm5 \n\t"
202 "paddw %%mm3, %%mm6 \n\t"
203 "psubw %%mm0, %%mm4 \n\t"
204 "psubw %%mm2, %%mm5 \n\t"
205 "psubw %%mm2, %%mm6 \n\t"
210 "movq %%mm0, %%mm1 \n\t"
211 "movq %%mm0, %%mm2 \n\t"
212 "movq %%mm0, %%mm3 \n\t"
213 "pcmpgtw %%mm4, %%mm1 \n\t"
214 "pcmpgtw %%mm5, %%mm2 \n\t"
215 "pcmpgtw %%mm6, %%mm3 \n\t"
216 "pxor %%mm1, %%mm4 \n\t"
217 "pxor %%mm2, %%mm5 \n\t"
218 "pxor %%mm3, %%mm6 \n\t"
219 "psubw %%mm1, %%mm4 \n\t"
220 "psubw %%mm2, %%mm5 \n\t"
221 "psubw %%mm3, %%mm6 \n\t"
222 "movq %%mm4, 8(%%"REG_d
") \n\t"
223 "movq %%mm5, 24(%%"REG_d
") \n\t"
224 "movq %%mm6, 40(%%"REG_d
") \n\t"
228 : "S" (old
), "D" (new), "a" ((long)os
), "b" ((long)ns
), "d" (out
)
231 m
->p
= m
->t
= m
->s
= 0;
232 for (i
=0; i
<8; i
++) {
237 //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);
241 //#define MAG(a) ((a)*(a))
242 //#define MAG(a) (abs(a))
243 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
245 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
246 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
247 #define LOWPASS(s) ((s)[0])
250 static void block_diffs_C(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
252 int x
, y
, e
=0, o
=0, s
=0, p
=0, t
=0;
253 unsigned char *oldp
, *newp
;
254 m
->s
= m
->p
= m
->t
= 0;
255 for (x
= 8; x
; x
--) {
259 for (y
= 4; y
; y
--) {
260 e
+= MAG(newp
[0]-oldp
[0]);
261 o
+= MAG(newp
[ns
]-oldp
[os
]);
262 s
+= newp
[ns
]-newp
[0];
263 p
+= oldp
[os
]-oldp
[0];
264 t
+= oldp
[os
]-newp
[0];
277 static void (*block_diffs
)(struct metrics
*, unsigned char *, unsigned char *, int, int);
279 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
281 static void diff_planes(struct frameinfo
*fi
,
282 unsigned char *old
, unsigned char *new, int w
, int h
, int os
, int ns
)
286 struct metrics
*peak
=&fi
->p
, *rel
=&fi
->r
, *mean
=&fi
->m
;
287 memset(peak
, 0, sizeof(struct metrics
));
288 memset(rel
, 0, sizeof(struct metrics
));
289 memset(mean
, 0, sizeof(struct metrics
));
290 for (y
= 0; y
< h
-7; y
+= 8) {
291 for (x
= 8; x
< w
-8-7; x
+= 8) {
292 block_diffs(&l
, old
+x
+y
*os
, new+x
+y
*ns
, os
, ns
);
305 MAXUP(rel
->e
, l
.e
-l
.o
);
306 MAXUP(rel
->o
, l
.o
-l
.e
);
307 MAXUP(rel
->s
, l
.s
-l
.t
);
308 MAXUP(rel
->p
, l
.p
-l
.t
);
309 MAXUP(rel
->t
, l
.t
-l
.p
);
310 MAXUP(rel
->d
, l
.t
-l
.s
); /* hack */
322 static void diff_fields(struct frameinfo
*fi
, mp_image_t
*old
, mp_image_t
*new)
324 diff_planes(fi
, old
->planes
[0], new->planes
[0],
325 new->w
, new->h
, old
->stride
[0], new->stride
[0]);
328 static void stats(struct frameinfo
*f
)
330 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",
331 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
);
334 static int foo(struct vf_priv_s
*p
, mp_image_t
*new, mp_image_t
*cur
)
336 struct frameinfo
*f
= p
->fi
;
339 diff_fields(&f
[1], cur
, new);
342 // Immediately drop this frame if it's already been used.
348 // Sometimes a pulldown frame comes all by itself, so both
349 // its top and bottom field are duplicates from the adjacent
350 // two frames. We can just drop such a frame, but we
351 // immediately show the next frame instead to keep the frame
352 // drops evenly spaced during normal 3:2 pulldown sequences.
353 if ((3*f
[1].r
.o
< f
[1].r
.e
) && (f
[1].r
.s
< f
[1].r
.d
)) {
358 // If none of these conditions hold, we will consider the frame
359 // progressive and just show it as-is.
360 if (!( (3*f
[0].r
.e
< f
[0].r
.o
) ||
361 ((2*f
[0].r
.d
< f
[0].r
.s
) && (f
[0].r
.s
> 1200)) ||
362 ((2*f
[1].r
.t
< f
[1].r
.p
) && (f
[1].r
.p
> 1200)) ))
365 // Otherwise, we have to decide whether to merge or drop.
366 // If the noise metric only increases minimally, we're off
367 // to a good start...
368 if (((2*f
[1].r
.t
< 3*f
[1].r
.p
) && (f
[1].r
.t
< 3600)) ||
369 (f
[1].r
.t
< 900) || (f
[1].r
.d
< 900)) {
370 // ...and if noise decreases or the duplicate even field
371 // is detected, we go ahead with the merge.
372 if ((3*f
[0].r
.e
< f
[0].r
.o
) || (2*f
[1].r
.t
< f
[1].r
.p
)) {
382 static void copy_image(mp_image_t
*dmpi
, mp_image_t
*mpi
, int field
)
386 my_memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
/2,
387 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
388 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
389 my_memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
390 mpi
->chroma_width
, mpi
->chroma_height
/2,
391 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
392 my_memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
393 mpi
->chroma_width
, mpi
->chroma_height
/2,
394 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
398 my_memcpy_pic(dmpi
->planes
[0]+dmpi
->stride
[0],
399 mpi
->planes
[0]+mpi
->stride
[0], mpi
->w
, mpi
->h
/2,
400 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
401 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
402 my_memcpy_pic(dmpi
->planes
[1]+dmpi
->stride
[1],
403 mpi
->planes
[1]+mpi
->stride
[1],
404 mpi
->chroma_width
, mpi
->chroma_height
/2,
405 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
406 my_memcpy_pic(dmpi
->planes
[2]+dmpi
->stride
[2],
407 mpi
->planes
[2]+mpi
->stride
[2],
408 mpi
->chroma_width
, mpi
->chroma_height
/2,
409 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
413 memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
,
414 dmpi
->stride
[0], mpi
->stride
[0]);
415 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
416 memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
417 mpi
->chroma_width
, mpi
->chroma_height
,
418 dmpi
->stride
[1], mpi
->stride
[1]);
419 memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
420 mpi
->chroma_width
, mpi
->chroma_height
,
421 dmpi
->stride
[2], mpi
->stride
[2]);
427 static int do_put_image(struct vf_instance_s
* vf
, mp_image_t
*dmpi
)
429 struct vf_priv_s
*p
= vf
->priv
;
432 if (!p
->dropnext
) switch (p
->drop
) {
437 dropflag
= (++p
->lastdrop
>= 5);
440 dropflag
= (++p
->lastdrop
>= 5) && (4*p
->inframes
<= 5*p
->outframes
);
445 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
446 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
447 mp_msg(MSGT_VFILTER
, MSGL_V
, "!");
453 return vf_next_put_image(vf
, dmpi
, MP_NOPTS_VALUE
);
456 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
)
459 struct vf_priv_s
*p
= vf
->priv
;
463 if (p
->first
) { /* hack */
468 if (!p
->dmpi
) p
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
469 MP_IMGTYPE_STATIC
, MP_IMGFLAG_ACCEPT_STRIDE
|
470 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
471 mpi
->width
, mpi
->height
);
472 /* FIXME -- not correct, off by one frame! */
473 p
->dmpi
->qscale
= mpi
->qscale
;
474 p
->dmpi
->qstride
= mpi
->qstride
;
475 p
->dmpi
->qscale_type
= mpi
->qscale_type
;
477 switch (foo(p
, mpi
, p
->dmpi
)) {
479 copy_image(p
->dmpi
, mpi
, 2);
482 mp_msg(MSGT_VFILTER
, MSGL_V
, "DROP\n");
485 copy_image(p
->dmpi
, mpi
, 0);
486 ret
= do_put_image(vf
, p
->dmpi
);
487 copy_image(p
->dmpi
, mpi
, 1);
488 mp_msg(MSGT_VFILTER
, MSGL_V
, "MERGE\n");
492 copy_image(p
->dmpi
, mpi
, 2);
493 ret
= do_put_image(vf
, p
->dmpi
);
494 mp_msg(MSGT_VFILTER
, MSGL_V
, "NEXT\n");
498 ret
= do_put_image(vf
, p
->dmpi
);
499 copy_image(p
->dmpi
, mpi
, 2);
500 mp_msg(MSGT_VFILTER
, MSGL_V
, "OK\n");
507 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
)
513 return vf_next_query_format(vf
, fmt
);
518 static void uninit(struct vf_instance_s
* vf
)
523 static int open(vf_instance_t
*vf
, char* args
)
526 vf
->put_image
= put_image
;
527 vf
->query_format
= query_format
;
529 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
530 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
533 if (args
) sscanf(args
, "%d", &p
->drop
);
534 block_diffs
= block_diffs_C
;
536 if(gCpuCaps
.hasMMX
) block_diffs
= block_diffs_MMX
;
541 vf_info_t vf_info_ivtc
= {
542 "inverse telecine, take 2",