10 #include "img_format.h"
15 #include "libswscale/swscale.h"
16 #include "fmt-conversion.h"
20 struct SwsContext
*ctx
;
24 static void halfpack_MMX(unsigned char *dst
, unsigned char *src
[3],
25 int dststride
, int srcstride
[3],
29 unsigned char *y1
, *y2
, *u
, *v
;
30 int dstinc
, yinc
, uinc
, vinc
;
33 y2
= src
[0] + srcstride
[0];
37 dstinc
= dststride
- 2*w
;
38 yinc
= 2*srcstride
[0] - w
;
39 uinc
= srcstride
[1] - w
/2;
40 vinc
= srcstride
[2] - w
/2;
44 "pxor %%mm0, %%mm0 \n\t"
47 "movq (%0), %%mm1 \n\t"
48 "movq (%0), %%mm2 \n\t"
49 "movq (%1), %%mm3 \n\t"
50 "movq (%1), %%mm4 \n\t"
51 "punpcklbw %%mm0, %%mm1 \n\t"
52 "punpckhbw %%mm0, %%mm2 \n\t"
53 "punpcklbw %%mm0, %%mm3 \n\t"
54 "punpckhbw %%mm0, %%mm4 \n\t"
55 "paddw %%mm3, %%mm1 \n\t"
56 "paddw %%mm4, %%mm2 \n\t"
57 "psrlw $1, %%mm1 \n\t"
58 "psrlw $1, %%mm2 \n\t"
60 "movq (%2), %%mm3 \n\t"
61 "movq (%3), %%mm5 \n\t"
62 "punpcklbw %%mm0, %%mm3 \n\t"
63 "punpcklbw %%mm0, %%mm5 \n\t"
64 "movq %%mm3, %%mm4 \n\t"
65 "movq %%mm5, %%mm6 \n\t"
66 "punpcklwd %%mm0, %%mm3 \n\t"
67 "punpckhwd %%mm0, %%mm4 \n\t"
68 "punpcklwd %%mm0, %%mm5 \n\t"
69 "punpckhwd %%mm0, %%mm6 \n\t"
70 "pslld $8, %%mm3 \n\t"
71 "pslld $8, %%mm4 \n\t"
72 "pslld $24, %%mm5 \n\t"
73 "pslld $24, %%mm6 \n\t"
75 "por %%mm3, %%mm1 \n\t"
76 "por %%mm4, %%mm2 \n\t"
77 "por %%mm5, %%mm1 \n\t"
78 "por %%mm6, %%mm2 \n\t"
84 "movq %%mm1, (%8) \n\t"
85 "movq %%mm2, 8(%8) \n\t"
89 : "=r" (y1
), "=r" (y2
), "=r" (u
), "=r" (v
)
90 : "0" (y1
), "1" (y2
), "2" (u
), "3" (v
), "r" (dst
), "r" (w
/8)
93 for (j
= (w
&7)/2; j
; j
--) {
94 *dst
++ = (*y1
++ + *y2
++)/2;
96 *dst
++ = (*y1
++ + *y2
++)/2;
105 __asm__
volatile ( "emms \n\t" ::: "memory" );
111 static void halfpack_C(unsigned char *dst
, unsigned char *src
[3],
112 int dststride
, int srcstride
[3],
116 unsigned char *y1
, *y2
, *u
, *v
;
117 int dstinc
, yinc
, uinc
, vinc
;
120 y2
= src
[0] + srcstride
[0];
124 dstinc
= dststride
- 2*w
;
125 yinc
= 2*srcstride
[0] - w
;
126 uinc
= srcstride
[1] - w
/2;
127 vinc
= srcstride
[2] - w
/2;
129 for (i
= h
/2; i
; i
--) {
130 for (j
= w
/2; j
; j
--) {
131 *dst
++ = (*y1
++ + *y2
++)>>1;
133 *dst
++ = (*y1
++ + *y2
++)>>1;
144 static void (*halfpack
)(unsigned char *dst
, unsigned char *src
[3],
145 int dststride
, int srcstride
[3], int w
, int h
);
148 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
)
150 const uint8_t *src
[MP_MAX_PLANES
] = {
151 mpi
->planes
[0] + mpi
->stride
[0]*vf
->priv
->field
,
152 mpi
->planes
[1], mpi
->planes
[2], NULL
};
153 int src_stride
[MP_MAX_PLANES
] = {mpi
->stride
[0]*2, mpi
->stride
[1], mpi
->stride
[2], 0};
156 // hope we'll get DR buffer:
157 dmpi
=vf_get_image(vf
->next
, IMGFMT_YUY2
,
158 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
161 switch(vf
->priv
->field
) {
164 sws_scale(vf
->priv
->ctx
, src
, src_stride
,
165 0, mpi
->h
/2, dmpi
->planes
, dmpi
->stride
);
168 halfpack(dmpi
->planes
[0], mpi
->planes
, dmpi
->stride
[0],
169 mpi
->stride
, mpi
->w
, mpi
->h
);
172 return vf_next_put_image(vf
,dmpi
, pts
);
175 static int config(struct vf_instance
* vf
,
176 int width
, int height
, int d_width
, int d_height
,
177 unsigned int flags
, unsigned int outfmt
)
179 if (vf
->priv
->field
< 2) {
180 sws_freeContext(vf
->priv
->ctx
);
181 // get unscaled 422p -> yuy2 conversion
183 sws_getContext(width
, height
/ 2, PIX_FMT_YUV422P
,
184 width
, height
/ 2, PIX_FMT_YUYV422
,
185 SWS_POINT
| SWS_PRINT_INFO
| get_sws_cpuflags(),
188 /* FIXME - also support UYVY output? */
189 return vf_next_config(vf
, width
, height
/2, d_width
, d_height
, flags
, IMGFMT_YUY2
);
193 static int query_format(struct vf_instance
* vf
, unsigned int fmt
)
195 /* FIXME - really any YUV 4:2:0 input format should work */
200 return vf_next_query_format(vf
,IMGFMT_YUY2
);
205 static void uninit(struct vf_instance
* vf
)
207 sws_freeContext(vf
->priv
->ctx
);
211 static int open(vf_instance_t
*vf
, char* args
)
214 vf
->query_format
=query_format
;
215 vf
->put_image
=put_image
;
218 vf
->priv
= calloc(1, sizeof (struct vf_priv_s
));
220 if (args
) sscanf(args
, "%d", &vf
->priv
->field
);
222 halfpack
= halfpack_C
;
224 if(gCpuCaps
.hasMMX
) halfpack
= halfpack_MMX
;
229 const vf_info_t vf_info_halfpack
= {
230 "yuv planar 4:2:0 -> packed 4:2:2, half height",