Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_yadif.c
blob880c4cdaeb64bbd974824b0dc0ba7f538a3f7e9f
1 /*
2 Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
4 This program 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 This program 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
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <math.h>
25 #include "config.h"
26 #include "cpudetect.h"
28 #include "mp_msg.h"
30 #ifdef HAVE_MALLOC_H
31 #include <malloc.h>
32 #endif
34 #include "img_format.h"
35 #include "mp_image.h"
36 #include "vf.h"
37 #include "libvo/fastmemcpy.h"
39 #define MIN(a,b) ((a) > (b) ? (b) : (a))
40 #define MAX(a,b) ((a) < (b) ? (b) : (a))
41 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
43 #define MIN3(a,b,c) MIN(MIN(a,b),c)
44 #define MAX3(a,b,c) MAX(MAX(a,b),c)
46 //===========================================================================//
48 struct vf_priv_s {
49 int mode;
50 int parity;
51 int buffered_i;
52 int buffered_tff;
53 double buffered_pts;
54 mp_image_t *buffered_mpi;
55 int stride[3];
56 uint8_t *ref[4][3];
57 int do_deinterlace;
60 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);
62 static void store_ref(struct vf_priv_s *p, uint8_t *src[3], int src_stride[3], int width, int height){
63 int i;
65 memcpy (p->ref[3], p->ref[0], sizeof(uint8_t *)*3);
66 memmove(p->ref[0], p->ref[1], sizeof(uint8_t *)*3*3);
68 for(i=0; i<3; i++){
69 int is_chroma= !!i;
71 memcpy_pic(p->ref[2][i], src[i], width>>is_chroma, height>>is_chroma, p->stride[i], src_stride[i]);
75 #if defined(HAVE_MMX) && defined(NAMED_ASM_ARGS)
77 #define LOAD4(mem,dst) \
78 "movd "mem", "#dst" \n\t"\
79 "punpcklbw %%mm7, "#dst" \n\t"
81 #define PABS(tmp,dst) \
82 "pxor "#tmp", "#tmp" \n\t"\
83 "psubw "#dst", "#tmp" \n\t"\
84 "pmaxsw "#tmp", "#dst" \n\t"
86 #define CHECK(pj,mj) \
87 "movq "#pj"(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1+j] */\
88 "movq "#mj"(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1-j] */\
89 "movq %%mm2, %%mm4 \n\t"\
90 "movq %%mm2, %%mm5 \n\t"\
91 "pxor %%mm3, %%mm4 \n\t"\
92 "pavgb %%mm3, %%mm5 \n\t"\
93 "pand %[pb1], %%mm4 \n\t"\
94 "psubusb %%mm4, %%mm5 \n\t"\
95 "psrlq $8, %%mm5 \n\t"\
96 "punpcklbw %%mm7, %%mm5 \n\t" /* (cur[x-refs+j] + cur[x+refs-j])>>1 */\
97 "movq %%mm2, %%mm4 \n\t"\
98 "psubusb %%mm3, %%mm2 \n\t"\
99 "psubusb %%mm4, %%mm3 \n\t"\
100 "pmaxub %%mm3, %%mm2 \n\t"\
101 "movq %%mm2, %%mm3 \n\t"\
102 "movq %%mm2, %%mm4 \n\t" /* ABS(cur[x-refs-1+j] - cur[x+refs-1-j]) */\
103 "psrlq $8, %%mm3 \n\t" /* ABS(cur[x-refs +j] - cur[x+refs -j]) */\
104 "psrlq $16, %%mm4 \n\t" /* ABS(cur[x-refs+1+j] - cur[x+refs+1-j]) */\
105 "punpcklbw %%mm7, %%mm2 \n\t"\
106 "punpcklbw %%mm7, %%mm3 \n\t"\
107 "punpcklbw %%mm7, %%mm4 \n\t"\
108 "paddw %%mm3, %%mm2 \n\t"\
109 "paddw %%mm4, %%mm2 \n\t" /* score */
111 #define CHECK1 \
112 "movq %%mm0, %%mm3 \n\t"\
113 "pcmpgtw %%mm2, %%mm3 \n\t" /* if(score < spatial_score) */\
114 "pminsw %%mm2, %%mm0 \n\t" /* spatial_score= score; */\
115 "movq %%mm3, %%mm6 \n\t"\
116 "pand %%mm3, %%mm5 \n\t"\
117 "pandn %%mm1, %%mm3 \n\t"\
118 "por %%mm5, %%mm3 \n\t"\
119 "movq %%mm3, %%mm1 \n\t" /* spatial_pred= (cur[x-refs+j] + cur[x+refs-j])>>1; */
121 #define CHECK2 /* pretend not to have checked dir=2 if dir=1 was bad.\
122 hurts both quality and speed, but matches the C version. */\
123 "paddw %[pw1], %%mm6 \n\t"\
124 "psllw $14, %%mm6 \n\t"\
125 "paddsw %%mm6, %%mm2 \n\t"\
126 "movq %%mm0, %%mm3 \n\t"\
127 "pcmpgtw %%mm2, %%mm3 \n\t"\
128 "pminsw %%mm2, %%mm0 \n\t"\
129 "pand %%mm3, %%mm5 \n\t"\
130 "pandn %%mm1, %%mm3 \n\t"\
131 "por %%mm5, %%mm3 \n\t"\
132 "movq %%mm3, %%mm1 \n\t"
134 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){
135 static const uint64_t pw_1 = 0x0001000100010001ULL;
136 static const uint64_t pb_1 = 0x0101010101010101ULL;
137 const int mode = p->mode;
138 uint64_t tmp0, tmp1, tmp2, tmp3;
139 int x;
141 #define FILTER\
142 for(x=0; x<w; x+=4){\
143 asm volatile(\
144 "pxor %%mm7, %%mm7 \n\t"\
145 LOAD4("(%[cur],%[mrefs])", %%mm0) /* c = cur[x-refs] */\
146 LOAD4("(%[cur],%[prefs])", %%mm1) /* e = cur[x+refs] */\
147 LOAD4("(%["prev2"])", %%mm2) /* prev2[x] */\
148 LOAD4("(%["next2"])", %%mm3) /* next2[x] */\
149 "movq %%mm3, %%mm4 \n\t"\
150 "paddw %%mm2, %%mm3 \n\t"\
151 "psraw $1, %%mm3 \n\t" /* d = (prev2[x] + next2[x])>>1 */\
152 "movq %%mm0, %[tmp0] \n\t" /* c */\
153 "movq %%mm3, %[tmp1] \n\t" /* d */\
154 "movq %%mm1, %[tmp2] \n\t" /* e */\
155 "psubw %%mm4, %%mm2 \n\t"\
156 PABS( %%mm4, %%mm2) /* temporal_diff0 */\
157 LOAD4("(%[prev],%[mrefs])", %%mm3) /* prev[x-refs] */\
158 LOAD4("(%[prev],%[prefs])", %%mm4) /* prev[x+refs] */\
159 "psubw %%mm0, %%mm3 \n\t"\
160 "psubw %%mm1, %%mm4 \n\t"\
161 PABS( %%mm5, %%mm3)\
162 PABS( %%mm5, %%mm4)\
163 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff1 */\
164 "psrlw $1, %%mm2 \n\t"\
165 "psrlw $1, %%mm3 \n\t"\
166 "pmaxsw %%mm3, %%mm2 \n\t"\
167 LOAD4("(%[next],%[mrefs])", %%mm3) /* next[x-refs] */\
168 LOAD4("(%[next],%[prefs])", %%mm4) /* next[x+refs] */\
169 "psubw %%mm0, %%mm3 \n\t"\
170 "psubw %%mm1, %%mm4 \n\t"\
171 PABS( %%mm5, %%mm3)\
172 PABS( %%mm5, %%mm4)\
173 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff2 */\
174 "psrlw $1, %%mm3 \n\t"\
175 "pmaxsw %%mm3, %%mm2 \n\t"\
176 "movq %%mm2, %[tmp3] \n\t" /* diff */\
178 "paddw %%mm0, %%mm1 \n\t"\
179 "paddw %%mm0, %%mm0 \n\t"\
180 "psubw %%mm1, %%mm0 \n\t"\
181 "psrlw $1, %%mm1 \n\t" /* spatial_pred */\
182 PABS( %%mm2, %%mm0) /* ABS(c-e) */\
184 "movq -1(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1] */\
185 "movq -1(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1] */\
186 "movq %%mm2, %%mm4 \n\t"\
187 "psubusb %%mm3, %%mm2 \n\t"\
188 "psubusb %%mm4, %%mm3 \n\t"\
189 "pmaxub %%mm3, %%mm2 \n\t"\
190 "pshufw $9,%%mm2, %%mm3 \n\t"\
191 "punpcklbw %%mm7, %%mm2 \n\t" /* ABS(cur[x-refs-1] - cur[x+refs-1]) */\
192 "punpcklbw %%mm7, %%mm3 \n\t" /* ABS(cur[x-refs+1] - cur[x+refs+1]) */\
193 "paddw %%mm2, %%mm0 \n\t"\
194 "paddw %%mm3, %%mm0 \n\t"\
195 "psubw %[pw1], %%mm0 \n\t" /* spatial_score */\
197 CHECK(-2,0)\
198 CHECK1\
199 CHECK(-3,1)\
200 CHECK2\
201 CHECK(0,-2)\
202 CHECK1\
203 CHECK(1,-3)\
204 CHECK2\
206 /* if(p->mode<2) ... */\
207 "movq %[tmp3], %%mm6 \n\t" /* diff */\
208 "cmp $2, %[mode] \n\t"\
209 "jge 1f \n\t"\
210 LOAD4("(%["prev2"],%[mrefs],2)", %%mm2) /* prev2[x-2*refs] */\
211 LOAD4("(%["next2"],%[mrefs],2)", %%mm4) /* next2[x-2*refs] */\
212 LOAD4("(%["prev2"],%[prefs],2)", %%mm3) /* prev2[x+2*refs] */\
213 LOAD4("(%["next2"],%[prefs],2)", %%mm5) /* next2[x+2*refs] */\
214 "paddw %%mm4, %%mm2 \n\t"\
215 "paddw %%mm5, %%mm3 \n\t"\
216 "psrlw $1, %%mm2 \n\t" /* b */\
217 "psrlw $1, %%mm3 \n\t" /* f */\
218 "movq %[tmp0], %%mm4 \n\t" /* c */\
219 "movq %[tmp1], %%mm5 \n\t" /* d */\
220 "movq %[tmp2], %%mm7 \n\t" /* e */\
221 "psubw %%mm4, %%mm2 \n\t" /* b-c */\
222 "psubw %%mm7, %%mm3 \n\t" /* f-e */\
223 "movq %%mm5, %%mm0 \n\t"\
224 "psubw %%mm4, %%mm5 \n\t" /* d-c */\
225 "psubw %%mm7, %%mm0 \n\t" /* d-e */\
226 "movq %%mm2, %%mm4 \n\t"\
227 "pminsw %%mm3, %%mm2 \n\t"\
228 "pmaxsw %%mm4, %%mm3 \n\t"\
229 "pmaxsw %%mm5, %%mm2 \n\t"\
230 "pminsw %%mm5, %%mm3 \n\t"\
231 "pmaxsw %%mm0, %%mm2 \n\t" /* max */\
232 "pminsw %%mm0, %%mm3 \n\t" /* min */\
233 "pxor %%mm4, %%mm4 \n\t"\
234 "pmaxsw %%mm3, %%mm6 \n\t"\
235 "psubw %%mm2, %%mm4 \n\t" /* -max */\
236 "pmaxsw %%mm4, %%mm6 \n\t" /* diff= MAX3(diff, min, -max); */\
237 "1: \n\t"\
239 "movq %[tmp1], %%mm2 \n\t" /* d */\
240 "movq %%mm2, %%mm3 \n\t"\
241 "psubw %%mm6, %%mm2 \n\t" /* d-diff */\
242 "paddw %%mm6, %%mm3 \n\t" /* d+diff */\
243 "pmaxsw %%mm2, %%mm1 \n\t"\
244 "pminsw %%mm3, %%mm1 \n\t" /* d = clip(spatial_pred, d-diff, d+diff); */\
245 "packuswb %%mm1, %%mm1 \n\t"\
247 :[tmp0]"=m"(tmp0),\
248 [tmp1]"=m"(tmp1),\
249 [tmp2]"=m"(tmp2),\
250 [tmp3]"=m"(tmp3)\
251 :[prev] "r"(prev),\
252 [cur] "r"(cur),\
253 [next] "r"(next),\
254 [prefs]"r"((long)refs),\
255 [mrefs]"r"((long)-refs),\
256 [pw1] "m"(pw_1),\
257 [pb1] "m"(pb_1),\
258 [mode] "g"(mode)\
260 asm volatile("movd %%mm1, %0" :"=m"(*dst));\
261 dst += 4;\
262 prev+= 4;\
263 cur += 4;\
264 next+= 4;\
267 if(parity){
268 #define prev2 "prev"
269 #define next2 "cur"
270 FILTER
271 #undef prev2
272 #undef next2
273 }else{
274 #define prev2 "cur"
275 #define next2 "next"
276 FILTER
277 #undef prev2
278 #undef next2
281 #undef LOAD4
282 #undef PABS
283 #undef CHECK
284 #undef CHECK1
285 #undef CHECK2
286 #undef FILTER
288 #endif /* defined(HAVE_MMX) && defined(NAMED_ASM_ARGS) */
290 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){
291 int x;
292 uint8_t *prev2= parity ? prev : cur ;
293 uint8_t *next2= parity ? cur : next;
294 for(x=0; x<w; x++){
295 int c= cur[-refs];
296 int d= (prev2[0] + next2[0])>>1;
297 int e= cur[+refs];
298 int temporal_diff0= ABS(prev2[0] - next2[0]);
299 int temporal_diff1=( ABS(prev[-refs] - c) + ABS(prev[+refs] - e) )>>1;
300 int temporal_diff2=( ABS(next[-refs] - c) + ABS(next[+refs] - e) )>>1;
301 int diff= MAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
302 int spatial_pred= (c+e)>>1;
303 int spatial_score= ABS(cur[-refs-1] - cur[+refs-1]) + ABS(c-e)
304 + ABS(cur[-refs+1] - cur[+refs+1]) - 1;
306 #define CHECK(j)\
307 { int score= ABS(cur[-refs-1+j] - cur[+refs-1-j])\
308 + ABS(cur[-refs +j] - cur[+refs -j])\
309 + ABS(cur[-refs+1+j] - cur[+refs+1-j]);\
310 if(score < spatial_score){\
311 spatial_score= score;\
312 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\
314 CHECK(-1) CHECK(-2) }} }}
315 CHECK( 1) CHECK( 2) }} }}
317 if(p->mode<2){
318 int b= (prev2[-2*refs] + next2[-2*refs])>>1;
319 int f= (prev2[+2*refs] + next2[+2*refs])>>1;
320 #if 0
321 int a= cur[-3*refs];
322 int g= cur[+3*refs];
323 int max= MAX3(d-e, d-c, MIN3(MAX(b-c,f-e),MAX(b-c,b-a),MAX(f-g,f-e)) );
324 int min= MIN3(d-e, d-c, MAX3(MIN(b-c,f-e),MIN(b-c,b-a),MIN(f-g,f-e)) );
325 #else
326 int max= MAX3(d-e, d-c, MIN(b-c, f-e));
327 int min= MIN3(d-e, d-c, MAX(b-c, f-e));
328 #endif
330 diff= MAX3(diff, min, -max);
333 if(spatial_pred > d + diff)
334 spatial_pred = d + diff;
335 else if(spatial_pred < d - diff)
336 spatial_pred = d - diff;
338 dst[0] = spatial_pred;
340 dst++;
341 cur++;
342 prev++;
343 next++;
344 prev2++;
345 next2++;
349 static void filter(struct vf_priv_s *p, uint8_t *dst[3], int dst_stride[3], int width, int height, int parity, int tff){
350 int y, i;
352 for(i=0; i<3; i++){
353 int is_chroma= !!i;
354 int w= width >>is_chroma;
355 int h= height>>is_chroma;
356 int refs= p->stride[i];
358 for(y=0; y<h; y++){
359 if((y ^ parity) & 1){
360 uint8_t *prev= &p->ref[0][i][y*refs];
361 uint8_t *cur = &p->ref[1][i][y*refs];
362 uint8_t *next= &p->ref[2][i][y*refs];
363 uint8_t *dst2= &dst[i][y*dst_stride[i]];
364 filter_line(p, dst2, prev, cur, next, w, refs, parity ^ tff);
365 }else{
366 fast_memcpy(&dst[i][y*dst_stride[i]], &p->ref[1][i][y*refs], w);
370 #if defined(HAVE_MMX) && defined(NAMED_ASM_ARGS)
371 if(gCpuCaps.hasMMX2) asm volatile("emms \n\t" : : : "memory");
372 #endif
375 static int config(struct vf_instance_s* vf,
376 int width, int height, int d_width, int d_height,
377 unsigned int flags, unsigned int outfmt){
378 int i, j;
380 for(i=0; i<3; i++){
381 int is_chroma= !!i;
382 int w= ((width + 31) & (~31))>>is_chroma;
383 int h= ((height+6+ 31) & (~31))>>is_chroma;
385 vf->priv->stride[i]= w;
386 for(j=0; j<3; j++)
387 vf->priv->ref[j][i]= malloc(w*h*sizeof(uint8_t))+3*w;
390 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
393 static int continue_buffered_image(struct vf_instance_s *vf);
394 extern int correct_pts;
396 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
397 int tff;
399 if(vf->priv->parity < 0) {
400 if (mpi->fields & MP_IMGFIELD_ORDERED)
401 tff = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
402 else
403 tff = 1;
405 else tff = (vf->priv->parity&1)^1;
407 store_ref(vf->priv, mpi->planes, mpi->stride, mpi->w, mpi->h);
409 vf->priv->buffered_mpi = mpi;
410 vf->priv->buffered_tff = tff;
411 vf->priv->buffered_i = 0;
412 vf->priv->buffered_pts = pts;
414 if(vf->priv->do_deinterlace == 0)
415 return vf_next_put_image(vf, mpi, pts);
416 else if(vf->priv->do_deinterlace == 1){
417 vf->priv->do_deinterlace= 2;
418 return 0;
419 }else
420 return continue_buffered_image(vf);
423 static int continue_buffered_image(struct vf_instance_s *vf)
425 mp_image_t *mpi = vf->priv->buffered_mpi;
426 int tff = vf->priv->buffered_tff;
427 double pts = vf->priv->buffered_pts;
428 int i;
429 int ret=0;
430 mp_image_t *dmpi;
432 pts += vf->priv->buffered_i * .02; // XXX not right
434 for(i = vf->priv->buffered_i; i<=(vf->priv->mode&1); i++){
435 dmpi=vf_get_image(vf->next,mpi->imgfmt,
436 MP_IMGTYPE_TEMP,
437 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
438 mpi->width,mpi->height);
439 vf_clone_mpi_attributes(dmpi, mpi);
440 filter(vf->priv, dmpi->planes, dmpi->stride, mpi->w, mpi->h, i ^ tff ^ 1, tff);
441 if (correct_pts && i < (vf->priv->mode & 1))
442 vf_queue_frame(vf, continue_buffered_image);
443 ret |= vf_next_put_image(vf, dmpi, pts /*FIXME*/);
444 if (correct_pts)
445 break;
446 if(i<(vf->priv->mode&1))
447 vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
449 vf->priv->buffered_i = 1;
450 return ret;
453 static void uninit(struct vf_instance_s* vf){
454 int i;
455 if(!vf->priv) return;
457 for(i=0; i<3*3; i++){
458 uint8_t **p= &vf->priv->ref[i%3][i/3];
459 if(*p) free(*p - 3*vf->priv->stride[i/3]);
460 *p= NULL;
462 free(vf->priv);
463 vf->priv=NULL;
466 //===========================================================================//
467 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
468 switch(fmt){
469 case IMGFMT_YV12:
470 case IMGFMT_I420:
471 case IMGFMT_IYUV:
472 case IMGFMT_Y800:
473 case IMGFMT_Y8:
474 return vf_next_query_format(vf,fmt);
476 return 0;
479 static int control(struct vf_instance_s* vf, int request, void* data){
480 switch (request){
481 case VFCTRL_GET_DEINTERLACE:
482 *(int*)data = vf->priv->do_deinterlace;
483 return CONTROL_OK;
484 case VFCTRL_SET_DEINTERLACE:
485 vf->priv->do_deinterlace = 2*!!*(int*)data;
486 return CONTROL_OK;
488 return vf_next_control (vf, request, data);
491 static int open(vf_instance_t *vf, char* args){
493 vf->config=config;
494 vf->put_image=put_image;
495 vf->query_format=query_format;
496 vf->uninit=uninit;
497 vf->priv=malloc(sizeof(struct vf_priv_s));
498 vf->control=control;
499 memset(vf->priv, 0, sizeof(struct vf_priv_s));
501 vf->priv->mode=0;
502 vf->priv->parity= -1;
503 vf->priv->do_deinterlace=1;
505 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
507 filter_line = filter_line_c;
508 #if defined(HAVE_MMX) && defined(NAMED_ASM_ARGS)
509 if(gCpuCaps.hasMMX2) filter_line = filter_line_mmx2;
510 #endif
512 return 1;
515 const vf_info_t vf_info_yadif = {
516 "Yet Another DeInterlacing Filter",
517 "yadif",
518 "Michael Niedermayer",
520 open,
521 NULL