9 #include "img_format.h"
13 #include "libvo/fastmemcpy.h"
19 mp_image_t
*buffered_mpi
;
23 static inline void *my_memcpy_pic(void * dst
, void * src
, int bytesPerLine
, int height
, int dstStride
, int srcStride
)
28 for(i
=0; i
<height
; i
++)
30 memcpy(dst
, src
, bytesPerLine
);
38 static void deint(unsigned char *dest
, int ds
, unsigned char *src
, int ss
, int w
, int h
, int field
)
50 if (((src
[x
-ss
] < src
[x
]) && (src
[x
+ss
] < src
[x
])) ||
51 ((src
[x
-ss
] > src
[x
]) && (src
[x
+ss
] > src
[x
]))) {
52 //dest[x] = (src[x+ss] + src[x-ss])>>1;
53 dest
[x
] = ((src
[x
+ss
]<<1) + (src
[x
-ss
]<<1)
54 + src
[x
+ss
+1] + src
[x
-ss
+1]
55 + src
[x
+ss
-1] + src
[x
-ss
-1])>>3;
57 else dest
[x
] = src
[x
];
65 static void qpel_li_3DNOW(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
78 "movq (%%"REG_S
"), %%mm0 \n\t"
79 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
80 "pavgusb %%mm0, %%mm1 \n\t"
81 "add $8, %%"REG_S
" \n\t"
82 "pavgusb %%mm0, %%mm1 \n\t"
83 "movq %%mm1, (%%"REG_D
") \n\t"
84 "add $8, %%"REG_D
" \n\t"
87 : "=S"(crap1
), "=D"(crap2
)
88 : "c"(w
>>3), "S"(s
), "D"(d
), "a"((long)ssd
)
90 for (j
=w
-(w
&7); j
<w
; j
++)
91 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
95 if (!up
) memcpy(d
, s
, w
);
96 asm volatile("emms \n\t" : : : "memory");
101 static void qpel_li_MMX2(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
111 for (i
=h
-1; i
; i
--) {
113 "pxor %%mm7, %%mm7 \n\t"
115 "movq (%%"REG_S
"), %%mm0 \n\t"
116 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
117 "pavgb %%mm0, %%mm1 \n\t"
118 "add $8, %%"REG_S
" \n\t"
119 "pavgb %%mm0, %%mm1 \n\t"
120 "movq %%mm1, (%%"REG_D
") \n\t"
121 "add $8, %%"REG_D
" \n\t"
124 : "=S"(crap1
), "=D"(crap2
)
125 : "c"(w
>>3), "S"(s
), "D"(d
), "a"((long)ssd
)
127 for (j
=w
-(w
&7); j
<w
; j
++)
128 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
132 if (!up
) memcpy(d
, s
, w
);
133 asm volatile("emms \n\t" : : : "memory");
138 static void qpel_li_MMX(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
148 for (i
=h
-1; i
; i
--) {
150 "pxor %%mm7, %%mm7 \n\t"
152 "movq (%%"REG_S
"), %%mm0 \n\t"
153 "movq (%%"REG_S
"), %%mm1 \n\t"
154 "movq (%%"REG_S
",%%"REG_a
"), %%mm2 \n\t"
155 "movq (%%"REG_S
",%%"REG_a
"), %%mm3 \n\t"
156 "add $8, %%"REG_S
" \n\t"
157 "punpcklbw %%mm7, %%mm0 \n\t"
158 "punpckhbw %%mm7, %%mm1 \n\t"
159 "punpcklbw %%mm7, %%mm2 \n\t"
160 "punpckhbw %%mm7, %%mm3 \n\t"
161 "paddw %%mm0, %%mm2 \n\t"
162 "paddw %%mm1, %%mm3 \n\t"
163 "paddw %%mm0, %%mm2 \n\t"
164 "paddw %%mm1, %%mm3 \n\t"
165 "paddw %%mm0, %%mm2 \n\t"
166 "paddw %%mm1, %%mm3 \n\t"
167 "psrlw $2, %%mm2 \n\t"
168 "psrlw $2, %%mm3 \n\t"
169 "packsswb %%mm3, %%mm2 \n\t"
170 "movq %%mm2, (%%"REG_D
") \n\t"
171 "add $8, %%"REG_D
" \n\t"
174 : "=S"(crap1
), "=D"(crap2
)
175 : "c"(w
>>3), "S"(s
), "D"(d
), "a"((long)ssd
)
177 for (j
=w
-(w
&7); j
<w
; j
++)
178 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
182 if (!up
) memcpy(d
, s
, w
);
183 asm volatile("emms \n\t" : : : "memory");
186 static void qpel_4tap_MMX(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
189 static const short filter
[] = {
190 29, 29, 29, 29, 110, 110, 110, 110,
191 9, 9, 9, 9, 3, 3, 3, 3,
200 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
202 for (i
=h
-3; i
; i
--) {
204 "pxor %%mm0, %%mm0 \n\t"
205 "movq (%%"REG_d
"), %%mm4 \n\t"
206 "movq 8(%%"REG_d
"), %%mm5 \n\t"
207 "movq 16(%%"REG_d
"), %%mm6 \n\t"
208 "movq 24(%%"REG_d
"), %%mm7 \n\t"
211 "movq (%%"REG_S
",%%"REG_a
"), %%mm1 \n\t"
212 "movq (%%"REG_S
"), %%mm2 \n\t"
213 "movq (%%"REG_S
",%%"REG_b
"), %%mm3 \n\t"
214 "punpcklbw %%mm0, %%mm1 \n\t"
215 "punpcklbw %%mm0, %%mm2 \n\t"
216 "pmullw %%mm4, %%mm1 \n\t"
217 "punpcklbw %%mm0, %%mm3 \n\t"
218 "pmullw %%mm5, %%mm2 \n\t"
219 "paddusw %%mm2, %%mm1 \n\t"
220 "pmullw %%mm6, %%mm3 \n\t"
221 "movq (%%"REG_S
",%%"REG_a
",2), %%mm2 \n\t"
222 "psubusw %%mm3, %%mm1 \n\t"
223 "punpcklbw %%mm0, %%mm2 \n\t"
224 "pmullw %%mm7, %%mm2 \n\t"
225 "psubusw %%mm2, %%mm1 \n\t"
226 "psrlw $7, %%mm1 \n\t"
228 "movq (%%"REG_S
",%%"REG_a
"), %%mm2 \n\t"
229 "movq (%%"REG_S
"), %%mm3 \n\t"
230 "punpckhbw %%mm0, %%mm2 \n\t"
231 "punpckhbw %%mm0, %%mm3 \n\t"
232 "pmullw %%mm4, %%mm2 \n\t"
233 "pmullw %%mm5, %%mm3 \n\t"
234 "paddusw %%mm3, %%mm2 \n\t"
235 "movq (%%"REG_S
",%%"REG_b
"), %%mm3 \n\t"
236 "punpckhbw %%mm0, %%mm3 \n\t"
237 "pmullw %%mm6, %%mm3 \n\t"
238 "psubusw %%mm3, %%mm2 \n\t"
239 "movq (%%"REG_S
",%%"REG_a
",2), %%mm3 \n\t"
240 "punpckhbw %%mm0, %%mm3 \n\t"
241 "add $8, %%"REG_S
" \n\t"
242 "pmullw %%mm7, %%mm3 \n\t"
243 "psubusw %%mm3, %%mm2 \n\t"
244 "psrlw $7, %%mm2 \n\t"
246 "packuswb %%mm2, %%mm1 \n\t"
247 "movq %%mm1, (%%"REG_D
") \n\t"
248 "add $8, %%"REG_D
" \n\t"
251 : "=S"(crap1
), "=D"(crap2
)
252 : "c"(w
>>3), "S"(s
), "D"(d
), "a"((long)ssd
), "b"((long)-ssd
), "d"(filter
)
254 for (j
=w
-(w
&7); j
<w
; j
++)
255 d
[j
] = (-9*s
[j
-ssd
] + 111*s
[j
] + 29*s
[j
+ssd
] - 3*s
[j
+ssd
+ssd
])>>7;
260 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
262 if (!up
) memcpy(d
, s
, w
);
263 asm volatile("emms \n\t" : : : "memory");
267 static inline int clamp(int a
)
269 // If a<512, this is equivalent to:
270 // return (a<0) ? 0 : ( (a>255) ? 255 : a);
271 return (~(a
>>31)) & (a
| ((a
<<23)>>31));
274 static void qpel_li_C(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
283 for (i
=h
-1; i
; i
--) {
285 d
[j
] = (s
[j
+ssd
] + 3*s
[j
])>>2;
289 if (!up
) memcpy(d
, s
, w
);
292 static void qpel_4tap_C(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
)
301 d
[j
] = (s
[j
+ssd
] + 3*s
[j
] + 2)>>2;
303 for (i
=h
-3; i
; i
--) {
305 d
[j
] = clamp((-9*s
[j
-ssd
] + 111*s
[j
] + 29*s
[j
+ssd
] - 3*s
[j
+ssd
+ssd
] + 64)>>7);
309 d
[j
] = (s
[j
+ssd
] + 3*s
[j
] + 2)>>2;
311 if (!up
) memcpy(d
, s
, w
);
314 static void (*qpel_li
)(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
);
315 static void (*qpel_4tap
)(unsigned char *d
, unsigned char *s
, int w
, int h
, int ds
, int ss
, int up
);
317 static int continue_buffered_image(struct vf_instance_s
*);
318 extern int correct_pts
;
320 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
)
322 vf
->priv
->buffered_mpi
= mpi
;
323 vf
->priv
->buffered_pts
= pts
;
324 vf
->priv
->buffered_i
= 0;
325 return continue_buffered_image(vf
);
328 static int continue_buffered_image(struct vf_instance_s
*vf
)
330 int i
=vf
->priv
->buffered_i
;
331 double pts
= vf
->priv
->buffered_pts
;
332 mp_image_t
*mpi
= vf
->priv
->buffered_mpi
;
335 void (*qpel
)(unsigned char *, unsigned char *, int, int, int, int, int);
340 vf_queue_frame(vf
, continue_buffered_image
);
341 pts
+= i
* .02; // XXX not right
343 if (!(mpi
->flags
& MP_IMGFLAG_PLANAR
)) bpp
= mpi
->bpp
/8;
344 if (vf
->priv
->parity
< 0) {
345 if (mpi
->fields
& MP_IMGFIELD_ORDERED
)
346 tff
= mpi
->fields
& MP_IMGFIELD_TOP_FIRST
;
350 else tff
= (vf
->priv
->parity
&1)^1;
352 switch (vf
->priv
->mode
) {
357 // TODO: add 3tap filter
365 switch (vf
->priv
->mode
) {
368 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
369 MP_IMGTYPE_EXPORT
, MP_IMGFLAG_ACCEPT_STRIDE
,
370 mpi
->width
, mpi
->height
/2);
371 dmpi
->planes
[0] = mpi
->planes
[0] + (i
^!tff
)*mpi
->stride
[0];
372 dmpi
->stride
[0] = 2*mpi
->stride
[0];
373 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
374 dmpi
->planes
[1] = mpi
->planes
[1] + (i
^!tff
)*mpi
->stride
[1];
375 dmpi
->planes
[2] = mpi
->planes
[2] + (i
^!tff
)*mpi
->stride
[2];
376 dmpi
->stride
[1] = 2*mpi
->stride
[1];
377 dmpi
->stride
[2] = 2*mpi
->stride
[2];
379 ret
|= vf_next_put_image(vf
, dmpi
, pts
);
383 if (!i
) vf_next_control(vf
, VFCTRL_FLIP_PAGE
, NULL
);
388 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
389 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
390 mpi
->width
, mpi
->height
);
391 my_memcpy_pic(dmpi
->planes
[0] + (i
^!tff
)*dmpi
->stride
[0],
392 mpi
->planes
[0] + (i
^!tff
)*mpi
->stride
[0],
393 mpi
->w
*bpp
, mpi
->h
/2, dmpi
->stride
[0]*2, mpi
->stride
[0]*2);
394 deint(dmpi
->planes
[0], dmpi
->stride
[0], mpi
->planes
[0], mpi
->stride
[0], mpi
->w
, mpi
->h
, (i
^!tff
));
395 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
396 my_memcpy_pic(dmpi
->planes
[1] + (i
^!tff
)*dmpi
->stride
[1],
397 mpi
->planes
[1] + (i
^!tff
)*mpi
->stride
[1],
398 mpi
->chroma_width
, mpi
->chroma_height
/2,
399 dmpi
->stride
[1]*2, mpi
->stride
[1]*2);
400 my_memcpy_pic(dmpi
->planes
[2] + (i
^!tff
)*dmpi
->stride
[2],
401 mpi
->planes
[2] + (i
^!tff
)*mpi
->stride
[2],
402 mpi
->chroma_width
, mpi
->chroma_height
/2,
403 dmpi
->stride
[2]*2, mpi
->stride
[2]*2);
404 deint(dmpi
->planes
[1], dmpi
->stride
[1], mpi
->planes
[1], mpi
->stride
[1],
405 mpi
->chroma_width
, mpi
->chroma_height
, (i
^!tff
));
406 deint(dmpi
->planes
[2], dmpi
->stride
[2], mpi
->planes
[2], mpi
->stride
[2],
407 mpi
->chroma_width
, mpi
->chroma_height
, (i
^!tff
));
409 ret
|= vf_next_put_image(vf
, dmpi
, pts
);
413 if (!i
) vf_next_control(vf
, VFCTRL_FLIP_PAGE
, NULL
);
420 dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
421 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
422 mpi
->width
, mpi
->height
/2);
423 qpel(dmpi
->planes
[0], mpi
->planes
[0] + (i
^!tff
)*mpi
->stride
[0],
424 mpi
->w
*bpp
, mpi
->h
/2, dmpi
->stride
[0], mpi
->stride
[0]*2, (i
^!tff
));
425 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
426 qpel(dmpi
->planes
[1],
427 mpi
->planes
[1] + (i
^!tff
)*mpi
->stride
[1],
428 mpi
->chroma_width
, mpi
->chroma_height
/2,
429 dmpi
->stride
[1], mpi
->stride
[1]*2, (i
^!tff
));
430 qpel(dmpi
->planes
[2],
431 mpi
->planes
[2] + (i
^!tff
)*mpi
->stride
[2],
432 mpi
->chroma_width
, mpi
->chroma_height
/2,
433 dmpi
->stride
[2], mpi
->stride
[2]*2, (i
^!tff
));
435 ret
|= vf_next_put_image(vf
, dmpi
, pts
);
439 if (!i
) vf_next_control(vf
, VFCTRL_FLIP_PAGE
, NULL
);
443 vf
->priv
->buffered_i
= 1;
447 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
)
449 /* FIXME - figure out which other formats work */
454 return vf_next_query_format(vf
, fmt
);
459 static int config(struct vf_instance_s
* vf
,
460 int width
, int height
, int d_width
, int d_height
,
461 unsigned int flags
, unsigned int outfmt
)
463 switch (vf
->priv
->mode
) {
468 return vf_next_config(vf
,width
,height
/2,d_width
,d_height
,flags
,outfmt
);
470 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
475 static void uninit(struct vf_instance_s
* vf
)
480 static int open(vf_instance_t
*vf
, char* args
)
484 vf
->put_image
= put_image
;
485 //vf->query_format = query_format;
487 vf
->default_reqs
= VFCAP_ACCEPT_STRIDE
;
488 vf
->priv
= p
= calloc(1, sizeof(struct vf_priv_s
));
490 vf
->priv
->parity
= -1;
491 if (args
) sscanf(args
, "%d:%d", &vf
->priv
->mode
, &vf
->priv
->parity
);
493 qpel_4tap
= qpel_4tap_C
;
495 if(gCpuCaps
.hasMMX
) qpel_li
= qpel_li_MMX
;
496 if(gCpuCaps
.hasMMX
) qpel_4tap
= qpel_4tap_MMX
;
499 if(gCpuCaps
.hasMMX2
) qpel_li
= qpel_li_MMX2
;
502 if(gCpuCaps
.has3DNow
) qpel_li
= qpel_li_3DNOW
;
507 vf_info_t vf_info_tfields
= {
508 "temporal field separation",