DOCS/xml: change --with-extraincdir to --extra-cflags
[mplayer/glamo.git] / libmpcodecs / vf_yadif.c
blob25a84ba0e79d0c91e4faed3c11667f1548259ed6
1 /*
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.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <math.h>
27 #include "config.h"
28 #include "cpudetect.h"
29 #include "options.h"
31 #include "mp_msg.h"
32 #include "img_format.h"
33 #include "mp_image.h"
34 #include "vf.h"
35 #include "libvo/fastmemcpy.h"
36 #include "libavutil/common.h"
38 //===========================================================================//
40 struct vf_priv_s {
41 int mode;
42 int parity;
43 int buffered_i;
44 int buffered_tff;
45 double buffered_pts;
46 mp_image_t *buffered_mpi;
47 int stride[3];
48 uint8_t *ref[4][3];
49 int do_deinterlace;
52 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);
54 static void store_ref(struct vf_priv_s *p, uint8_t *src[3], int src_stride[3], int width, int height){
55 int i;
57 memcpy (p->ref[3], p->ref[0], sizeof(uint8_t *)*3);
58 memmove(p->ref[0], p->ref[1], sizeof(uint8_t *)*3*3);
60 for(i=0; i<3; i++){
61 int is_chroma= !!i;
63 memcpy_pic(p->ref[2][i], src[i], width>>is_chroma, height>>is_chroma, p->stride[i], src_stride[i]);
67 #if HAVE_MMX && defined(NAMED_ASM_ARGS)
69 #define LOAD4(mem,dst) \
70 "movd "mem", "#dst" \n\t"\
71 "punpcklbw %%mm7, "#dst" \n\t"
73 #define PABS(tmp,dst) \
74 "pxor "#tmp", "#tmp" \n\t"\
75 "psubw "#dst", "#tmp" \n\t"\
76 "pmaxsw "#tmp", "#dst" \n\t"
78 #define CHECK(pj,mj) \
79 "movq "#pj"(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1+j] */\
80 "movq "#mj"(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1-j] */\
81 "movq %%mm2, %%mm4 \n\t"\
82 "movq %%mm2, %%mm5 \n\t"\
83 "pxor %%mm3, %%mm4 \n\t"\
84 "pavgb %%mm3, %%mm5 \n\t"\
85 "pand %[pb1], %%mm4 \n\t"\
86 "psubusb %%mm4, %%mm5 \n\t"\
87 "psrlq $8, %%mm5 \n\t"\
88 "punpcklbw %%mm7, %%mm5 \n\t" /* (cur[x-refs+j] + cur[x+refs-j])>>1 */\
89 "movq %%mm2, %%mm4 \n\t"\
90 "psubusb %%mm3, %%mm2 \n\t"\
91 "psubusb %%mm4, %%mm3 \n\t"\
92 "pmaxub %%mm3, %%mm2 \n\t"\
93 "movq %%mm2, %%mm3 \n\t"\
94 "movq %%mm2, %%mm4 \n\t" /* ABS(cur[x-refs-1+j] - cur[x+refs-1-j]) */\
95 "psrlq $8, %%mm3 \n\t" /* ABS(cur[x-refs +j] - cur[x+refs -j]) */\
96 "psrlq $16, %%mm4 \n\t" /* ABS(cur[x-refs+1+j] - cur[x+refs+1-j]) */\
97 "punpcklbw %%mm7, %%mm2 \n\t"\
98 "punpcklbw %%mm7, %%mm3 \n\t"\
99 "punpcklbw %%mm7, %%mm4 \n\t"\
100 "paddw %%mm3, %%mm2 \n\t"\
101 "paddw %%mm4, %%mm2 \n\t" /* score */
103 #define CHECK1 \
104 "movq %%mm0, %%mm3 \n\t"\
105 "pcmpgtw %%mm2, %%mm3 \n\t" /* if(score < spatial_score) */\
106 "pminsw %%mm2, %%mm0 \n\t" /* spatial_score= score; */\
107 "movq %%mm3, %%mm6 \n\t"\
108 "pand %%mm3, %%mm5 \n\t"\
109 "pandn %%mm1, %%mm3 \n\t"\
110 "por %%mm5, %%mm3 \n\t"\
111 "movq %%mm3, %%mm1 \n\t" /* spatial_pred= (cur[x-refs+j] + cur[x+refs-j])>>1; */
113 #define CHECK2 /* pretend not to have checked dir=2 if dir=1 was bad.\
114 hurts both quality and speed, but matches the C version. */\
115 "paddw %[pw1], %%mm6 \n\t"\
116 "psllw $14, %%mm6 \n\t"\
117 "paddsw %%mm6, %%mm2 \n\t"\
118 "movq %%mm0, %%mm3 \n\t"\
119 "pcmpgtw %%mm2, %%mm3 \n\t"\
120 "pminsw %%mm2, %%mm0 \n\t"\
121 "pand %%mm3, %%mm5 \n\t"\
122 "pandn %%mm1, %%mm3 \n\t"\
123 "por %%mm5, %%mm3 \n\t"\
124 "movq %%mm3, %%mm1 \n\t"
126 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){
127 static const uint64_t pw_1 = 0x0001000100010001ULL;
128 static const uint64_t pb_1 = 0x0101010101010101ULL;
129 const int mode = p->mode;
130 uint64_t tmp0, tmp1, tmp2, tmp3;
131 int x;
133 #define FILTER\
134 for(x=0; x<w; x+=4){\
135 __asm__ volatile(\
136 "pxor %%mm7, %%mm7 \n\t"\
137 LOAD4("(%[cur],%[mrefs])", %%mm0) /* c = cur[x-refs] */\
138 LOAD4("(%[cur],%[prefs])", %%mm1) /* e = cur[x+refs] */\
139 LOAD4("(%["prev2"])", %%mm2) /* prev2[x] */\
140 LOAD4("(%["next2"])", %%mm3) /* next2[x] */\
141 "movq %%mm3, %%mm4 \n\t"\
142 "paddw %%mm2, %%mm3 \n\t"\
143 "psraw $1, %%mm3 \n\t" /* d = (prev2[x] + next2[x])>>1 */\
144 "movq %%mm0, %[tmp0] \n\t" /* c */\
145 "movq %%mm3, %[tmp1] \n\t" /* d */\
146 "movq %%mm1, %[tmp2] \n\t" /* e */\
147 "psubw %%mm4, %%mm2 \n\t"\
148 PABS( %%mm4, %%mm2) /* temporal_diff0 */\
149 LOAD4("(%[prev],%[mrefs])", %%mm3) /* prev[x-refs] */\
150 LOAD4("(%[prev],%[prefs])", %%mm4) /* prev[x+refs] */\
151 "psubw %%mm0, %%mm3 \n\t"\
152 "psubw %%mm1, %%mm4 \n\t"\
153 PABS( %%mm5, %%mm3)\
154 PABS( %%mm5, %%mm4)\
155 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff1 */\
156 "psrlw $1, %%mm2 \n\t"\
157 "psrlw $1, %%mm3 \n\t"\
158 "pmaxsw %%mm3, %%mm2 \n\t"\
159 LOAD4("(%[next],%[mrefs])", %%mm3) /* next[x-refs] */\
160 LOAD4("(%[next],%[prefs])", %%mm4) /* next[x+refs] */\
161 "psubw %%mm0, %%mm3 \n\t"\
162 "psubw %%mm1, %%mm4 \n\t"\
163 PABS( %%mm5, %%mm3)\
164 PABS( %%mm5, %%mm4)\
165 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff2 */\
166 "psrlw $1, %%mm3 \n\t"\
167 "pmaxsw %%mm3, %%mm2 \n\t"\
168 "movq %%mm2, %[tmp3] \n\t" /* diff */\
170 "paddw %%mm0, %%mm1 \n\t"\
171 "paddw %%mm0, %%mm0 \n\t"\
172 "psubw %%mm1, %%mm0 \n\t"\
173 "psrlw $1, %%mm1 \n\t" /* spatial_pred */\
174 PABS( %%mm2, %%mm0) /* ABS(c-e) */\
176 "movq -1(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1] */\
177 "movq -1(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1] */\
178 "movq %%mm2, %%mm4 \n\t"\
179 "psubusb %%mm3, %%mm2 \n\t"\
180 "psubusb %%mm4, %%mm3 \n\t"\
181 "pmaxub %%mm3, %%mm2 \n\t"\
182 "pshufw $9,%%mm2, %%mm3 \n\t"\
183 "punpcklbw %%mm7, %%mm2 \n\t" /* ABS(cur[x-refs-1] - cur[x+refs-1]) */\
184 "punpcklbw %%mm7, %%mm3 \n\t" /* ABS(cur[x-refs+1] - cur[x+refs+1]) */\
185 "paddw %%mm2, %%mm0 \n\t"\
186 "paddw %%mm3, %%mm0 \n\t"\
187 "psubw %[pw1], %%mm0 \n\t" /* spatial_score */\
189 CHECK(-2,0)\
190 CHECK1\
191 CHECK(-3,1)\
192 CHECK2\
193 CHECK(0,-2)\
194 CHECK1\
195 CHECK(1,-3)\
196 CHECK2\
198 /* if(p->mode<2) ... */\
199 "movq %[tmp3], %%mm6 \n\t" /* diff */\
200 "cmpl $2, %[mode] \n\t"\
201 "jge 1f \n\t"\
202 LOAD4("(%["prev2"],%[mrefs],2)", %%mm2) /* prev2[x-2*refs] */\
203 LOAD4("(%["next2"],%[mrefs],2)", %%mm4) /* next2[x-2*refs] */\
204 LOAD4("(%["prev2"],%[prefs],2)", %%mm3) /* prev2[x+2*refs] */\
205 LOAD4("(%["next2"],%[prefs],2)", %%mm5) /* next2[x+2*refs] */\
206 "paddw %%mm4, %%mm2 \n\t"\
207 "paddw %%mm5, %%mm3 \n\t"\
208 "psrlw $1, %%mm2 \n\t" /* b */\
209 "psrlw $1, %%mm3 \n\t" /* f */\
210 "movq %[tmp0], %%mm4 \n\t" /* c */\
211 "movq %[tmp1], %%mm5 \n\t" /* d */\
212 "movq %[tmp2], %%mm7 \n\t" /* e */\
213 "psubw %%mm4, %%mm2 \n\t" /* b-c */\
214 "psubw %%mm7, %%mm3 \n\t" /* f-e */\
215 "movq %%mm5, %%mm0 \n\t"\
216 "psubw %%mm4, %%mm5 \n\t" /* d-c */\
217 "psubw %%mm7, %%mm0 \n\t" /* d-e */\
218 "movq %%mm2, %%mm4 \n\t"\
219 "pminsw %%mm3, %%mm2 \n\t"\
220 "pmaxsw %%mm4, %%mm3 \n\t"\
221 "pmaxsw %%mm5, %%mm2 \n\t"\
222 "pminsw %%mm5, %%mm3 \n\t"\
223 "pmaxsw %%mm0, %%mm2 \n\t" /* max */\
224 "pminsw %%mm0, %%mm3 \n\t" /* min */\
225 "pxor %%mm4, %%mm4 \n\t"\
226 "pmaxsw %%mm3, %%mm6 \n\t"\
227 "psubw %%mm2, %%mm4 \n\t" /* -max */\
228 "pmaxsw %%mm4, %%mm6 \n\t" /* diff= MAX3(diff, min, -max); */\
229 "1: \n\t"\
231 "movq %[tmp1], %%mm2 \n\t" /* d */\
232 "movq %%mm2, %%mm3 \n\t"\
233 "psubw %%mm6, %%mm2 \n\t" /* d-diff */\
234 "paddw %%mm6, %%mm3 \n\t" /* d+diff */\
235 "pmaxsw %%mm2, %%mm1 \n\t"\
236 "pminsw %%mm3, %%mm1 \n\t" /* d = clip(spatial_pred, d-diff, d+diff); */\
237 "packuswb %%mm1, %%mm1 \n\t"\
239 :[tmp0]"=m"(tmp0),\
240 [tmp1]"=m"(tmp1),\
241 [tmp2]"=m"(tmp2),\
242 [tmp3]"=m"(tmp3)\
243 :[prev] "r"(prev),\
244 [cur] "r"(cur),\
245 [next] "r"(next),\
246 [prefs]"r"((x86_reg)refs),\
247 [mrefs]"r"((x86_reg)-refs),\
248 [pw1] "m"(pw_1),\
249 [pb1] "m"(pb_1),\
250 [mode] "g"(mode)\
252 __asm__ volatile("movd %%mm1, %0" :"=m"(*dst));\
253 dst += 4;\
254 prev+= 4;\
255 cur += 4;\
256 next+= 4;\
259 if(parity){
260 #define prev2 "prev"
261 #define next2 "cur"
262 FILTER
263 #undef prev2
264 #undef next2
265 }else{
266 #define prev2 "cur"
267 #define next2 "next"
268 FILTER
269 #undef prev2
270 #undef next2
273 #undef LOAD4
274 #undef PABS
275 #undef CHECK
276 #undef CHECK1
277 #undef CHECK2
278 #undef FILTER
280 #endif /* HAVE_MMX && defined(NAMED_ASM_ARGS) */
282 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){
283 int x;
284 uint8_t *prev2= parity ? prev : cur ;
285 uint8_t *next2= parity ? cur : next;
286 for(x=0; x<w; x++){
287 int c= cur[-refs];
288 int d= (prev2[0] + next2[0])>>1;
289 int e= cur[+refs];
290 int temporal_diff0= FFABS(prev2[0] - next2[0]);
291 int temporal_diff1=( FFABS(prev[-refs] - c) + FFABS(prev[+refs] - e) )>>1;
292 int temporal_diff2=( FFABS(next[-refs] - c) + FFABS(next[+refs] - e) )>>1;
293 int diff= FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
294 int spatial_pred= (c+e)>>1;
295 int spatial_score= FFABS(cur[-refs-1] - cur[+refs-1]) + FFABS(c-e)
296 + FFABS(cur[-refs+1] - cur[+refs+1]) - 1;
298 #define CHECK(j)\
299 { int score= FFABS(cur[-refs-1+j] - cur[+refs-1-j])\
300 + FFABS(cur[-refs +j] - cur[+refs -j])\
301 + FFABS(cur[-refs+1+j] - cur[+refs+1-j]);\
302 if(score < spatial_score){\
303 spatial_score= score;\
304 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\
306 CHECK(-1) CHECK(-2) }} }}
307 CHECK( 1) CHECK( 2) }} }}
309 if(p->mode<2){
310 int b= (prev2[-2*refs] + next2[-2*refs])>>1;
311 int f= (prev2[+2*refs] + next2[+2*refs])>>1;
312 #if 0
313 int a= cur[-3*refs];
314 int g= cur[+3*refs];
315 int max= FFMAX3(d-e, d-c, FFMIN3(FFMAX(b-c,f-e),FFMAX(b-c,b-a),FFMAX(f-g,f-e)) );
316 int min= FFMIN3(d-e, d-c, FFMAX3(FFMIN(b-c,f-e),FFMIN(b-c,b-a),FFMIN(f-g,f-e)) );
317 #else
318 int max= FFMAX3(d-e, d-c, FFMIN(b-c, f-e));
319 int min= FFMIN3(d-e, d-c, FFMAX(b-c, f-e));
320 #endif
322 diff= FFMAX3(diff, min, -max);
325 if(spatial_pred > d + diff)
326 spatial_pred = d + diff;
327 else if(spatial_pred < d - diff)
328 spatial_pred = d - diff;
330 dst[0] = spatial_pred;
332 dst++;
333 cur++;
334 prev++;
335 next++;
336 prev2++;
337 next2++;
341 static void filter(struct vf_priv_s *p, uint8_t *dst[3], int dst_stride[3], int width, int height, int parity, int tff){
342 int y, i;
344 for(i=0; i<3; i++){
345 int is_chroma= !!i;
346 int w= width >>is_chroma;
347 int h= height>>is_chroma;
348 int refs= p->stride[i];
350 for(y=0; y<h; y++){
351 if((y ^ parity) & 1){
352 uint8_t *prev= &p->ref[0][i][y*refs];
353 uint8_t *cur = &p->ref[1][i][y*refs];
354 uint8_t *next= &p->ref[2][i][y*refs];
355 uint8_t *dst2= &dst[i][y*dst_stride[i]];
356 filter_line(p, dst2, prev, cur, next, w, refs, parity ^ tff);
357 }else{
358 fast_memcpy(&dst[i][y*dst_stride[i]], &p->ref[1][i][y*refs], w);
362 #if HAVE_MMX && defined(NAMED_ASM_ARGS)
363 if(gCpuCaps.hasMMX2) __asm__ volatile("emms \n\t" : : : "memory");
364 #endif
367 static int config(struct vf_instance *vf,
368 int width, int height, int d_width, int d_height,
369 unsigned int flags, unsigned int outfmt){
370 int i, j;
372 for(i=0; i<3; i++){
373 int is_chroma= !!i;
374 int w= ((width + 31) & (~31))>>is_chroma;
375 int h= ((height+6+ 31) & (~31))>>is_chroma;
377 vf->priv->stride[i]= w;
378 for(j=0; j<3; j++)
379 vf->priv->ref[j][i]= malloc(w*h*sizeof(uint8_t))+3*w;
382 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
385 static int continue_buffered_image(struct vf_instance *vf);
387 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
388 int tff;
390 if(vf->priv->parity < 0) {
391 if (mpi->fields & MP_IMGFIELD_ORDERED)
392 tff = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
393 else
394 tff = 1;
396 else tff = (vf->priv->parity&1)^1;
398 store_ref(vf->priv, mpi->planes, mpi->stride, mpi->w, mpi->h);
400 vf->priv->buffered_mpi = mpi;
401 vf->priv->buffered_tff = tff;
402 vf->priv->buffered_i = 0;
403 vf->priv->buffered_pts = pts;
405 if(vf->priv->do_deinterlace == 0)
406 return vf_next_put_image(vf, mpi, pts);
407 else if(vf->priv->do_deinterlace == 1){
408 vf->priv->do_deinterlace= 2;
409 return 0;
410 }else
411 return continue_buffered_image(vf);
414 extern const int under_mencoder;
416 static int continue_buffered_image(struct vf_instance *vf)
418 mp_image_t *mpi = vf->priv->buffered_mpi;
419 int tff = vf->priv->buffered_tff;
420 double pts = vf->priv->buffered_pts;
421 int i;
422 int ret=0;
423 mp_image_t *dmpi;
425 pts += vf->priv->buffered_i * .02; // XXX not right
427 for(i = vf->priv->buffered_i; i<=(vf->priv->mode&1); i++){
428 dmpi=vf_get_image(vf->next,mpi->imgfmt,
429 MP_IMGTYPE_TEMP,
430 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
431 mpi->width,mpi->height);
432 vf_clone_mpi_attributes(dmpi, mpi);
433 filter(vf->priv, dmpi->planes, dmpi->stride, mpi->w, mpi->h, i ^ tff ^ 1, tff);
434 if (i < (vf->priv->mode & 1) && !under_mencoder)
435 vf_queue_frame(vf, continue_buffered_image);
436 ret |= vf_next_put_image(vf, dmpi, pts /*FIXME*/);
437 if (!under_mencoder)
438 break;
440 vf->priv->buffered_i = 1;
441 return ret;
444 static void uninit(struct vf_instance *vf){
445 int i;
446 if(!vf->priv) return;
448 for(i=0; i<3*3; i++){
449 uint8_t **p= &vf->priv->ref[i%3][i/3];
450 if(*p) free(*p - 3*vf->priv->stride[i/3]);
451 *p= NULL;
453 free(vf->priv);
454 vf->priv=NULL;
457 //===========================================================================//
458 static int query_format(struct vf_instance *vf, unsigned int fmt){
459 switch(fmt){
460 case IMGFMT_YV12:
461 case IMGFMT_I420:
462 case IMGFMT_IYUV:
463 case IMGFMT_Y800:
464 case IMGFMT_Y8:
465 return vf_next_query_format(vf,fmt);
467 return 0;
470 static int control(struct vf_instance *vf, int request, void* data){
471 switch (request){
472 case VFCTRL_GET_DEINTERLACE:
473 *(int*)data = vf->priv->do_deinterlace;
474 return CONTROL_OK;
475 case VFCTRL_SET_DEINTERLACE:
476 vf->priv->do_deinterlace = 2*!!*(int*)data;
477 return CONTROL_OK;
479 return vf_next_control (vf, request, data);
482 static int vf_open(vf_instance_t *vf, char *args){
484 vf->config=config;
485 vf->put_image=put_image;
486 vf->query_format=query_format;
487 vf->uninit=uninit;
488 vf->priv=malloc(sizeof(struct vf_priv_s));
489 vf->control=control;
490 memset(vf->priv, 0, sizeof(struct vf_priv_s));
492 vf->priv->mode=0;
493 vf->priv->parity= -1;
494 vf->priv->do_deinterlace=1;
496 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
498 filter_line = filter_line_c;
499 #if HAVE_MMX && defined(NAMED_ASM_ARGS)
500 if(gCpuCaps.hasMMX2) filter_line = filter_line_mmx2;
501 #endif
503 return 1;
506 const vf_info_t vf_info_yadif = {
507 "Yet Another DeInterlacing Filter",
508 "yadif",
509 "Michael Niedermayer",
511 vf_open,
512 NULL