2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "cpudetect.h"
27 #include "img_format.h"
31 #include "libvo/fastmemcpy.h"
35 /* difference: total, even lines, odd lines */
37 /* noise: temporal, spacial (current), spacial (past) */
42 /* peak, relative, mean */
43 struct metrics p
, r
, m
;
47 struct frameinfo fi
[2];
50 int drop
, lastdrop
, dropnext
;
51 int inframes
, outframes
;
52 struct vf_detc_pts_buf ptsbuf
;
62 #if HAVE_MMX && HAVE_EBX_AVAILABLE
63 static void block_diffs_MMX(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
66 short out
[24]; // output buffer for the partial metrics from the mmx code
70 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
71 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
72 "pxor %%mm7, %%mm7 \n\t" // all zeros
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, %%mm4 \n\t"
92 "paddw %%mm1, %%mm4 \n\t"
93 "paddw %%mm2, %%mm4 \n\t"
94 "paddw %%mm3, %%mm4 \n\t"
97 "movq (%%"REG_S
"), %%mm0 \n\t"
98 "movq (%%"REG_S
"), %%mm2 \n\t"
99 "add %%"REG_a
", %%"REG_S
" \n\t"
100 "movq (%%"REG_D
"), %%mm1 \n\t"
101 "add %%"REG_b
", %%"REG_D
" \n\t"
102 "psubusb %%mm1, %%mm2 \n\t"
103 "psubusb %%mm0, %%mm1 \n\t"
104 "movq %%mm2, %%mm0 \n\t"
105 "movq %%mm1, %%mm3 \n\t"
106 "punpcklbw %%mm7, %%mm0 \n\t"
107 "punpcklbw %%mm7, %%mm1 \n\t"
108 "punpckhbw %%mm7, %%mm2 \n\t"
109 "punpckhbw %%mm7, %%mm3 \n\t"
110 "paddw %%mm0, %%mm5 \n\t"
111 "paddw %%mm1, %%mm5 \n\t"
112 "paddw %%mm2, %%mm5 \n\t"
113 "paddw %%mm3, %%mm5 \n\t"
117 "movq %%mm4, (%%"REG_d
") \n\t"
118 "movq %%mm5, 8(%%"REG_d
") \n\t"
120 : "S" (old
), "D" (new), "a" (os
), "b" (ns
), "d" (out
)
123 m
->e
= out
[0]+out
[1]+out
[2]+out
[3];
124 m
->o
= out
[4]+out
[5]+out
[6]+out
[7];
128 // First loop to measure first four columns
129 "movl $4, %%ecx \n\t"
130 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
131 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
132 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
137 "movq (%%"REG_S
"), %%mm0 \n\t"
138 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
139 "add %%"REG_a
", %%"REG_S
" \n\t"
140 "add %%"REG_a
", %%"REG_S
" \n\t"
141 "movq (%%"REG_D
"), %%mm2 \n\t"
142 "movq (%%"REG_D
",%%"REG_b
"), %%mm3 \n\t"
143 "add %%"REG_b
", %%"REG_D
" \n\t"
144 "add %%"REG_b
", %%"REG_D
" \n\t"
145 "punpcklbw %%mm7, %%mm0 \n\t"
146 "punpcklbw %%mm7, %%mm1 \n\t"
147 "punpcklbw %%mm7, %%mm2 \n\t"
148 "punpcklbw %%mm7, %%mm3 \n\t"
149 "paddw %%mm1, %%mm4 \n\t"
150 "paddw %%mm1, %%mm5 \n\t"
151 "paddw %%mm3, %%mm6 \n\t"
152 "psubw %%mm0, %%mm4 \n\t"
153 "psubw %%mm2, %%mm5 \n\t"
154 "psubw %%mm2, %%mm6 \n\t"
159 "movq %%mm0, %%mm1 \n\t"
160 "movq %%mm0, %%mm2 \n\t"
161 "movq %%mm0, %%mm3 \n\t"
162 "pcmpgtw %%mm4, %%mm1 \n\t"
163 "pcmpgtw %%mm5, %%mm2 \n\t"
164 "pcmpgtw %%mm6, %%mm3 \n\t"
165 "pxor %%mm1, %%mm4 \n\t"
166 "pxor %%mm2, %%mm5 \n\t"
167 "pxor %%mm3, %%mm6 \n\t"
168 "psubw %%mm1, %%mm4 \n\t"
169 "psubw %%mm2, %%mm5 \n\t"
170 "psubw %%mm3, %%mm6 \n\t"
171 "movq %%mm4, (%%"REG_d
") \n\t"
172 "movq %%mm5, 16(%%"REG_d
") \n\t"
173 "movq %%mm6, 32(%%"REG_d
") \n\t"
175 "mov %%"REG_a
", %%"REG_c
" \n\t"
176 "shl $3, %%"REG_c
" \n\t"
177 "sub %%"REG_c
", %%"REG_S
" \n\t"
178 "mov %%"REG_b
", %%"REG_c
" \n\t"
179 "shl $3, %%"REG_c
" \n\t"
180 "sub %%"REG_c
", %%"REG_D
" \n\t"
182 // Second loop for the last four columns
183 "movl $4, %%ecx \n\t"
184 "pxor %%mm4, %%mm4 \n\t"
185 "pxor %%mm5, %%mm5 \n\t"
186 "pxor %%mm6, %%mm6 \n\t"
191 "movq (%%"REG_S
"), %%mm0 \n\t"
192 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
193 "add %%"REG_a
", %%"REG_S
" \n\t"
194 "add %%"REG_a
", %%"REG_S
" \n\t"
195 "movq (%%"REG_D
"), %%mm2 \n\t"
196 "movq (%%"REG_D
",%%"REG_b
"), %%mm3 \n\t"
197 "add %%"REG_b
", %%"REG_D
" \n\t"
198 "add %%"REG_b
", %%"REG_D
" \n\t"
199 "punpckhbw %%mm7, %%mm0 \n\t"
200 "punpckhbw %%mm7, %%mm1 \n\t"
201 "punpckhbw %%mm7, %%mm2 \n\t"
202 "punpckhbw %%mm7, %%mm3 \n\t"
203 "paddw %%mm1, %%mm4 \n\t"
204 "paddw %%mm1, %%mm5 \n\t"
205 "paddw %%mm3, %%mm6 \n\t"
206 "psubw %%mm0, %%mm4 \n\t"
207 "psubw %%mm2, %%mm5 \n\t"
208 "psubw %%mm2, %%mm6 \n\t"
213 "movq %%mm0, %%mm1 \n\t"
214 "movq %%mm0, %%mm2 \n\t"
215 "movq %%mm0, %%mm3 \n\t"
216 "pcmpgtw %%mm4, %%mm1 \n\t"
217 "pcmpgtw %%mm5, %%mm2 \n\t"
218 "pcmpgtw %%mm6, %%mm3 \n\t"
219 "pxor %%mm1, %%mm4 \n\t"
220 "pxor %%mm2, %%mm5 \n\t"
221 "pxor %%mm3, %%mm6 \n\t"
222 "psubw %%mm1, %%mm4 \n\t"
223 "psubw %%mm2, %%mm5 \n\t"
224 "psubw %%mm3, %%mm6 \n\t"
225 "movq %%mm4, 8(%%"REG_d
") \n\t"
226 "movq %%mm5, 24(%%"REG_d
") \n\t"
227 "movq %%mm6, 40(%%"REG_d
") \n\t"
231 : "S" (old
), "D" (new), "a" ((long)os
), "b" ((long)ns
), "d" (out
)
234 m
->p
= m
->t
= m
->s
= 0;
235 for (i
=0; i
<8; i
++) {
240 //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);
244 //#define MAG(a) ((a)*(a))
245 //#define MAG(a) (abs(a))
246 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
248 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
249 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
250 #define LOWPASS(s) ((s)[0])
253 static void block_diffs_C(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
255 int x
, y
, e
=0, o
=0, s
=0, p
=0, t
=0;
256 unsigned char *oldp
, *newp
;
257 m
->s
= m
->p
= m
->t
= 0;
258 for (x
= 8; x
; x
--) {
262 for (y
= 4; y
; y
--) {
263 e
+= MAG(newp
[0]-oldp
[0]);
264 o
+= MAG(newp
[ns
]-oldp
[os
]);
265 s
+= newp
[ns
]-newp
[0];
266 p
+= oldp
[os
]-oldp
[0];
267 t
+= oldp
[os
]-newp
[0];
280 static void (*block_diffs
)(struct metrics
*, unsigned char *, unsigned char *, int, int);
282 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
284 static void diff_planes(struct frameinfo
*fi
,
285 unsigned char *old
, unsigned char *new, int w
, int h
, int os
, int ns
)
289 struct metrics
*peak
=&fi
->p
, *rel
=&fi
->r
, *mean
=&fi
->m
;
290 memset(peak
, 0, sizeof(struct metrics
));
291 memset(rel
, 0, sizeof(struct metrics
));
292 memset(mean
, 0, sizeof(struct metrics
));
293 for (y
= 0; y
< h
-7; y
+= 8) {
294 for (x
= 8; x
< w
-8-7; x
+= 8) {
295 block_diffs(&l
, old
+x
+y
*os
, new+x
+y
*ns
, os
, ns
);
308 MAXUP(rel
->e
, l
.e
-l
.o
);
309 MAXUP(rel
->o
, l
.o
-l
.e
);
310 MAXUP(rel
->s
, l
.s
-l
.t
);
311 MAXUP(rel
->p
, l
.p
-l
.t
);
312 MAXUP(rel
->t
, l
.t
-l
.p
);
313 MAXUP(rel
->d
, l
.t
-l
.s
); /* hack */
325 static void diff_fields(struct frameinfo
*fi
, mp_image_t
*old
, mp_image_t
*new)
327 diff_planes(fi
, old
->planes
[0], new->planes
[0],
328 new->w
, new->h
, old
->stride
[0], new->stride
[0]);
331 static void stats(struct frameinfo
*f
)
333 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",
334 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
);
337 static int foo(struct vf_priv_s
*p
, mp_image_t
*new, mp_image_t
*cur
)
339 struct frameinfo
*f
= p
->fi
;
342 diff_fields(&f
[1], cur
, new);
345 // Immediately drop this frame if it's already been used.
351 // Sometimes a pulldown frame comes all by itself, so both
352 // its top and bottom field are duplicates from the adjacent
353 // two frames. We can just drop such a frame, but we
354 // immediately show the next frame instead to keep the frame
355 // drops evenly spaced during normal 3:2 pulldown sequences.
356 if ((3*f
[1].r
.o
< f
[1].r
.e
) && (f
[1].r
.s
< f
[1].r
.d
)) {
361 // If none of these conditions hold, we will consider the frame
362 // progressive and just show it as-is.
363 if (!( (3*f
[0].r
.e
< f
[0].r
.o
) ||
364 ((2*f
[0].r
.d
< f
[0].r
.s
) && (f
[0].r
.s
> 1200)) ||
365 ((2*f
[1].r
.t
< f
[1].r
.p
) && (f
[1].r
.p
> 1200)) ))
368 // Otherwise, we have to decide whether to merge or drop.
369 // If the noise metric only increases minimally, we're off
370 // to a good start...
371 if (((2*f
[1].r
.t
< 3*f
[1].r
.p
) && (f
[1].r
.t
< 3600)) ||
372 (f
[1].r
.t
< 900) || (f
[1].r
.d
< 900)) {
373 // ...and if noise decreases or the duplicate even field
374 // is detected, we go ahead with the merge.
375 if ((3*f
[0].r
.e
< f
[0].r
.o
) || (2*f
[1].r
.t
< f
[1].r
.p
)) {
385 static void copy_image(mp_image_t
*dmpi
, mp_image_t
*mpi
, int field
)
389 my_memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
/2,
390 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
391 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
392 my_memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
393 mpi
->chroma_width
, mpi
->chroma_height
/2,
394 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
395 my_memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
396 mpi
->chroma_width
, mpi
->chroma_height
/2,
397 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
401 my_memcpy_pic(dmpi
->planes
[0]+dmpi
->stride
[0],
402 mpi
->planes
[0]+mpi
->stride
[0], mpi
->w
, mpi
->h
/2,
403 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
404 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
405 my_memcpy_pic(dmpi
->planes
[1]+dmpi
->stride
[1],
406 mpi
->planes
[1]+mpi
->stride
[1],
407 mpi
->chroma_width
, mpi
->chroma_height
/2,
408 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
409 my_memcpy_pic(dmpi
->planes
[2]+dmpi
->stride
[2],
410 mpi
->planes
[2]+mpi
->stride
[2],
411 mpi
->chroma_width
, mpi
->chroma_height
/2,
412 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
416 memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
,
417 dmpi
->stride
[0], mpi
->stride
[0]);
418 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
419 memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
420 mpi
->chroma_width
, mpi
->chroma_height
,
421 dmpi
->stride
[1], mpi
->stride
[1]);
422 memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
423 mpi
->chroma_width
, mpi
->chroma_height
,
424 dmpi
->stride
[2], mpi
->stride
[2]);
430 static int do_put_image(struct vf_instance
*vf
, mp_image_t
*dmpi
, double pts
)
432 struct vf_priv_s
*p
= vf
->priv
;
435 if (!p
->dropnext
) switch (p
->drop
) {
440 dropflag
= (++p
->lastdrop
>= 5);
443 dropflag
= (++p
->lastdrop
>= 5) && (4*p
->inframes
<= 5*p
->outframes
);
448 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
449 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
450 mp_msg(MSGT_VFILTER
, MSGL_V
, "!");
452 vf_detc_adjust_pts(&p
->ptsbuf
, pts
, 0, 1);
457 return vf_next_put_image(vf
, dmpi
, vf_detc_adjust_pts(&p
->ptsbuf
, pts
, 0, 0));
460 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
463 struct vf_priv_s
*p
= vf
->priv
;
467 if (p
->first
) { /* hack */
469 vf_detc_adjust_pts(&p
->ptsbuf
, pts
, 0, 1);
473 if (!p
->dmpi
) p
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
474 MP_IMGTYPE_STATIC
, MP_IMGFLAG_ACCEPT_STRIDE
|
475 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
476 mpi
->width
, mpi
->height
);
477 /* FIXME -- not correct, off by one frame! */
478 p
->dmpi
->qscale
= mpi
->qscale
;
479 p
->dmpi
->qstride
= mpi
->qstride
;
480 p
->dmpi
->qscale_type
= mpi
->qscale_type
;
482 switch (foo(p
, mpi
, p
->dmpi
)) {
484 copy_image(p
->dmpi
, mpi
, 2);
487 mp_msg(MSGT_VFILTER
, MSGL_V
, "DROP\n");
488 vf_detc_adjust_pts(&p
->ptsbuf
, pts
, 0, 1);
491 copy_image(p
->dmpi
, mpi
, 0);
492 ret
= do_put_image(vf
, p
->dmpi
, pts
);
493 copy_image(p
->dmpi
, mpi
, 1);
494 mp_msg(MSGT_VFILTER
, MSGL_V
, "MERGE\n");
498 copy_image(p
->dmpi
, mpi
, 2);
499 ret
= do_put_image(vf
, p
->dmpi
, pts
);
500 mp_msg(MSGT_VFILTER
, MSGL_V
, "NEXT\n");
504 ret
= do_put_image(vf
, p
->dmpi
, pts
);
505 copy_image(p
->dmpi
, mpi
, 2);
506 mp_msg(MSGT_VFILTER
, MSGL_V
, "OK\n");
513 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
519 return vf_next_query_format(vf
, fmt
);
524 static void uninit(struct vf_instance
*vf
)
529 static int vf_open(vf_instance_t
*vf
, char *args
)
532 vf
->put_image
= put_image
;
533 vf
->query_format
= query_format
;
535 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
536 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
539 if (args
) sscanf(args
, "%d", &p
->drop
);
540 block_diffs
= block_diffs_C
;
541 #if HAVE_MMX && HAVE_EBX_AVAILABLE
542 if(gCpuCaps
.hasMMX
) block_diffs
= block_diffs_MMX
;
544 vf_detc_init_pts_buf(&p
->ptsbuf
);
548 const vf_info_t vf_info_ivtc
= {
549 "inverse telecine, take 2",