input: fix possible crash in printing key combo names
[mplayer.git] / libmpcodecs / vf_yadif.c
blob6b9d05fde501dbd5428a70161ebc76abe0339fff
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 double buffered_pts_delta;
47 mp_image_t *buffered_mpi;
48 int stride[3];
49 uint8_t *ref[4][3];
50 int do_deinterlace;
53 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);
55 static void store_ref(struct vf_priv_s *p, uint8_t *src[3], int src_stride[3], int width, int height){
56 int i;
58 memcpy (p->ref[3], p->ref[0], sizeof(uint8_t *)*3);
59 memmove(p->ref[0], p->ref[1], sizeof(uint8_t *)*3*3);
61 for(i=0; i<3; i++){
62 int is_chroma= !!i;
64 memcpy_pic(p->ref[2][i], src[i], width>>is_chroma, height>>is_chroma, p->stride[i], src_stride[i]);
68 #if HAVE_MMX
70 #define LOAD4(mem,dst) \
71 "movd "mem", "#dst" \n\t"\
72 "punpcklbw %%mm7, "#dst" \n\t"
74 #define PABS(tmp,dst) \
75 "pxor "#tmp", "#tmp" \n\t"\
76 "psubw "#dst", "#tmp" \n\t"\
77 "pmaxsw "#tmp", "#dst" \n\t"
79 #define CHECK(pj,mj) \
80 "movq "#pj"(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1+j] */\
81 "movq "#mj"(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1-j] */\
82 "movq %%mm2, %%mm4 \n\t"\
83 "movq %%mm2, %%mm5 \n\t"\
84 "pxor %%mm3, %%mm4 \n\t"\
85 "pavgb %%mm3, %%mm5 \n\t"\
86 "pand %[pb1], %%mm4 \n\t"\
87 "psubusb %%mm4, %%mm5 \n\t"\
88 "psrlq $8, %%mm5 \n\t"\
89 "punpcklbw %%mm7, %%mm5 \n\t" /* (cur[x-refs+j] + cur[x+refs-j])>>1 */\
90 "movq %%mm2, %%mm4 \n\t"\
91 "psubusb %%mm3, %%mm2 \n\t"\
92 "psubusb %%mm4, %%mm3 \n\t"\
93 "pmaxub %%mm3, %%mm2 \n\t"\
94 "movq %%mm2, %%mm3 \n\t"\
95 "movq %%mm2, %%mm4 \n\t" /* ABS(cur[x-refs-1+j] - cur[x+refs-1-j]) */\
96 "psrlq $8, %%mm3 \n\t" /* ABS(cur[x-refs +j] - cur[x+refs -j]) */\
97 "psrlq $16, %%mm4 \n\t" /* ABS(cur[x-refs+1+j] - cur[x+refs+1-j]) */\
98 "punpcklbw %%mm7, %%mm2 \n\t"\
99 "punpcklbw %%mm7, %%mm3 \n\t"\
100 "punpcklbw %%mm7, %%mm4 \n\t"\
101 "paddw %%mm3, %%mm2 \n\t"\
102 "paddw %%mm4, %%mm2 \n\t" /* score */
104 #define CHECK1 \
105 "movq %%mm0, %%mm3 \n\t"\
106 "pcmpgtw %%mm2, %%mm3 \n\t" /* if(score < spatial_score) */\
107 "pminsw %%mm2, %%mm0 \n\t" /* spatial_score= score; */\
108 "movq %%mm3, %%mm6 \n\t"\
109 "pand %%mm3, %%mm5 \n\t"\
110 "pandn %%mm1, %%mm3 \n\t"\
111 "por %%mm5, %%mm3 \n\t"\
112 "movq %%mm3, %%mm1 \n\t" /* spatial_pred= (cur[x-refs+j] + cur[x+refs-j])>>1; */
114 #define CHECK2 /* pretend not to have checked dir=2 if dir=1 was bad.\
115 hurts both quality and speed, but matches the C version. */\
116 "paddw %[pw1], %%mm6 \n\t"\
117 "psllw $14, %%mm6 \n\t"\
118 "paddsw %%mm6, %%mm2 \n\t"\
119 "movq %%mm0, %%mm3 \n\t"\
120 "pcmpgtw %%mm2, %%mm3 \n\t"\
121 "pminsw %%mm2, %%mm0 \n\t"\
122 "pand %%mm3, %%mm5 \n\t"\
123 "pandn %%mm1, %%mm3 \n\t"\
124 "por %%mm5, %%mm3 \n\t"\
125 "movq %%mm3, %%mm1 \n\t"
127 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){
128 static const uint64_t pw_1 = 0x0001000100010001ULL;
129 static const uint64_t pb_1 = 0x0101010101010101ULL;
130 const int mode = p->mode;
131 uint64_t tmp0, tmp1, tmp2, tmp3;
132 int x;
134 #define FILTER\
135 for(x=0; x<w; x+=4){\
136 __asm__ volatile(\
137 "pxor %%mm7, %%mm7 \n\t"\
138 LOAD4("(%[cur],%[mrefs])", %%mm0) /* c = cur[x-refs] */\
139 LOAD4("(%[cur],%[prefs])", %%mm1) /* e = cur[x+refs] */\
140 LOAD4("(%["prev2"])", %%mm2) /* prev2[x] */\
141 LOAD4("(%["next2"])", %%mm3) /* next2[x] */\
142 "movq %%mm3, %%mm4 \n\t"\
143 "paddw %%mm2, %%mm3 \n\t"\
144 "psraw $1, %%mm3 \n\t" /* d = (prev2[x] + next2[x])>>1 */\
145 "movq %%mm0, %[tmp0] \n\t" /* c */\
146 "movq %%mm3, %[tmp1] \n\t" /* d */\
147 "movq %%mm1, %[tmp2] \n\t" /* e */\
148 "psubw %%mm4, %%mm2 \n\t"\
149 PABS( %%mm4, %%mm2) /* temporal_diff0 */\
150 LOAD4("(%[prev],%[mrefs])", %%mm3) /* prev[x-refs] */\
151 LOAD4("(%[prev],%[prefs])", %%mm4) /* prev[x+refs] */\
152 "psubw %%mm0, %%mm3 \n\t"\
153 "psubw %%mm1, %%mm4 \n\t"\
154 PABS( %%mm5, %%mm3)\
155 PABS( %%mm5, %%mm4)\
156 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff1 */\
157 "psrlw $1, %%mm2 \n\t"\
158 "psrlw $1, %%mm3 \n\t"\
159 "pmaxsw %%mm3, %%mm2 \n\t"\
160 LOAD4("(%[next],%[mrefs])", %%mm3) /* next[x-refs] */\
161 LOAD4("(%[next],%[prefs])", %%mm4) /* next[x+refs] */\
162 "psubw %%mm0, %%mm3 \n\t"\
163 "psubw %%mm1, %%mm4 \n\t"\
164 PABS( %%mm5, %%mm3)\
165 PABS( %%mm5, %%mm4)\
166 "paddw %%mm4, %%mm3 \n\t" /* temporal_diff2 */\
167 "psrlw $1, %%mm3 \n\t"\
168 "pmaxsw %%mm3, %%mm2 \n\t"\
169 "movq %%mm2, %[tmp3] \n\t" /* diff */\
171 "paddw %%mm0, %%mm1 \n\t"\
172 "paddw %%mm0, %%mm0 \n\t"\
173 "psubw %%mm1, %%mm0 \n\t"\
174 "psrlw $1, %%mm1 \n\t" /* spatial_pred */\
175 PABS( %%mm2, %%mm0) /* ABS(c-e) */\
177 "movq -1(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1] */\
178 "movq -1(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1] */\
179 "movq %%mm2, %%mm4 \n\t"\
180 "psubusb %%mm3, %%mm2 \n\t"\
181 "psubusb %%mm4, %%mm3 \n\t"\
182 "pmaxub %%mm3, %%mm2 \n\t"\
183 "pshufw $9,%%mm2, %%mm3 \n\t"\
184 "punpcklbw %%mm7, %%mm2 \n\t" /* ABS(cur[x-refs-1] - cur[x+refs-1]) */\
185 "punpcklbw %%mm7, %%mm3 \n\t" /* ABS(cur[x-refs+1] - cur[x+refs+1]) */\
186 "paddw %%mm2, %%mm0 \n\t"\
187 "paddw %%mm3, %%mm0 \n\t"\
188 "psubw %[pw1], %%mm0 \n\t" /* spatial_score */\
190 CHECK(-2,0)\
191 CHECK1\
192 CHECK(-3,1)\
193 CHECK2\
194 CHECK(0,-2)\
195 CHECK1\
196 CHECK(1,-3)\
197 CHECK2\
199 /* if(p->mode<2) ... */\
200 "movq %[tmp3], %%mm6 \n\t" /* diff */\
201 "cmpl $2, %[mode] \n\t"\
202 "jge 1f \n\t"\
203 LOAD4("(%["prev2"],%[mrefs],2)", %%mm2) /* prev2[x-2*refs] */\
204 LOAD4("(%["next2"],%[mrefs],2)", %%mm4) /* next2[x-2*refs] */\
205 LOAD4("(%["prev2"],%[prefs],2)", %%mm3) /* prev2[x+2*refs] */\
206 LOAD4("(%["next2"],%[prefs],2)", %%mm5) /* next2[x+2*refs] */\
207 "paddw %%mm4, %%mm2 \n\t"\
208 "paddw %%mm5, %%mm3 \n\t"\
209 "psrlw $1, %%mm2 \n\t" /* b */\
210 "psrlw $1, %%mm3 \n\t" /* f */\
211 "movq %[tmp0], %%mm4 \n\t" /* c */\
212 "movq %[tmp1], %%mm5 \n\t" /* d */\
213 "movq %[tmp2], %%mm7 \n\t" /* e */\
214 "psubw %%mm4, %%mm2 \n\t" /* b-c */\
215 "psubw %%mm7, %%mm3 \n\t" /* f-e */\
216 "movq %%mm5, %%mm0 \n\t"\
217 "psubw %%mm4, %%mm5 \n\t" /* d-c */\
218 "psubw %%mm7, %%mm0 \n\t" /* d-e */\
219 "movq %%mm2, %%mm4 \n\t"\
220 "pminsw %%mm3, %%mm2 \n\t"\
221 "pmaxsw %%mm4, %%mm3 \n\t"\
222 "pmaxsw %%mm5, %%mm2 \n\t"\
223 "pminsw %%mm5, %%mm3 \n\t"\
224 "pmaxsw %%mm0, %%mm2 \n\t" /* max */\
225 "pminsw %%mm0, %%mm3 \n\t" /* min */\
226 "pxor %%mm4, %%mm4 \n\t"\
227 "pmaxsw %%mm3, %%mm6 \n\t"\
228 "psubw %%mm2, %%mm4 \n\t" /* -max */\
229 "pmaxsw %%mm4, %%mm6 \n\t" /* diff= MAX3(diff, min, -max); */\
230 "1: \n\t"\
232 "movq %[tmp1], %%mm2 \n\t" /* d */\
233 "movq %%mm2, %%mm3 \n\t"\
234 "psubw %%mm6, %%mm2 \n\t" /* d-diff */\
235 "paddw %%mm6, %%mm3 \n\t" /* d+diff */\
236 "pmaxsw %%mm2, %%mm1 \n\t"\
237 "pminsw %%mm3, %%mm1 \n\t" /* d = clip(spatial_pred, d-diff, d+diff); */\
238 "packuswb %%mm1, %%mm1 \n\t"\
240 :[tmp0]"=m"(tmp0),\
241 [tmp1]"=m"(tmp1),\
242 [tmp2]"=m"(tmp2),\
243 [tmp3]"=m"(tmp3)\
244 :[prev] "r"(prev),\
245 [cur] "r"(cur),\
246 [next] "r"(next),\
247 [prefs]"r"((x86_reg)refs),\
248 [mrefs]"r"((x86_reg)-refs),\
249 [pw1] "m"(pw_1),\
250 [pb1] "m"(pb_1),\
251 [mode] "g"(mode)\
253 __asm__ volatile("movd %%mm1, %0" :"=m"(*dst));\
254 dst += 4;\
255 prev+= 4;\
256 cur += 4;\
257 next+= 4;\
260 if(parity){
261 #define prev2 "prev"
262 #define next2 "cur"
263 FILTER
264 #undef prev2
265 #undef next2
266 }else{
267 #define prev2 "cur"
268 #define next2 "next"
269 FILTER
270 #undef prev2
271 #undef next2
274 #undef LOAD4
275 #undef PABS
276 #undef CHECK
277 #undef CHECK1
278 #undef CHECK2
279 #undef FILTER
281 #endif /* HAVE_MMX */
283 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){
284 int x;
285 uint8_t *prev2= parity ? prev : cur ;
286 uint8_t *next2= parity ? cur : next;
287 for(x=0; x<w; x++){
288 int c= cur[-refs];
289 int d= (prev2[0] + next2[0])>>1;
290 int e= cur[+refs];
291 int temporal_diff0= FFABS(prev2[0] - next2[0]);
292 int temporal_diff1=( FFABS(prev[-refs] - c) + FFABS(prev[+refs] - e) )>>1;
293 int temporal_diff2=( FFABS(next[-refs] - c) + FFABS(next[+refs] - e) )>>1;
294 int diff= FFMAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
295 int spatial_pred= (c+e)>>1;
296 int spatial_score= FFABS(cur[-refs-1] - cur[+refs-1]) + FFABS(c-e)
297 + FFABS(cur[-refs+1] - cur[+refs+1]) - 1;
299 #define CHECK(j)\
300 { int score= FFABS(cur[-refs-1+j] - cur[+refs-1-j])\
301 + FFABS(cur[-refs +j] - cur[+refs -j])\
302 + FFABS(cur[-refs+1+j] - cur[+refs+1-j]);\
303 if(score < spatial_score){\
304 spatial_score= score;\
305 spatial_pred= (cur[-refs +j] + cur[+refs -j])>>1;\
307 CHECK(-1) CHECK(-2) }} }}
308 CHECK( 1) CHECK( 2) }} }}
310 if(p->mode<2){
311 int b= (prev2[-2*refs] + next2[-2*refs])>>1;
312 int f= (prev2[+2*refs] + next2[+2*refs])>>1;
313 #if 0
314 int a= cur[-3*refs];
315 int g= cur[+3*refs];
316 int max= FFMAX3(d-e, d-c, FFMIN3(FFMAX(b-c,f-e),FFMAX(b-c,b-a),FFMAX(f-g,f-e)) );
317 int min= FFMIN3(d-e, d-c, FFMAX3(FFMIN(b-c,f-e),FFMIN(b-c,b-a),FFMIN(f-g,f-e)) );
318 #else
319 int max= FFMAX3(d-e, d-c, FFMIN(b-c, f-e));
320 int min= FFMIN3(d-e, d-c, FFMAX(b-c, f-e));
321 #endif
323 diff= FFMAX3(diff, min, -max);
326 if(spatial_pred > d + diff)
327 spatial_pred = d + diff;
328 else if(spatial_pred < d - diff)
329 spatial_pred = d - diff;
331 dst[0] = spatial_pred;
333 dst++;
334 cur++;
335 prev++;
336 next++;
337 prev2++;
338 next2++;
342 static void filter(struct vf_priv_s *p, uint8_t *dst[3], int dst_stride[3], int width, int height, int parity, int tff){
343 int y, i;
345 for(i=0; i<3; i++){
346 int is_chroma= !!i;
347 int w= width >>is_chroma;
348 int h= height>>is_chroma;
349 int refs= p->stride[i];
351 for(y=0; y<h; y++){
352 if((y ^ parity) & 1){
353 uint8_t *prev= &p->ref[0][i][y*refs];
354 uint8_t *cur = &p->ref[1][i][y*refs];
355 uint8_t *next= &p->ref[2][i][y*refs];
356 uint8_t *dst2= &dst[i][y*dst_stride[i]];
357 filter_line(p, dst2, prev, cur, next, w, refs, parity ^ tff);
358 }else{
359 fast_memcpy(&dst[i][y*dst_stride[i]], &p->ref[1][i][y*refs], w);
363 #if HAVE_MMX
364 if(gCpuCaps.hasMMX2) __asm__ volatile("emms \n\t" : : : "memory");
365 #endif
368 static int config(struct vf_instance *vf,
369 int width, int height, int d_width, int d_height,
370 unsigned int flags, unsigned int outfmt){
371 int i, j;
373 for(i=0; i<3; i++){
374 int is_chroma= !!i;
375 int w= ((width + 31) & (~31))>>is_chroma;
376 int h= ((height+6+ 31) & (~31))>>is_chroma;
378 vf->priv->stride[i]= w;
379 for(j=0; j<3; j++)
380 vf->priv->ref[j][i]= (char *)malloc(w*h*sizeof(uint8_t))+3*w;
383 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
386 static int continue_buffered_image(struct vf_instance *vf);
388 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
389 int tff;
391 if(vf->priv->parity < 0) {
392 if (mpi->fields & MP_IMGFIELD_ORDERED)
393 tff = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
394 else
395 tff = 1;
397 else tff = (vf->priv->parity&1)^1;
399 store_ref(vf->priv, mpi->planes, mpi->stride, mpi->w, mpi->h);
402 double delta;
403 if (vf->priv->buffered_pts == MP_NOPTS_VALUE)
404 delta = 1001.0/60000.0; // delta = field time distance
405 else
406 delta = (pts - vf->priv->buffered_pts) / 2;
407 if (delta <= 0.0 || delta >= 0.5)
408 delta = 0.0;
409 vf->priv->buffered_pts_delta = delta;
412 vf->priv->buffered_mpi = mpi;
413 vf->priv->buffered_tff = tff;
414 vf->priv->buffered_i = 0;
415 vf->priv->buffered_pts = pts;
417 if(vf->priv->do_deinterlace == 0)
418 return vf_next_put_image(vf, mpi, pts);
419 else if(vf->priv->do_deinterlace == 1){
420 vf->priv->do_deinterlace= 2;
421 return 0;
422 }else
423 return continue_buffered_image(vf);
426 static int continue_buffered_image(struct vf_instance *vf)
428 mp_image_t *mpi = vf->priv->buffered_mpi;
429 int tff = vf->priv->buffered_tff;
430 double pts = vf->priv->buffered_pts;
431 int i;
432 int ret=0;
433 mp_image_t *dmpi;
435 pts += (vf->priv->buffered_i - 0.5 * (vf->priv->mode&1)) * vf->priv->buffered_pts_delta;
437 for(i = vf->priv->buffered_i; i<=(vf->priv->mode&1); i++){
438 dmpi=vf_get_image(vf->next,mpi->imgfmt,
439 MP_IMGTYPE_TEMP,
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 (i < (vf->priv->mode & 1))
445 vf_queue_frame(vf, continue_buffered_image);
446 ret |= vf_next_put_image(vf, dmpi, pts);
447 break;
449 vf->priv->buffered_i = 1;
450 return ret;
453 static void uninit(struct vf_instance *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 *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 *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 vf_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 HAVE_MMX
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 vf_open,
521 NULL