10 #include "img_format.h"
14 #include "libvo/fastmemcpy.h"
20 mp_image_t
*buffered_mpi
;
24 static void deint(unsigned char *dest
, int ds
, unsigned char *src
, int ss
, int w
, int h
, int field
)
36 if (((src
[x
-ss
] < src
[x
]) && (src
[x
+ss
] < src
[x
])) ||
37 ((src
[x
-ss
] > src
[x
]) && (src
[x
+ss
] > src
[x
]))) {
38 //dest[x] = (src[x+ss] + src[x-ss])>>1;
39 dest
[x
] = ((src
[x
+ss
]<<1) + (src
[x
-ss
]<<1)
40 + src
[x
+ss
+1] + src
[x
-ss
+1]
41 + src
[x
+ss
-1] + src
[x
-ss
-1])>>3;
43 else dest
[x
] = src
[x
];
51 static void qpel_li_3DNOW(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
64 "movq (%%"REG_S
"), %%mm0 \n\t"
65 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
66 "pavgusb %%mm0, %%mm1 \n\t"
67 "add $8, %%"REG_S
" \n\t"
68 "pavgusb %%mm0, %%mm1 \n\t"
69 "movq %%mm1, (%%"REG_D
") \n\t"
70 "add $8, %%"REG_D
" \n\t"
73 : "=S"(crap1
), "=D"(crap2
)
74 : "c"(w
>>3), "S"(s
), "D"(d
), "a"((long)ssd
)
76 for (j
=w
-(w
&7); j
<w
; j
++)
77 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
81 if (!up
) fast_memcpy(d
, s
, w
);
82 __asm__
volatile("emms \n\t" : : : "memory");
87 static void qpel_li_MMX2(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
99 "pxor %%mm7, %%mm7 \n\t"
101 "movq (%%"REG_S
"), %%mm0 \n\t"
102 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
103 "pavgb %%mm0, %%mm1 \n\t"
104 "add $8, %%"REG_S
" \n\t"
105 "pavgb %%mm0, %%mm1 \n\t"
106 "movq %%mm1, (%%"REG_D
") \n\t"
107 "add $8, %%"REG_D
" \n\t"
110 : "=S"(crap1
), "=D"(crap2
)
111 : "c"(w
>>3), "S"(s
), "D"(d
), "a"((long)ssd
)
113 for (j
=w
-(w
&7); j
<w
; j
++)
114 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
118 if (!up
) fast_memcpy(d
, s
, w
);
119 __asm__
volatile("emms \n\t" : : : "memory");
124 static void qpel_li_MMX(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
130 fast_memcpy(d
, s
, w
);
134 for (i
=h
-1; i
; i
--) {
136 "pxor %%mm7, %%mm7 \n\t"
138 "movq (%%"REG_S
"), %%mm0 \n\t"
139 "movq (%%"REG_S
"), %%mm1 \n\t"
140 "movq (%%"REG_S
",%%"REG_a
"), %%mm2 \n\t"
141 "movq (%%"REG_S
",%%"REG_a
"), %%mm3 \n\t"
142 "add $8, %%"REG_S
" \n\t"
143 "punpcklbw %%mm7, %%mm0 \n\t"
144 "punpckhbw %%mm7, %%mm1 \n\t"
145 "punpcklbw %%mm7, %%mm2 \n\t"
146 "punpckhbw %%mm7, %%mm3 \n\t"
147 "paddw %%mm0, %%mm2 \n\t"
148 "paddw %%mm1, %%mm3 \n\t"
149 "paddw %%mm0, %%mm2 \n\t"
150 "paddw %%mm1, %%mm3 \n\t"
151 "paddw %%mm0, %%mm2 \n\t"
152 "paddw %%mm1, %%mm3 \n\t"
153 "psrlw $2, %%mm2 \n\t"
154 "psrlw $2, %%mm3 \n\t"
155 "packsswb %%mm3, %%mm2 \n\t"
156 "movq %%mm2, (%%"REG_D
") \n\t"
157 "add $8, %%"REG_D
" \n\t"
160 : "=S"(crap1
), "=D"(crap2
)
161 : "c"(w
>>3), "S"(s
), "D"(d
), "a"((long)ssd
)
163 for (j
=w
-(w
&7); j
<w
; j
++)
164 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
168 if (!up
) fast_memcpy(d
, s
, w
);
169 __asm__
volatile("emms \n\t" : : : "memory");
172 #if HAVE_EBX_AVAILABLE
173 static void qpel_4tap_MMX(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
176 static const short filter
[] = {
177 29, 29, 29, 29, 110, 110, 110, 110,
178 9, 9, 9, 9, 3, 3, 3, 3,
183 fast_memcpy(d
, s
, w
);
187 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
189 for (i
=h
-3; i
; i
--) {
191 "pxor %%mm0, %%mm0 \n\t"
192 "movq (%%"REG_d
"), %%mm4 \n\t"
193 "movq 8(%%"REG_d
"), %%mm5 \n\t"
194 "movq 16(%%"REG_d
"), %%mm6 \n\t"
195 "movq 24(%%"REG_d
"), %%mm7 \n\t"
198 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
199 "movq (%%"REG_S
"), %%mm2 \n\t"
200 "movq (%%"REG_S
",%%"REG_b
"), %%mm3 \n\t"
201 "punpcklbw %%mm0, %%mm1 \n\t"
202 "punpcklbw %%mm0, %%mm2 \n\t"
203 "pmullw %%mm4, %%mm1 \n\t"
204 "punpcklbw %%mm0, %%mm3 \n\t"
205 "pmullw %%mm5, %%mm2 \n\t"
206 "paddusw %%mm2, %%mm1 \n\t"
207 "pmullw %%mm6, %%mm3 \n\t"
208 "movq (%%"REG_S
",%%"REG_a
",2), %%mm2 \n\t"
209 "psubusw %%mm3, %%mm1 \n\t"
210 "punpcklbw %%mm0, %%mm2 \n\t"
211 "pmullw %%mm7, %%mm2 \n\t"
212 "psubusw %%mm2, %%mm1 \n\t"
213 "psrlw $7, %%mm1 \n\t"
215 "movq (%%"REG_S
",%%"REG_a
"), %%mm2 \n\t"
216 "movq (%%"REG_S
"), %%mm3 \n\t"
217 "punpckhbw %%mm0, %%mm2 \n\t"
218 "punpckhbw %%mm0, %%mm3 \n\t"
219 "pmullw %%mm4, %%mm2 \n\t"
220 "pmullw %%mm5, %%mm3 \n\t"
221 "paddusw %%mm3, %%mm2 \n\t"
222 "movq (%%"REG_S
",%%"REG_b
"), %%mm3 \n\t"
223 "punpckhbw %%mm0, %%mm3 \n\t"
224 "pmullw %%mm6, %%mm3 \n\t"
225 "psubusw %%mm3, %%mm2 \n\t"
226 "movq (%%"REG_S
",%%"REG_a
",2), %%mm3 \n\t"
227 "punpckhbw %%mm0, %%mm3 \n\t"
228 "add $8, %%"REG_S
" \n\t"
229 "pmullw %%mm7, %%mm3 \n\t"
230 "psubusw %%mm3, %%mm2 \n\t"
231 "psrlw $7, %%mm2 \n\t"
233 "packuswb %%mm2, %%mm1 \n\t"
234 "movq %%mm1, (%%"REG_D
") \n\t"
235 "add $8, %%"REG_D
" \n\t"
238 : "=S"(crap1
), "=D"(crap2
)
239 : "c"(w
>>3), "S"(s
), "D"(d
), "a"((long)ssd
), "b"((long)-ssd
), "d"(filter
)
241 for (j
=w
-(w
&7); j
<w
; j
++)
242 d
[j
] = (-9*s
[j
-ssd
] + 111*s
[j
] + 29*s
[j
+ssd
] - 3*s
[j
+ssd
+ssd
])>>7;
247 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
249 if (!up
) fast_memcpy(d
, s
, w
);
250 __asm__
volatile("emms \n\t" : : : "memory");
252 #endif /* HAVE_EBX_AVAILABLE */
255 static inline int clamp(int a
)
257 // If a<512, this is equivalent to:
258 // return (a<0) ? 0 : ( (a>255) ? 255 : a);
259 return (~(a
>>31)) & (a
| ((a
<<23)>>31));
262 static void qpel_li_C(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
267 fast_memcpy(d
, s
, w
);
271 for (i
=h
-1; i
; i
--) {
273 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
277 if (!up
) fast_memcpy(d
, s
, w
);
280 static void qpel_4tap_C(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
285 fast_memcpy(d
, s
, w
);
289 d
[j
] = (s
[j
+ssd
] + 3*s
[j
] + 2)>>2;
291 for (i
=h
-3; i
; i
--) {
293 d
[j
] = clamp((-9*s
[j
-ssd
] + 111*s
[j
] + 29*s
[j
+ssd
] - 3*s
[j
+ssd
+ssd
] + 64)>>7);
297 d
[j
] = (s
[j
+ssd
] + 3*s
[j
] + 2)>>2;
299 if (!up
) fast_memcpy(d
, s
, w
);
302 static void (*qpel_li
)(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
);
303 static void (*qpel_4tap
)(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
);
305 static int continue_buffered_image(struct vf_instance
*);
307 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
)
309 vf
->priv
->buffered_mpi
= mpi
;
310 vf
->priv
->buffered_pts
= pts
;
311 vf
->priv
->buffered_i
= 0;
312 return continue_buffered_image(vf
);
315 extern const int under_mencoder
;
317 static int continue_buffered_image(struct vf_instance
*vf
)
319 int i
=vf
->priv
->buffered_i
;
320 double pts
= vf
->priv
->buffered_pts
;
321 mp_image_t
*mpi
= vf
->priv
->buffered_mpi
;
324 void (*qpel
)(unsigned char *, unsigned char *, int, int, int, int, int);
329 vf_queue_frame(vf
, continue_buffered_image
);
330 pts
+= i
* .02; // XXX not right
332 if (!(mpi
->flags
& MP_IMGFLAG_PLANAR
)) bpp
= mpi
->bpp
/8;
333 if (vf
->priv
->parity
< 0) {
334 if (mpi
->fields
& MP_IMGFIELD_ORDERED
)
335 tff
= mpi
->fields
& MP_IMGFIELD_TOP_FIRST
;
339 else tff
= (vf
->priv
->parity
&1)^1;
341 switch (vf
->priv
->mode
) {
346 // TODO: add 3tap filter
354 switch (vf
->priv
->mode
) {
357 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
358 MP_IMGTYPE_EXPORT
, MP_IMGFLAG_ACCEPT_STRIDE
,
359 mpi
->width
, mpi
->height
/2);
360 dmpi
->planes
[0] = mpi
->planes
[0] + (i
^!tff
)*mpi
->stride
[0];
361 dmpi
->stride
[0] = 2*mpi
->stride
[0];
362 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
363 dmpi
->planes
[1] = mpi
->planes
[1] + (i
^!tff
)*mpi
->stride
[1];
364 dmpi
->planes
[2] = mpi
->planes
[2] + (i
^!tff
)*mpi
->stride
[2];
365 dmpi
->stride
[1] = 2*mpi
->stride
[1];
366 dmpi
->stride
[2] = 2*mpi
->stride
[2];
368 ret
|= vf_next_put_image(vf
, dmpi
, pts
);
375 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
376 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
377 mpi
->width
, mpi
->height
);
378 my_memcpy_pic(dmpi
->planes
[0] + (i
^!tff
)*dmpi
->stride
[0],
379 mpi
->planes
[0] + (i
^!tff
)*mpi
->stride
[0],
380 mpi
->w
*bpp
, mpi
->h
/2, dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
381 deint(dmpi
->planes
[0], dmpi
->stride
[0], mpi
->planes
[0], mpi
->stride
[0], mpi
->w
, mpi
->h
, (i
^!tff
));
382 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
383 my_memcpy_pic(dmpi
->planes
[1] + (i
^!tff
)*dmpi
->stride
[1],
384 mpi
->planes
[1] + (i
^!tff
)*mpi
->stride
[1],
385 mpi
->chroma_width
, mpi
->chroma_height
/2,
386 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
387 my_memcpy_pic(dmpi
->planes
[2] + (i
^!tff
)*dmpi
->stride
[2],
388 mpi
->planes
[2] + (i
^!tff
)*mpi
->stride
[2],
389 mpi
->chroma_width
, mpi
->chroma_height
/2,
390 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
391 deint(dmpi
->planes
[1], dmpi
->stride
[1], mpi
->planes
[1], mpi
->stride
[1],
392 mpi
->chroma_width
, mpi
->chroma_height
, (i
^!tff
));
393 deint(dmpi
->planes
[2], dmpi
->stride
[2], mpi
->planes
[2], mpi
->stride
[2],
394 mpi
->chroma_width
, mpi
->chroma_height
, (i
^!tff
));
396 ret
|= vf_next_put_image(vf
, dmpi
, pts
);
405 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
406 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
407 mpi
->width
, mpi
->height
/2);
408 qpel(dmpi
->planes
[0], mpi
->planes
[0] + (i
^!tff
)*mpi
->stride
[0],
409 mpi
->w
*bpp
, mpi
->h
/2, dmpi
->stride
[0], mpi
->stride
[0]*2, (i
^!tff
));
410 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
411 qpel(dmpi
->planes
[1],
412 mpi
->planes
[1] + (i
^!tff
)*mpi
->stride
[1],
413 mpi
->chroma_width
, mpi
->chroma_height
/2,
414 dmpi
->stride
[1], mpi
->stride
[1]*2, (i
^!tff
));
415 qpel(dmpi
->planes
[2],
416 mpi
->planes
[2] + (i
^!tff
)*mpi
->stride
[2],
417 mpi
->chroma_width
, mpi
->chroma_height
/2,
418 dmpi
->stride
[2], mpi
->stride
[2]*2, (i
^!tff
));
420 ret
|= vf_next_put_image(vf
, dmpi
, pts
);
426 vf
->priv
->buffered_i
= 1;
431 static int query_format(struct vf_instance
* vf
, unsigned int fmt
)
433 /* FIXME - figure out which other formats work */
438 return vf_next_query_format(vf
, fmt
);
444 static int config(struct vf_instance
* vf
,
445 int width
, int height
, int d_width
, int d_height
,
446 unsigned int flags
, unsigned int outfmt
)
448 switch (vf
->priv
->mode
) {
453 return vf_next_config(vf
,width
,height
/2,d_width
,d_height
,flags
,outfmt
);
455 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
460 static void uninit(struct vf_instance
* vf
)
465 static int open(vf_instance_t
*vf
, char* args
)
469 vf
->put_image
= put_image
;
470 //vf->query_format = query_format;
472 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
473 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
475 vf
->priv
->parity
= -1;
476 if (args
) sscanf(args
, "%d:%d", &vf
->priv
->mode
, &vf
->priv
->parity
);
478 qpel_4tap
= qpel_4tap_C
;
480 if(gCpuCaps
.hasMMX
) qpel_li
= qpel_li_MMX
;
481 #if HAVE_EBX_AVAILABLE
482 if(gCpuCaps
.hasMMX
) qpel_4tap
= qpel_4tap_MMX
;
486 if(gCpuCaps
.hasMMX2
) qpel_li
= qpel_li_MMX2
;
489 if(gCpuCaps
.has3DNow
) qpel_li
= qpel_li_3DNOW
;
494 const vf_info_t vf_info_tfields
= {
495 "temporal field separation",