2 * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "cpudetect.h"
37 #include "img_format.h"
40 #include "libvo/fastmemcpy.h"
42 #define MIN(a,b) ((a) > (b) ? (b) : (a))
43 #define MAX(a,b) ((a) < (b) ? (b) : (a))
44 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
46 #define MIN3(a,b,c) MIN(MIN(a,b),c)
47 #define MAX3(a,b,c) MAX(MAX(a,b),c)
49 //===========================================================================//
57 mp_image_t
*buffered_mpi
;
63 static void (*filter_line
)(struct vf_priv_s
*p
, uint8_t *dst
, uint8_t *prev
, uint8_t *cur
, uint8_t *next
, int w
, int refs
, int parity
);
65 static void store_ref(struct vf_priv_s
*p
, uint8_t *src
[3], int src_stride
[3], int width
, int height
){
68 memcpy (p
->ref
[3], p
->ref
[0], sizeof(uint8_t *)*3);
69 memmove(p
->ref
[0], p
->ref
[1], sizeof(uint8_t *)*3*3);
74 memcpy_pic(p
->ref
[2][i
], src
[i
], width
>>is_chroma
, height
>>is_chroma
, p
->stride
[i
], src_stride
[i
]);
78 #if defined(HAVE_MMX) && defined(NAMED_ASM_ARGS)
80 #define LOAD4(mem,dst) \
81 "movd "mem", "#dst" \n\t"\
82 "punpcklbw %%mm7, "#dst" \n\t"
84 #define PABS(tmp,dst) \
85 "pxor "#tmp", "#tmp" \n\t"\
86 "psubw "#dst", "#tmp" \n\t"\
87 "pmaxsw "#tmp", "#dst" \n\t"
89 #define CHECK(pj,mj) \
90 "movq "#pj"(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1+j] */\
91 "movq "#mj"(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1-j] */\
92 "movq %%mm2, %%mm4 \n\t"\
93 "movq %%mm2, %%mm5 \n\t"\
94 "pxor %%mm3, %%mm4 \n\t"\
95 "pavgb %%mm3, %%mm5 \n\t"\
96 "pand %[pb1], %%mm4 \n\t"\
97 "psubusb %%mm4, %%mm5 \n\t"\
98 "psrlq $8, %%mm5 \n\t"\
99 "punpcklbw %%mm7, %%mm5 \n\t" /* (cur[x-refs+j] + cur[x+refs-j])>>1 */\
100 "movq %%mm2, %%mm4 \n\t"\
101 "psubusb %%mm3, %%mm2 \n\t"\
102 "psubusb %%mm4, %%mm3 \n\t"\
103 "pmaxub %%mm3, %%mm2 \n\t"\
104 "movq %%mm2, %%mm3 \n\t"\
105 "movq %%mm2, %%mm4 \n\t" /* ABS(cur[x-refs-1+j] - cur[x+refs-1-j]) */\
106 "psrlq $8, %%mm3 \n\t" /* ABS(cur[x-refs +j] - cur[x+refs -j]) */\
107 "psrlq $16, %%mm4 \n\t" /* ABS(cur[x-refs+1+j] - cur[x+refs+1-j]) */\
108 "punpcklbw %%mm7, %%mm2 \n\t"\
109 "punpcklbw %%mm7, %%mm3 \n\t"\
110 "punpcklbw %%mm7, %%mm4 \n\t"\
111 "paddw %%mm3, %%mm2 \n\t"\
112 "paddw %%mm4, %%mm2 \n\t" /* score */
115 "movq %%mm0, %%mm3 \n\t"\
116 "pcmpgtw %%mm2, %%mm3 \n\t" /* if(score < spatial_score) */\
117 "pminsw %%mm2, %%mm0 \n\t" /* spatial_score= score; */\
118 "movq %%mm3, %%mm6 \n\t"\
119 "pand %%mm3, %%mm5 \n\t"\
120 "pandn %%mm1, %%mm3 \n\t"\
121 "por %%mm5, %%mm3 \n\t"\
122 "movq %%mm3, %%mm1 \n\t" /* spatial_pred= (cur[x-refs+j] + cur[x+refs-j])>>1; */
124 #define CHECK2 /* pretend not to have checked dir=2 if dir=1 was bad.\
125 hurts both quality and speed, but matches the C version. */\
126 "paddw %[pw1], %%mm6 \n\t"\
127 "psllw $14, %%mm6 \n\t"\
128 "paddsw %%mm6, %%mm2 \n\t"\
129 "movq %%mm0, %%mm3 \n\t"\
130 "pcmpgtw %%mm2, %%mm3 \n\t"\
131 "pminsw %%mm2, %%mm0 \n\t"\
132 "pand %%mm3, %%mm5 \n\t"\
133 "pandn %%mm1, %%mm3 \n\t"\
134 "por %%mm5, %%mm3 \n\t"\
135 "movq %%mm3, %%mm1 \n\t"
137 static void filter_line_mmx2(struct vf_priv_s
*p
, uint8_t *dst
, uint8_t *prev
, uint8_t *cur
, uint8_t *next
, int w
, int refs
, int parity
){
138 static const uint64_t pw_1
= 0x0001000100010001ULL
;
139 static const uint64_t pb_1
= 0x0101010101010101ULL
;
140 const int mode
= p
->mode
;
141 uint64_t tmp0
, tmp1
, tmp2
, tmp3
;
145 for(x=0; x<w; x+=4){\
147 "pxor %%mm7, %%mm7 \n\t"\
148 LOAD4("(%[cur],%[mrefs])", %%mm0) /* c = cur[x-refs] */\
149 LOAD4("(%[cur],%[prefs])", %%mm1) /* e = cur[x+refs] */\
150 LOAD4("(%["prev2"])", %%mm2) /* prev2[x] */\
151 LOAD4("(%["next2"])", %%mm3) /* next2[x] */\
152 "movq %%mm3, %%mm4 \n\t"\
153 "paddw %%mm2, %%mm3 \n\t"\
154 "psraw $1, %%mm3 \n\t" /* d = (prev2[x] + next2[x])>>1 */\
155 "movq %%mm0, %[tmp0] \n\t" /* c */\
156 "movq %%mm3, %[tmp1] \n\t" /* d */\
157 "movq %%mm1, %[tmp2] \n\t" /* e */\
158 "psubw %%mm4, %%mm2 \n\t"\
159 PABS( %%mm4, %%mm2) /* temporal_diff0 */\
160 LOAD4("(%[prev],%[mrefs])", %%mm3) /* prev[x-refs] */\
161 LOAD4("(%[prev],%[prefs])", %%mm4) /* prev[x+refs] */\
162 "psubw %%mm0, %%mm3 \n\t"\
163 "psubw %%mm1, %%mm4 \n\t"\
166 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff1 */\
167 "psrlw $1, %%mm2 \n\t"\
168 "psrlw $1, %%mm3 \n\t"\
169 "pmaxsw %%mm3, %%mm2 \n\t"\
170 LOAD4("(%[next],%[mrefs])", %%mm3) /* next[x-refs] */\
171 LOAD4("(%[next],%[prefs])", %%mm4) /* next[x+refs] */\
172 "psubw %%mm0, %%mm3 \n\t"\
173 "psubw %%mm1, %%mm4 \n\t"\
176 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff2 */\
177 "psrlw $1, %%mm3 \n\t"\
178 "pmaxsw %%mm3, %%mm2 \n\t"\
179 "movq %%mm2, %[tmp3] \n\t" /* diff */\
181 "paddw %%mm0, %%mm1 \n\t"\
182 "paddw %%mm0, %%mm0 \n\t"\
183 "psubw %%mm1, %%mm0 \n\t"\
184 "psrlw $1, %%mm1 \n\t" /* spatial_pred */\
185 PABS( %%mm2, %%mm0) /* ABS(c-e) */\
187 "movq -1(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1] */\
188 "movq -1(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1] */\
189 "movq %%mm2, %%mm4 \n\t"\
190 "psubusb %%mm3, %%mm2 \n\t"\
191 "psubusb %%mm4, %%mm3 \n\t"\
192 "pmaxub %%mm3, %%mm2 \n\t"\
193 "pshufw $9,%%mm2, %%mm3 \n\t"\
194 "punpcklbw %%mm7, %%mm2 \n\t" /* ABS(cur[x-refs-1] - cur[x+refs-1]) */\
195 "punpcklbw %%mm7, %%mm3 \n\t" /* ABS(cur[x-refs+1] - cur[x+refs+1]) */\
196 "paddw %%mm2, %%mm0 \n\t"\
197 "paddw %%mm3, %%mm0 \n\t"\
198 "psubw %[pw1], %%mm0 \n\t" /* spatial_score */\
209 /* if(p->mode<2) ... */\
210 "movq %[tmp3], %%mm6 \n\t" /* diff */\
211 "cmp $2, %[mode] \n\t"\
213 LOAD4("(%["prev2"],%[mrefs],2)", %%mm2) /* prev2[x-2*refs] */\
214 LOAD4("(%["next2"],%[mrefs],2)", %%mm4) /* next2[x-2*refs] */\
215 LOAD4("(%["prev2"],%[prefs],2)", %%mm3) /* prev2[x+2*refs] */\
216 LOAD4("(%["next2"],%[prefs],2)", %%mm5) /* next2[x+2*refs] */\
217 "paddw %%mm4, %%mm2 \n\t"\
218 "paddw %%mm5, %%mm3 \n\t"\
219 "psrlw $1, %%mm2 \n\t" /* b */\
220 "psrlw $1, %%mm3 \n\t" /* f */\
221 "movq %[tmp0], %%mm4 \n\t" /* c */\
222 "movq %[tmp1], %%mm5 \n\t" /* d */\
223 "movq %[tmp2], %%mm7 \n\t" /* e */\
224 "psubw %%mm4, %%mm2 \n\t" /* b-c */\
225 "psubw %%mm7, %%mm3 \n\t" /* f-e */\
226 "movq %%mm5, %%mm0 \n\t"\
227 "psubw %%mm4, %%mm5 \n\t" /* d-c */\
228 "psubw %%mm7, %%mm0 \n\t" /* d-e */\
229 "movq %%mm2, %%mm4 \n\t"\
230 "pminsw %%mm3, %%mm2 \n\t"\
231 "pmaxsw %%mm4, %%mm3 \n\t"\
232 "pmaxsw %%mm5, %%mm2 \n\t"\
233 "pminsw %%mm5, %%mm3 \n\t"\
234 "pmaxsw %%mm0, %%mm2 \n\t" /* max */\
235 "pminsw %%mm0, %%mm3 \n\t" /* min */\
236 "pxor %%mm4, %%mm4 \n\t"\
237 "pmaxsw %%mm3, %%mm6 \n\t"\
238 "psubw %%mm2, %%mm4 \n\t" /* -max */\
239 "pmaxsw %%mm4, %%mm6 \n\t" /* diff= MAX3(diff, min, -max); */\
242 "movq %[tmp1], %%mm2 \n\t" /* d */\
243 "movq %%mm2, %%mm3 \n\t"\
244 "psubw %%mm6, %%mm2 \n\t" /* d-diff */\
245 "paddw %%mm6, %%mm3 \n\t" /* d+diff */\
246 "pmaxsw %%mm2, %%mm1 \n\t"\
247 "pminsw %%mm3, %%mm1 \n\t" /* d = clip(spatial_pred, d-diff, d+diff); */\
248 "packuswb %%mm1, %%mm1 \n\t"\
257 [prefs]"r"((long)refs),\
258 [mrefs]"r"((long)-refs),\
263 asm volatile("movd %%mm1, %0" :"=m"(*dst));\
291 #endif /* defined(HAVE_MMX) && defined(NAMED_ASM_ARGS) */
293 static void filter_line_c(struct vf_priv_s
*p
, uint8_t *dst
, uint8_t *prev
, uint8_t *cur
, uint8_t *next
, int w
, int refs
, int parity
){
295 uint8_t *prev2
= parity
? prev
: cur
;
296 uint8_t *next2
= parity
? cur
: next
;
299 int d
= (prev2
[0] + next2
[0])>>1;
301 int temporal_diff0
= ABS(prev2
[0] - next2
[0]);
302 int temporal_diff1
=( ABS(prev
[-refs
] - c
) + ABS(prev
[+refs
] - e
) )>>1;
303 int temporal_diff2
=( ABS(next
[-refs
] - c
) + ABS(next
[+refs
] - e
) )>>1;
304 int diff
= MAX3(temporal_diff0
>>1, temporal_diff1
, temporal_diff2
);
305 int spatial_pred
= (c
+e
)>>1;
306 int spatial_score
= ABS(cur
[-refs
-1] - cur
[+refs
-1]) + ABS(c
-e
)
307 + ABS(cur
[-refs
+1] - cur
[+refs
+1]) - 1;
310 { int score= ABS(cur[-refs-1+j] - cur[+refs-1-j])\
311 + ABS(cur[-refs +j] - cur[+refs -j])\
312 + ABS(cur[-refs+1+j] - cur[+refs+1-j]);\
313 if(score < spatial_score){\
314 spatial_score= score;\
315 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\
317 CHECK(-1) CHECK(-2) }} }}
318 CHECK( 1) CHECK( 2) }} }}
321 int b
= (prev2
[-2*refs
] + next2
[-2*refs
])>>1;
322 int f
= (prev2
[+2*refs
] + next2
[+2*refs
])>>1;
326 int max
= MAX3(d
-e
, d
-c
, MIN3(MAX(b
-c
,f
-e
),MAX(b
-c
,b
-a
),MAX(f
-g
,f
-e
)) );
327 int min
= MIN3(d
-e
, d
-c
, MAX3(MIN(b
-c
,f
-e
),MIN(b
-c
,b
-a
),MIN(f
-g
,f
-e
)) );
329 int max
= MAX3(d
-e
, d
-c
, MIN(b
-c
, f
-e
));
330 int min
= MIN3(d
-e
, d
-c
, MAX(b
-c
, f
-e
));
333 diff
= MAX3(diff
, min
, -max
);
336 if(spatial_pred
> d
+ diff
)
337 spatial_pred
= d
+ diff
;
338 else if(spatial_pred
< d
- diff
)
339 spatial_pred
= d
- diff
;
341 dst
[0] = spatial_pred
;
352 static void filter(struct vf_priv_s
*p
, uint8_t *dst
[3], int dst_stride
[3], int width
, int height
, int parity
, int tff
){
357 int w
= width
>>is_chroma
;
358 int h
= height
>>is_chroma
;
359 int refs
= p
->stride
[i
];
362 if((y
^ parity
) & 1){
363 uint8_t *prev
= &p
->ref
[0][i
][y
*refs
];
364 uint8_t *cur
= &p
->ref
[1][i
][y
*refs
];
365 uint8_t *next
= &p
->ref
[2][i
][y
*refs
];
366 uint8_t *dst2
= &dst
[i
][y
*dst_stride
[i
]];
367 filter_line(p
, dst2
, prev
, cur
, next
, w
, refs
, parity
^ tff
);
369 fast_memcpy(&dst
[i
][y
*dst_stride
[i
]], &p
->ref
[1][i
][y
*refs
], w
);
373 #if defined(HAVE_MMX) && defined(NAMED_ASM_ARGS)
374 if(gCpuCaps
.hasMMX2
) asm volatile("emms \n\t" : : : "memory");
378 static int config(struct vf_instance
* vf
,
379 int width
, int height
, int d_width
, int d_height
,
380 unsigned int flags
, unsigned int outfmt
){
385 int w
= ((width
+ 31) & (~31))>>is_chroma
;
386 int h
= ((height
+6+ 31) & (~31))>>is_chroma
;
388 vf
->priv
->stride
[i
]= w
;
390 vf
->priv
->ref
[j
][i
]= malloc(w
*h
*sizeof(uint8_t))+3*w
;
393 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
396 static int continue_buffered_image(struct vf_instance
*vf
);
398 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
){
401 if(vf
->priv
->parity
< 0) {
402 if (mpi
->fields
& MP_IMGFIELD_ORDERED
)
403 tff
= !!(mpi
->fields
& MP_IMGFIELD_TOP_FIRST
);
407 else tff
= (vf
->priv
->parity
&1)^1;
409 store_ref(vf
->priv
, mpi
->planes
, mpi
->stride
, mpi
->w
, mpi
->h
);
411 vf
->priv
->buffered_mpi
= mpi
;
412 vf
->priv
->buffered_tff
= tff
;
413 vf
->priv
->buffered_i
= 0;
414 vf
->priv
->buffered_pts
= pts
;
416 if(vf
->priv
->do_deinterlace
== 0)
417 return vf_next_put_image(vf
, mpi
, pts
);
418 else if(vf
->priv
->do_deinterlace
== 1){
419 vf
->priv
->do_deinterlace
= 2;
422 return continue_buffered_image(vf
);
425 static int continue_buffered_image(struct vf_instance
*vf
)
427 struct MPOpts
*opts
= vf
->opts
;
428 mp_image_t
*mpi
= vf
->priv
->buffered_mpi
;
429 int tff
= vf
->priv
->buffered_tff
;
430 double pts
= vf
->priv
->buffered_pts
;
435 pts
+= vf
->priv
->buffered_i
* .02; // XXX not right
437 for(i
= vf
->priv
->buffered_i
; i
<=(vf
->priv
->mode
&1); i
++){
438 dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
440 MP_IMGFLAG_ACCEPT_STRIDE
|MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
441 mpi
->width
,mpi
->height
);
442 vf_clone_mpi_attributes(dmpi
, mpi
);
443 filter(vf
->priv
, dmpi
->planes
, dmpi
->stride
, mpi
->w
, mpi
->h
, i
^ tff
^ 1, tff
);
444 if (opts
->correct_pts
&& i
< (vf
->priv
->mode
& 1))
445 vf_queue_frame(vf
, continue_buffered_image
);
446 ret
|= vf_next_put_image(vf
, dmpi
, pts
/*FIXME*/);
447 if (opts
->correct_pts
)
449 if(i
<(vf
->priv
->mode
&1))
450 vf_next_control(vf
, VFCTRL_FLIP_PAGE
, NULL
);
452 vf
->priv
->buffered_i
= 1;
456 static void uninit(struct vf_instance
* vf
){
458 if(!vf
->priv
) return;
460 for(i
=0; i
<3*3; i
++){
461 uint8_t **p
= &vf
->priv
->ref
[i
%3][i
/3];
462 if(*p
) free(*p
- 3*vf
->priv
->stride
[i
/3]);
469 //===========================================================================//
470 static int query_format(struct vf_instance
* vf
, unsigned int fmt
){
477 return vf_next_query_format(vf
,fmt
);
482 static int control(struct vf_instance
* vf
, int request
, void* data
){
484 case VFCTRL_GET_DEINTERLACE
:
485 *(int*)data
= vf
->priv
->do_deinterlace
;
487 case VFCTRL_SET_DEINTERLACE
:
488 vf
->priv
->do_deinterlace
= 2*!!*(int*)data
;
491 return vf_next_control (vf
, request
, data
);
494 static int open(vf_instance_t
*vf
, char* args
){
497 vf
->put_image
=put_image
;
498 vf
->query_format
=query_format
;
500 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
502 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
505 vf
->priv
->parity
= -1;
506 vf
->priv
->do_deinterlace
=1;
508 if (args
) sscanf(args
, "%d:%d", &vf
->priv
->mode
, &vf
->priv
->parity
);
510 filter_line
= filter_line_c
;
511 #if defined(HAVE_MMX) && defined(NAMED_ASM_ARGS)
512 if(gCpuCaps
.hasMMX2
) filter_line
= filter_line_mmx2
;
518 const vf_info_t vf_info_yadif
= {
519 "Yet Another DeInterlacing Filter",
521 "Michael Niedermayer",