codec-cfg.c: Remove bogus printf argument
[mplayer.git] / libmpcodecs / vf_yadif.c
blob828ca29dd75e6edd1a8665bf4d876082ebbf8355
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"
33 #if HAVE_MALLOC_H
34 #include <malloc.h>
35 #endif
37 #include "img_format.h"
38 #include "mp_image.h"
39 #include "vf.h"
40 #include "libvo/fastmemcpy.h"
41 #include "libavutil/common.h"
43 //===========================================================================//
45 struct vf_priv_s {
46 int mode;
47 int parity;
48 int buffered_i;
49 int buffered_tff;
50 double buffered_pts;
51 mp_image_t *buffered_mpi;
52 int stride[3];
53 uint8_t *ref[4][3];
54 int do_deinterlace;
57 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);
59 static void store_ref(struct vf_priv_s *p, uint8_t *src[3], int src_stride[3], int width, int height){
60 int i;
62 memcpy (p->ref[3], p->ref[0], sizeof(uint8_t *)*3);
63 memmove(p->ref[0], p->ref[1], sizeof(uint8_t *)*3*3);
65 for(i=0; i<3; i++){
66 int is_chroma= !!i;
68 memcpy_pic(p->ref[2][i], src[i], width>>is_chroma, height>>is_chroma, p->stride[i], src_stride[i]);
72 #if HAVE_MMX && defined(NAMED_ASM_ARGS)
74 #define LOAD4(mem,dst) \
75 "movd "mem", "#dst" \n\t"\
76 "punpcklbw %%mm7, "#dst" \n\t"
78 #define PABS(tmp,dst) \
79 "pxor "#tmp", "#tmp" \n\t"\
80 "psubw "#dst", "#tmp" \n\t"\
81 "pmaxsw "#tmp", "#dst" \n\t"
83 #define CHECK(pj,mj) \
84 "movq "#pj"(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1+j] */\
85 "movq "#mj"(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1-j] */\
86 "movq %%mm2, %%mm4 \n\t"\
87 "movq %%mm2, %%mm5 \n\t"\
88 "pxor %%mm3, %%mm4 \n\t"\
89 "pavgb %%mm3, %%mm5 \n\t"\
90 "pand %[pb1], %%mm4 \n\t"\
91 "psubusb %%mm4, %%mm5 \n\t"\
92 "psrlq $8, %%mm5 \n\t"\
93 "punpcklbw %%mm7, %%mm5 \n\t" /* (cur[x-refs+j] + cur[x+refs-j])>>1 */\
94 "movq %%mm2, %%mm4 \n\t"\
95 "psubusb %%mm3, %%mm2 \n\t"\
96 "psubusb %%mm4, %%mm3 \n\t"\
97 "pmaxub %%mm3, %%mm2 \n\t"\
98 "movq %%mm2, %%mm3 \n\t"\
99 "movq %%mm2, %%mm4 \n\t" /* ABS(cur[x-refs-1+j] - cur[x+refs-1-j]) */\
100 "psrlq $8, %%mm3 \n\t" /* ABS(cur[x-refs +j] - cur[x+refs -j]) */\
101 "psrlq $16, %%mm4 \n\t" /* ABS(cur[x-refs+1+j] - cur[x+refs+1-j]) */\
102 "punpcklbw %%mm7, %%mm2 \n\t"\
103 "punpcklbw %%mm7, %%mm3 \n\t"\
104 "punpcklbw %%mm7, %%mm4 \n\t"\
105 "paddw %%mm3, %%mm2 \n\t"\
106 "paddw %%mm4, %%mm2 \n\t" /* score */
108 #define CHECK1 \
109 "movq %%mm0, %%mm3 \n\t"\
110 "pcmpgtw %%mm2, %%mm3 \n\t" /* if(score < spatial_score) */\
111 "pminsw %%mm2, %%mm0 \n\t" /* spatial_score= score; */\
112 "movq %%mm3, %%mm6 \n\t"\
113 "pand %%mm3, %%mm5 \n\t"\
114 "pandn %%mm1, %%mm3 \n\t"\
115 "por %%mm5, %%mm3 \n\t"\
116 "movq %%mm3, %%mm1 \n\t" /* spatial_pred= (cur[x-refs+j] + cur[x+refs-j])>>1; */
118 #define CHECK2 /* pretend not to have checked dir=2 if dir=1 was bad.\
119 hurts both quality and speed, but matches the C version. */\
120 "paddw %[pw1], %%mm6 \n\t"\
121 "psllw $14, %%mm6 \n\t"\
122 "paddsw %%mm6, %%mm2 \n\t"\
123 "movq %%mm0, %%mm3 \n\t"\
124 "pcmpgtw %%mm2, %%mm3 \n\t"\
125 "pminsw %%mm2, %%mm0 \n\t"\
126 "pand %%mm3, %%mm5 \n\t"\
127 "pandn %%mm1, %%mm3 \n\t"\
128 "por %%mm5, %%mm3 \n\t"\
129 "movq %%mm3, %%mm1 \n\t"
131 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){
132 static const uint64_t pw_1 = 0x0001000100010001ULL;
133 static const uint64_t pb_1 = 0x0101010101010101ULL;
134 const int mode = p->mode;
135 uint64_t tmp0, tmp1, tmp2, tmp3;
136 int x;
138 #define FILTER\
139 for(x=0; x<w; x+=4){\
140 __asm__ volatile(\
141 "pxor %%mm7, %%mm7 \n\t"\
142 LOAD4("(%[cur],%[mrefs])", %%mm0) /* c = cur[x-refs] */\
143 LOAD4("(%[cur],%[prefs])", %%mm1) /* e = cur[x+refs] */\
144 LOAD4("(%["prev2"])", %%mm2) /* prev2[x] */\
145 LOAD4("(%["next2"])", %%mm3) /* next2[x] */\
146 "movq %%mm3, %%mm4 \n\t"\
147 "paddw %%mm2, %%mm3 \n\t"\
148 "psraw $1, %%mm3 \n\t" /* d = (prev2[x] + next2[x])>>1 */\
149 "movq %%mm0, %[tmp0] \n\t" /* c */\
150 "movq %%mm3, %[tmp1] \n\t" /* d */\
151 "movq %%mm1, %[tmp2] \n\t" /* e */\
152 "psubw %%mm4, %%mm2 \n\t"\
153 PABS( %%mm4, %%mm2) /* temporal_diff0 */\
154 LOAD4("(%[prev],%[mrefs])", %%mm3) /* prev[x-refs] */\
155 LOAD4("(%[prev],%[prefs])", %%mm4) /* prev[x+refs] */\
156 "psubw %%mm0, %%mm3 \n\t"\
157 "psubw %%mm1, %%mm4 \n\t"\
158 PABS( %%mm5, %%mm3)\
159 PABS( %%mm5, %%mm4)\
160 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff1 */\
161 "psrlw $1, %%mm2 \n\t"\
162 "psrlw $1, %%mm3 \n\t"\
163 "pmaxsw %%mm3, %%mm2 \n\t"\
164 LOAD4("(%[next],%[mrefs])", %%mm3) /* next[x-refs] */\
165 LOAD4("(%[next],%[prefs])", %%mm4) /* next[x+refs] */\
166 "psubw %%mm0, %%mm3 \n\t"\
167 "psubw %%mm1, %%mm4 \n\t"\
168 PABS( %%mm5, %%mm3)\
169 PABS( %%mm5, %%mm4)\
170 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff2 */\
171 "psrlw $1, %%mm3 \n\t"\
172 "pmaxsw %%mm3, %%mm2 \n\t"\
173 "movq %%mm2, %[tmp3] \n\t" /* diff */\
175 "paddw %%mm0, %%mm1 \n\t"\
176 "paddw %%mm0, %%mm0 \n\t"\
177 "psubw %%mm1, %%mm0 \n\t"\
178 "psrlw $1, %%mm1 \n\t" /* spatial_pred */\
179 PABS( %%mm2, %%mm0) /* ABS(c-e) */\
181 "movq -1(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1] */\
182 "movq -1(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1] */\
183 "movq %%mm2, %%mm4 \n\t"\
184 "psubusb %%mm3, %%mm2 \n\t"\
185 "psubusb %%mm4, %%mm3 \n\t"\
186 "pmaxub %%mm3, %%mm2 \n\t"\
187 "pshufw $9,%%mm2, %%mm3 \n\t"\
188 "punpcklbw %%mm7, %%mm2 \n\t" /* ABS(cur[x-refs-1] - cur[x+refs-1]) */\
189 "punpcklbw %%mm7, %%mm3 \n\t" /* ABS(cur[x-refs+1] - cur[x+refs+1]) */\
190 "paddw %%mm2, %%mm0 \n\t"\
191 "paddw %%mm3, %%mm0 \n\t"\
192 "psubw %[pw1], %%mm0 \n\t" /* spatial_score */\
194 CHECK(-2,0)\
195 CHECK1\
196 CHECK(-3,1)\
197 CHECK2\
198 CHECK(0,-2)\
199 CHECK1\
200 CHECK(1,-3)\
201 CHECK2\
203 /* if(p->mode<2) ... */\
204 "movq %[tmp3], %%mm6 \n\t" /* diff */\
205 "cmp $2, %[mode] \n\t"\
206 "jge 1f \n\t"\
207 LOAD4("(%["prev2"],%[mrefs],2)", %%mm2) /* prev2[x-2*refs] */\
208 LOAD4("(%["next2"],%[mrefs],2)", %%mm4) /* next2[x-2*refs] */\
209 LOAD4("(%["prev2"],%[prefs],2)", %%mm3) /* prev2[x+2*refs] */\
210 LOAD4("(%["next2"],%[prefs],2)", %%mm5) /* next2[x+2*refs] */\
211 "paddw %%mm4, %%mm2 \n\t"\
212 "paddw %%mm5, %%mm3 \n\t"\
213 "psrlw $1, %%mm2 \n\t" /* b */\
214 "psrlw $1, %%mm3 \n\t" /* f */\
215 "movq %[tmp0], %%mm4 \n\t" /* c */\
216 "movq %[tmp1], %%mm5 \n\t" /* d */\
217 "movq %[tmp2], %%mm7 \n\t" /* e */\
218 "psubw %%mm4, %%mm2 \n\t" /* b-c */\
219 "psubw %%mm7, %%mm3 \n\t" /* f-e */\
220 "movq %%mm5, %%mm0 \n\t"\
221 "psubw %%mm4, %%mm5 \n\t" /* d-c */\
222 "psubw %%mm7, %%mm0 \n\t" /* d-e */\
223 "movq %%mm2, %%mm4 \n\t"\
224 "pminsw %%mm3, %%mm2 \n\t"\
225 "pmaxsw %%mm4, %%mm3 \n\t"\
226 "pmaxsw %%mm5, %%mm2 \n\t"\
227 "pminsw %%mm5, %%mm3 \n\t"\
228 "pmaxsw %%mm0, %%mm2 \n\t" /* max */\
229 "pminsw %%mm0, %%mm3 \n\t" /* min */\
230 "pxor %%mm4, %%mm4 \n\t"\
231 "pmaxsw %%mm3, %%mm6 \n\t"\
232 "psubw %%mm2, %%mm4 \n\t" /* -max */\
233 "pmaxsw %%mm4, %%mm6 \n\t" /* diff= MAX3(diff, min, -max); */\
234 "1: \n\t"\
236 "movq %[tmp1], %%mm2 \n\t" /* d */\
237 "movq %%mm2, %%mm3 \n\t"\
238 "psubw %%mm6, %%mm2 \n\t" /* d-diff */\
239 "paddw %%mm6, %%mm3 \n\t" /* d+diff */\
240 "pmaxsw %%mm2, %%mm1 \n\t"\
241 "pminsw %%mm3, %%mm1 \n\t" /* d = clip(spatial_pred, d-diff, d+diff); */\
242 "packuswb %%mm1, %%mm1 \n\t"\
244 :[tmp0]"=m"(tmp0),\
245 [tmp1]"=m"(tmp1),\
246 [tmp2]"=m"(tmp2),\
247 [tmp3]"=m"(tmp3)\
248 :[prev] "r"(prev),\
249 [cur] "r"(cur),\
250 [next] "r"(next),\
251 [prefs]"r"((long)refs),\
252 [mrefs]"r"((long)-refs),\
253 [pw1] "m"(pw_1),\
254 [pb1] "m"(pb_1),\
255 [mode] "g"(mode)\
257 __asm__ volatile("movd %%mm1, %0" :"=m"(*dst));\
258 dst += 4;\
259 prev+= 4;\
260 cur += 4;\
261 next+= 4;\
264 if(parity){
265 #define prev2 "prev"
266 #define next2 "cur"
267 FILTER
268 #undef prev2
269 #undef next2
270 }else{
271 #define prev2 "cur"
272 #define next2 "next"
273 FILTER
274 #undef prev2
275 #undef next2
278 #undef LOAD4
279 #undef PABS
280 #undef CHECK
281 #undef CHECK1
282 #undef CHECK2
283 #undef FILTER
285 #endif /* HAVE_MMX && defined(NAMED_ASM_ARGS) */
287 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){
288 int x;
289 uint8_t *prev2= parity ? prev : cur ;
290 uint8_t *next2= parity ? cur : next;
291 for(x=0; x<w; x++){
292 int c= cur[-refs];
293 int d= (prev2[0] + next2[0])>>1;
294 int e= cur[+refs];
295 int temporal_diff0= FFABS(prev2[0] - next2[0]);
296 int temporal_diff1=( FFABS(prev[-refs] - c) + FFABS(prev[+refs] - e) )>>1;
297 int temporal_diff2=( FFABS(next[-refs] - c) + FFABS(next[+refs] - e) )>>1;
298 int diff= FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
299 int spatial_pred= (c+e)>>1;
300 int spatial_score= FFABS(cur[-refs-1] - cur[+refs-1]) + FFABS(c-e)
301 + FFABS(cur[-refs+1] - cur[+refs+1]) - 1;
303 #define CHECK(j)\
304 { int score= FFABS(cur[-refs-1+j] - cur[+refs-1-j])\
305 + FFABS(cur[-refs +j] - cur[+refs -j])\
306 + FFABS(cur[-refs+1+j] - cur[+refs+1-j]);\
307 if(score < spatial_score){\
308 spatial_score= score;\
309 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\
311 CHECK(-1) CHECK(-2) }} }}
312 CHECK( 1) CHECK( 2) }} }}
314 if(p->mode<2){
315 int b= (prev2[-2*refs] + next2[-2*refs])>>1;
316 int f= (prev2[+2*refs] + next2[+2*refs])>>1;
317 #if 0
318 int a= cur[-3*refs];
319 int g= cur[+3*refs];
320 int max= FFMAX3(d-e, d-c, FFMIN3(FFMAX(b-c,f-e),FFMAX(b-c,b-a),FFMAX(f-g,f-e)) );
321 int min= FFMIN3(d-e, d-c, FFMAX3(FFMIN(b-c,f-e),FFMIN(b-c,b-a),FFMIN(f-g,f-e)) );
322 #else
323 int max= FFMAX3(d-e, d-c, FFMIN(b-c, f-e));
324 int min= FFMIN3(d-e, d-c, FFMAX(b-c, f-e));
325 #endif
327 diff= FFMAX3(diff, min, -max);
330 if(spatial_pred > d + diff)
331 spatial_pred = d + diff;
332 else if(spatial_pred < d - diff)
333 spatial_pred = d - diff;
335 dst[0] = spatial_pred;
337 dst++;
338 cur++;
339 prev++;
340 next++;
341 prev2++;
342 next2++;
346 static void filter(struct vf_priv_s *p, uint8_t *dst[3], int dst_stride[3], int width, int height, int parity, int tff){
347 int y, i;
349 for(i=0; i<3; i++){
350 int is_chroma= !!i;
351 int w= width >>is_chroma;
352 int h= height>>is_chroma;
353 int refs= p->stride[i];
355 for(y=0; y<h; y++){
356 if((y ^ parity) & 1){
357 uint8_t *prev= &p->ref[0][i][y*refs];
358 uint8_t *cur = &p->ref[1][i][y*refs];
359 uint8_t *next= &p->ref[2][i][y*refs];
360 uint8_t *dst2= &dst[i][y*dst_stride[i]];
361 filter_line(p, dst2, prev, cur, next, w, refs, parity ^ tff);
362 }else{
363 fast_memcpy(&dst[i][y*dst_stride[i]], &p->ref[1][i][y*refs], w);
367 #if HAVE_MMX && defined(NAMED_ASM_ARGS)
368 if(gCpuCaps.hasMMX2) __asm__ volatile("emms \n\t" : : : "memory");
369 #endif
372 static int config(struct vf_instance* vf,
373 int width, int height, int d_width, int d_height,
374 unsigned int flags, unsigned int outfmt){
375 int i, j;
377 for(i=0; i<3; i++){
378 int is_chroma= !!i;
379 int w= ((width + 31) & (~31))>>is_chroma;
380 int h= ((height+6+ 31) & (~31))>>is_chroma;
382 vf->priv->stride[i]= w;
383 for(j=0; j<3; j++)
384 vf->priv->ref[j][i]= malloc(w*h*sizeof(uint8_t))+3*w;
387 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
390 static int continue_buffered_image(struct vf_instance *vf);
392 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts){
393 int tff;
395 if(vf->priv->parity < 0) {
396 if (mpi->fields & MP_IMGFIELD_ORDERED)
397 tff = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
398 else
399 tff = 1;
401 else tff = (vf->priv->parity&1)^1;
403 store_ref(vf->priv, mpi->planes, mpi->stride, mpi->w, mpi->h);
405 vf->priv->buffered_mpi = mpi;
406 vf->priv->buffered_tff = tff;
407 vf->priv->buffered_i = 0;
408 vf->priv->buffered_pts = pts;
410 if(vf->priv->do_deinterlace == 0)
411 return vf_next_put_image(vf, mpi, pts);
412 else if(vf->priv->do_deinterlace == 1){
413 vf->priv->do_deinterlace= 2;
414 return 0;
415 }else
416 return continue_buffered_image(vf);
419 extern const int under_mencoder;
421 static int continue_buffered_image(struct vf_instance *vf)
423 mp_image_t *mpi = vf->priv->buffered_mpi;
424 int tff = vf->priv->buffered_tff;
425 double pts = vf->priv->buffered_pts;
426 int i;
427 int ret=0;
428 mp_image_t *dmpi;
430 pts += vf->priv->buffered_i * .02; // XXX not right
432 for(i = vf->priv->buffered_i; i<=(vf->priv->mode&1); i++){
433 dmpi=vf_get_image(vf->next,mpi->imgfmt,
434 MP_IMGTYPE_TEMP,
435 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
436 mpi->width,mpi->height);
437 vf_clone_mpi_attributes(dmpi, mpi);
438 filter(vf->priv, dmpi->planes, dmpi->stride, mpi->w, mpi->h, i ^ tff ^ 1, tff);
439 if (i < (vf->priv->mode & 1) && !under_mencoder)
440 vf_queue_frame(vf, continue_buffered_image);
441 ret |= vf_next_put_image(vf, dmpi, pts /*FIXME*/);
442 if (!under_mencoder)
443 break;
444 if(i<(vf->priv->mode&1))
445 vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
447 vf->priv->buffered_i = 1;
448 return ret;
451 static void uninit(struct vf_instance* vf){
452 int i;
453 if(!vf->priv) return;
455 for(i=0; i<3*3; i++){
456 uint8_t **p= &vf->priv->ref[i%3][i/3];
457 if(*p) free(*p - 3*vf->priv->stride[i/3]);
458 *p= NULL;
460 free(vf->priv);
461 vf->priv=NULL;
464 //===========================================================================//
465 static int query_format(struct vf_instance* vf, unsigned int fmt){
466 switch(fmt){
467 case IMGFMT_YV12:
468 case IMGFMT_I420:
469 case IMGFMT_IYUV:
470 case IMGFMT_Y800:
471 case IMGFMT_Y8:
472 return vf_next_query_format(vf,fmt);
474 return 0;
477 static int control(struct vf_instance* vf, int request, void* data){
478 switch (request){
479 case VFCTRL_GET_DEINTERLACE:
480 *(int*)data = vf->priv->do_deinterlace;
481 return CONTROL_OK;
482 case VFCTRL_SET_DEINTERLACE:
483 vf->priv->do_deinterlace = 2*!!*(int*)data;
484 return CONTROL_OK;
486 return vf_next_control (vf, request, data);
489 static int open(vf_instance_t *vf, char* args){
491 vf->config=config;
492 vf->put_image=put_image;
493 vf->query_format=query_format;
494 vf->uninit=uninit;
495 vf->priv=malloc(sizeof(struct vf_priv_s));
496 vf->control=control;
497 memset(vf->priv, 0, sizeof(struct vf_priv_s));
499 vf->priv->mode=0;
500 vf->priv->parity= -1;
501 vf->priv->do_deinterlace=1;
503 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
505 filter_line = filter_line_c;
506 #if HAVE_MMX && defined(NAMED_ASM_ARGS)
507 if(gCpuCaps.hasMMX2) filter_line = filter_line_mmx2;
508 #endif
510 return 1;
513 const vf_info_t vf_info_yadif = {
514 "Yet Another DeInterlacing Filter",
515 "yadif",
516 "Michael Niedermayer",
518 open,
519 NULL