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
;
61 #if HAVE_MMX && HAVE_EBX_AVAILABLE
62 static void block_diffs_MMX(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
65 short out
[24]; // output buffer for the partial metrics from the mmx code
69 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
70 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
71 "pxor %%mm7, %%mm7 \n\t" // all zeros
77 "movq (%%"REG_S
"), %%mm0 \n\t"
78 "movq (%%"REG_S
"), %%mm2 \n\t"
79 "add %%"REG_a
", %%"REG_S
" \n\t"
80 "movq (%%"REG_D
"), %%mm1 \n\t"
81 "add %%"REG_b
", %%"REG_D
" \n\t"
82 "psubusb %%mm1, %%mm2 \n\t"
83 "psubusb %%mm0, %%mm1 \n\t"
84 "movq %%mm2, %%mm0 \n\t"
85 "movq %%mm1, %%mm3 \n\t"
86 "punpcklbw %%mm7, %%mm0 \n\t"
87 "punpcklbw %%mm7, %%mm1 \n\t"
88 "punpckhbw %%mm7, %%mm2 \n\t"
89 "punpckhbw %%mm7, %%mm3 \n\t"
90 "paddw %%mm0, %%mm4 \n\t"
91 "paddw %%mm1, %%mm4 \n\t"
92 "paddw %%mm2, %%mm4 \n\t"
93 "paddw %%mm3, %%mm4 \n\t"
96 "movq (%%"REG_S
"), %%mm0 \n\t"
97 "movq (%%"REG_S
"), %%mm2 \n\t"
98 "add %%"REG_a
", %%"REG_S
" \n\t"
99 "movq (%%"REG_D
"), %%mm1 \n\t"
100 "add %%"REG_b
", %%"REG_D
" \n\t"
101 "psubusb %%mm1, %%mm2 \n\t"
102 "psubusb %%mm0, %%mm1 \n\t"
103 "movq %%mm2, %%mm0 \n\t"
104 "movq %%mm1, %%mm3 \n\t"
105 "punpcklbw %%mm7, %%mm0 \n\t"
106 "punpcklbw %%mm7, %%mm1 \n\t"
107 "punpckhbw %%mm7, %%mm2 \n\t"
108 "punpckhbw %%mm7, %%mm3 \n\t"
109 "paddw %%mm0, %%mm5 \n\t"
110 "paddw %%mm1, %%mm5 \n\t"
111 "paddw %%mm2, %%mm5 \n\t"
112 "paddw %%mm3, %%mm5 \n\t"
116 "movq %%mm4, (%%"REG_d
") \n\t"
117 "movq %%mm5, 8(%%"REG_d
") \n\t"
119 : "S" (old
), "D" (new), "a" (os
), "b" (ns
), "d" (out
)
122 m
->e
= out
[0]+out
[1]+out
[2]+out
[3];
123 m
->o
= out
[4]+out
[5]+out
[6]+out
[7];
127 // First loop to measure first four columns
128 "movl $4, %%ecx \n\t"
129 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
130 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
131 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
136 "movq (%%"REG_S
"), %%mm0 \n\t"
137 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
138 "add %%"REG_a
", %%"REG_S
" \n\t"
139 "add %%"REG_a
", %%"REG_S
" \n\t"
140 "movq (%%"REG_D
"), %%mm2 \n\t"
141 "movq (%%"REG_D
",%%"REG_b
"), %%mm3 \n\t"
142 "add %%"REG_b
", %%"REG_D
" \n\t"
143 "add %%"REG_b
", %%"REG_D
" \n\t"
144 "punpcklbw %%mm7, %%mm0 \n\t"
145 "punpcklbw %%mm7, %%mm1 \n\t"
146 "punpcklbw %%mm7, %%mm2 \n\t"
147 "punpcklbw %%mm7, %%mm3 \n\t"
148 "paddw %%mm1, %%mm4 \n\t"
149 "paddw %%mm1, %%mm5 \n\t"
150 "paddw %%mm3, %%mm6 \n\t"
151 "psubw %%mm0, %%mm4 \n\t"
152 "psubw %%mm2, %%mm5 \n\t"
153 "psubw %%mm2, %%mm6 \n\t"
158 "movq %%mm0, %%mm1 \n\t"
159 "movq %%mm0, %%mm2 \n\t"
160 "movq %%mm0, %%mm3 \n\t"
161 "pcmpgtw %%mm4, %%mm1 \n\t"
162 "pcmpgtw %%mm5, %%mm2 \n\t"
163 "pcmpgtw %%mm6, %%mm3 \n\t"
164 "pxor %%mm1, %%mm4 \n\t"
165 "pxor %%mm2, %%mm5 \n\t"
166 "pxor %%mm3, %%mm6 \n\t"
167 "psubw %%mm1, %%mm4 \n\t"
168 "psubw %%mm2, %%mm5 \n\t"
169 "psubw %%mm3, %%mm6 \n\t"
170 "movq %%mm4, (%%"REG_d
") \n\t"
171 "movq %%mm5, 16(%%"REG_d
") \n\t"
172 "movq %%mm6, 32(%%"REG_d
") \n\t"
174 "mov %%"REG_a
", %%"REG_c
" \n\t"
175 "shl $3, %%"REG_c
" \n\t"
176 "sub %%"REG_c
", %%"REG_S
" \n\t"
177 "mov %%"REG_b
", %%"REG_c
" \n\t"
178 "shl $3, %%"REG_c
" \n\t"
179 "sub %%"REG_c
", %%"REG_D
" \n\t"
181 // Second loop for the last four columns
182 "movl $4, %%ecx \n\t"
183 "pxor %%mm4, %%mm4 \n\t"
184 "pxor %%mm5, %%mm5 \n\t"
185 "pxor %%mm6, %%mm6 \n\t"
190 "movq (%%"REG_S
"), %%mm0 \n\t"
191 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
192 "add %%"REG_a
", %%"REG_S
" \n\t"
193 "add %%"REG_a
", %%"REG_S
" \n\t"
194 "movq (%%"REG_D
"), %%mm2 \n\t"
195 "movq (%%"REG_D
",%%"REG_b
"), %%mm3 \n\t"
196 "add %%"REG_b
", %%"REG_D
" \n\t"
197 "add %%"REG_b
", %%"REG_D
" \n\t"
198 "punpckhbw %%mm7, %%mm0 \n\t"
199 "punpckhbw %%mm7, %%mm1 \n\t"
200 "punpckhbw %%mm7, %%mm2 \n\t"
201 "punpckhbw %%mm7, %%mm3 \n\t"
202 "paddw %%mm1, %%mm4 \n\t"
203 "paddw %%mm1, %%mm5 \n\t"
204 "paddw %%mm3, %%mm6 \n\t"
205 "psubw %%mm0, %%mm4 \n\t"
206 "psubw %%mm2, %%mm5 \n\t"
207 "psubw %%mm2, %%mm6 \n\t"
212 "movq %%mm0, %%mm1 \n\t"
213 "movq %%mm0, %%mm2 \n\t"
214 "movq %%mm0, %%mm3 \n\t"
215 "pcmpgtw %%mm4, %%mm1 \n\t"
216 "pcmpgtw %%mm5, %%mm2 \n\t"
217 "pcmpgtw %%mm6, %%mm3 \n\t"
218 "pxor %%mm1, %%mm4 \n\t"
219 "pxor %%mm2, %%mm5 \n\t"
220 "pxor %%mm3, %%mm6 \n\t"
221 "psubw %%mm1, %%mm4 \n\t"
222 "psubw %%mm2, %%mm5 \n\t"
223 "psubw %%mm3, %%mm6 \n\t"
224 "movq %%mm4, 8(%%"REG_d
") \n\t"
225 "movq %%mm5, 24(%%"REG_d
") \n\t"
226 "movq %%mm6, 40(%%"REG_d
") \n\t"
230 : "S" (old
), "D" (new), "a" ((long)os
), "b" ((long)ns
), "d" (out
)
233 m
->p
= m
->t
= m
->s
= 0;
234 for (i
=0; i
<8; i
++) {
239 //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);
243 //#define MAG(a) ((a)*(a))
244 //#define MAG(a) (abs(a))
245 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
247 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
248 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
249 #define LOWPASS(s) ((s)[0])
252 static void block_diffs_C(struct metrics
*m
, unsigned char *old
, unsigned char *new, int os
, int ns
)
254 int x
, y
, e
=0, o
=0, s
=0, p
=0, t
=0;
255 unsigned char *oldp
, *newp
;
256 m
->s
= m
->p
= m
->t
= 0;
257 for (x
= 8; x
; x
--) {
261 for (y
= 4; y
; y
--) {
262 e
+= MAG(newp
[0]-oldp
[0]);
263 o
+= MAG(newp
[ns
]-oldp
[os
]);
264 s
+= newp
[ns
]-newp
[0];
265 p
+= oldp
[os
]-oldp
[0];
266 t
+= oldp
[os
]-newp
[0];
279 static void (*block_diffs
)(struct metrics
*, unsigned char *, unsigned char *, int, int);
281 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
283 static void diff_planes(struct frameinfo
*fi
,
284 unsigned char *old
, unsigned char *new, int w
, int h
, int os
, int ns
)
288 struct metrics
*peak
=&fi
->p
, *rel
=&fi
->r
, *mean
=&fi
->m
;
289 memset(peak
, 0, sizeof(struct metrics
));
290 memset(rel
, 0, sizeof(struct metrics
));
291 memset(mean
, 0, sizeof(struct metrics
));
292 for (y
= 0; y
< h
-7; y
+= 8) {
293 for (x
= 8; x
< w
-8-7; x
+= 8) {
294 block_diffs(&l
, old
+x
+y
*os
, new+x
+y
*ns
, os
, ns
);
307 MAXUP(rel
->e
, l
.e
-l
.o
);
308 MAXUP(rel
->o
, l
.o
-l
.e
);
309 MAXUP(rel
->s
, l
.s
-l
.t
);
310 MAXUP(rel
->p
, l
.p
-l
.t
);
311 MAXUP(rel
->t
, l
.t
-l
.p
);
312 MAXUP(rel
->d
, l
.t
-l
.s
); /* hack */
324 static void diff_fields(struct frameinfo
*fi
, mp_image_t
*old
, mp_image_t
*new)
326 diff_planes(fi
, old
->planes
[0], new->planes
[0],
327 new->w
, new->h
, old
->stride
[0], new->stride
[0]);
330 static void stats(struct frameinfo
*f
)
332 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",
333 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
);
336 static int foo(struct vf_priv_s
*p
, mp_image_t
*new, mp_image_t
*cur
)
338 struct frameinfo
*f
= p
->fi
;
341 diff_fields(&f
[1], cur
, new);
344 // Immediately drop this frame if it's already been used.
350 // Sometimes a pulldown frame comes all by itself, so both
351 // its top and bottom field are duplicates from the adjacent
352 // two frames. We can just drop such a frame, but we
353 // immediately show the next frame instead to keep the frame
354 // drops evenly spaced during normal 3:2 pulldown sequences.
355 if ((3*f
[1].r
.o
< f
[1].r
.e
) && (f
[1].r
.s
< f
[1].r
.d
)) {
360 // If none of these conditions hold, we will consider the frame
361 // progressive and just show it as-is.
362 if (!( (3*f
[0].r
.e
< f
[0].r
.o
) ||
363 ((2*f
[0].r
.d
< f
[0].r
.s
) && (f
[0].r
.s
> 1200)) ||
364 ((2*f
[1].r
.t
< f
[1].r
.p
) && (f
[1].r
.p
> 1200)) ))
367 // Otherwise, we have to decide whether to merge or drop.
368 // If the noise metric only increases minimally, we're off
369 // to a good start...
370 if (((2*f
[1].r
.t
< 3*f
[1].r
.p
) && (f
[1].r
.t
< 3600)) ||
371 (f
[1].r
.t
< 900) || (f
[1].r
.d
< 900)) {
372 // ...and if noise decreases or the duplicate even field
373 // is detected, we go ahead with the merge.
374 if ((3*f
[0].r
.e
< f
[0].r
.o
) || (2*f
[1].r
.t
< f
[1].r
.p
)) {
384 static void copy_image(mp_image_t
*dmpi
, mp_image_t
*mpi
, int field
)
388 my_memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
/2,
389 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
390 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
391 my_memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
392 mpi
->chroma_width
, mpi
->chroma_height
/2,
393 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
394 my_memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
395 mpi
->chroma_width
, mpi
->chroma_height
/2,
396 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
400 my_memcpy_pic(dmpi
->planes
[0]+dmpi
->stride
[0],
401 mpi
->planes
[0]+mpi
->stride
[0], mpi
->w
, mpi
->h
/2,
402 dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
403 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
404 my_memcpy_pic(dmpi
->planes
[1]+dmpi
->stride
[1],
405 mpi
->planes
[1]+mpi
->stride
[1],
406 mpi
->chroma_width
, mpi
->chroma_height
/2,
407 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
408 my_memcpy_pic(dmpi
->planes
[2]+dmpi
->stride
[2],
409 mpi
->planes
[2]+mpi
->stride
[2],
410 mpi
->chroma_width
, mpi
->chroma_height
/2,
411 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
415 memcpy_pic(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
, mpi
->h
,
416 dmpi
->stride
[0], mpi
->stride
[0]);
417 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
418 memcpy_pic(dmpi
->planes
[1], mpi
->planes
[1],
419 mpi
->chroma_width
, mpi
->chroma_height
,
420 dmpi
->stride
[1], mpi
->stride
[1]);
421 memcpy_pic(dmpi
->planes
[2], mpi
->planes
[2],
422 mpi
->chroma_width
, mpi
->chroma_height
,
423 dmpi
->stride
[2], mpi
->stride
[2]);
429 static int do_put_image(struct vf_instance
*vf
, mp_image_t
*dmpi
)
431 struct vf_priv_s
*p
= vf
->priv
;
434 if (!p
->dropnext
) switch (p
->drop
) {
439 dropflag
= (++p
->lastdrop
>= 5);
442 dropflag
= (++p
->lastdrop
>= 5) && (4*p
->inframes
<= 5*p
->outframes
);
447 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
448 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
449 mp_msg(MSGT_VFILTER
, MSGL_V
, "!");
455 return vf_next_put_image(vf
, dmpi
, MP_NOPTS_VALUE
);
458 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
461 struct vf_priv_s
*p
= vf
->priv
;
465 if (p
->first
) { /* hack */
470 if (!p
->dmpi
) p
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
471 MP_IMGTYPE_STATIC
, MP_IMGFLAG_ACCEPT_STRIDE
|
472 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
473 mpi
->width
, mpi
->height
);
474 /* FIXME -- not correct, off by one frame! */
475 p
->dmpi
->qscale
= mpi
->qscale
;
476 p
->dmpi
->qstride
= mpi
->qstride
;
477 p
->dmpi
->qscale_type
= mpi
->qscale_type
;
479 switch (foo(p
, mpi
, p
->dmpi
)) {
481 copy_image(p
->dmpi
, mpi
, 2);
484 mp_msg(MSGT_VFILTER
, MSGL_V
, "DROP\n");
487 copy_image(p
->dmpi
, mpi
, 0);
488 ret
= do_put_image(vf
, p
->dmpi
);
489 copy_image(p
->dmpi
, mpi
, 1);
490 mp_msg(MSGT_VFILTER
, MSGL_V
, "MERGE\n");
494 copy_image(p
->dmpi
, mpi
, 2);
495 ret
= do_put_image(vf
, p
->dmpi
);
496 mp_msg(MSGT_VFILTER
, MSGL_V
, "NEXT\n");
500 ret
= do_put_image(vf
, p
->dmpi
);
501 copy_image(p
->dmpi
, mpi
, 2);
502 mp_msg(MSGT_VFILTER
, MSGL_V
, "OK\n");
509 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
515 return vf_next_query_format(vf
, fmt
);
520 static void uninit(struct vf_instance
*vf
)
525 static int vf_open(vf_instance_t
*vf
, char *args
)
528 vf
->put_image
= put_image
;
529 vf
->query_format
= query_format
;
531 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
532 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
535 if (args
) sscanf(args
, "%d", &p
->drop
);
536 block_diffs
= block_diffs_C
;
537 #if HAVE_MMX && HAVE_EBX_AVAILABLE
538 if(gCpuCaps
.hasMMX
) block_diffs
= block_diffs_MMX
;
543 const vf_info_t vf_info_ivtc
= {
544 "inverse telecine, take 2",