2 * This file is part of MPlayer.
4 * MPlayer 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 * MPlayer 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 along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "cpudetect.h"
28 #include "img_format.h"
33 #include "libswscale/swscale.h"
34 #include "fmt-conversion.h"
38 struct SwsContext
*ctx
;
42 static void halfpack_MMX(unsigned char *dst
, unsigned char *src
[3],
43 int dststride
, int srcstride
[3],
47 unsigned char *y1
, *y2
, *u
, *v
;
48 int dstinc
, yinc
, uinc
, vinc
;
51 y2
= src
[0] + srcstride
[0];
55 dstinc
= dststride
- 2*w
;
56 yinc
= 2*srcstride
[0] - w
;
57 uinc
= srcstride
[1] - w
/2;
58 vinc
= srcstride
[2] - w
/2;
62 "pxor %%mm0, %%mm0 \n\t"
65 "movq (%0), %%mm1 \n\t"
66 "movq (%0), %%mm2 \n\t"
67 "movq (%1), %%mm3 \n\t"
68 "movq (%1), %%mm4 \n\t"
69 "punpcklbw %%mm0, %%mm1 \n\t"
70 "punpckhbw %%mm0, %%mm2 \n\t"
71 "punpcklbw %%mm0, %%mm3 \n\t"
72 "punpckhbw %%mm0, %%mm4 \n\t"
73 "paddw %%mm3, %%mm1 \n\t"
74 "paddw %%mm4, %%mm2 \n\t"
75 "psrlw $1, %%mm1 \n\t"
76 "psrlw $1, %%mm2 \n\t"
78 "movq (%2), %%mm3 \n\t"
79 "movq (%3), %%mm5 \n\t"
80 "punpcklbw %%mm0, %%mm3 \n\t"
81 "punpcklbw %%mm0, %%mm5 \n\t"
82 "movq %%mm3, %%mm4 \n\t"
83 "movq %%mm5, %%mm6 \n\t"
84 "punpcklwd %%mm0, %%mm3 \n\t"
85 "punpckhwd %%mm0, %%mm4 \n\t"
86 "punpcklwd %%mm0, %%mm5 \n\t"
87 "punpckhwd %%mm0, %%mm6 \n\t"
88 "pslld $8, %%mm3 \n\t"
89 "pslld $8, %%mm4 \n\t"
90 "pslld $24, %%mm5 \n\t"
91 "pslld $24, %%mm6 \n\t"
93 "por %%mm3, %%mm1 \n\t"
94 "por %%mm4, %%mm2 \n\t"
95 "por %%mm5, %%mm1 \n\t"
96 "por %%mm6, %%mm2 \n\t"
102 "movq %%mm1, (%8) \n\t"
103 "movq %%mm2, 8(%8) \n\t"
107 : "=r" (y1
), "=r" (y2
), "=r" (u
), "=r" (v
)
108 : "0" (y1
), "1" (y2
), "2" (u
), "3" (v
), "r" (dst
), "r" (w
/8)
111 for (j
= (w
&7)/2; j
; j
--) {
112 *dst
++ = (*y1
++ + *y2
++)/2;
114 *dst
++ = (*y1
++ + *y2
++)/2;
123 __asm__
volatile ( "emms \n\t" ::: "memory" );
129 static void halfpack_C(unsigned char *dst
, unsigned char *src
[3],
130 int dststride
, int srcstride
[3],
134 unsigned char *y1
, *y2
, *u
, *v
;
135 int dstinc
, yinc
, uinc
, vinc
;
138 y2
= src
[0] + srcstride
[0];
142 dstinc
= dststride
- 2*w
;
143 yinc
= 2*srcstride
[0] - w
;
144 uinc
= srcstride
[1] - w
/2;
145 vinc
= srcstride
[2] - w
/2;
147 for (i
= h
/2; i
; i
--) {
148 for (j
= w
/2; j
; j
--) {
149 *dst
++ = (*y1
++ + *y2
++)>>1;
151 *dst
++ = (*y1
++ + *y2
++)>>1;
162 static void (*halfpack
)(unsigned char *dst
, unsigned char *src
[3],
163 int dststride
, int srcstride
[3], int w
, int h
);
166 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
168 const uint8_t *src
[MP_MAX_PLANES
] = {
169 mpi
->planes
[0] + mpi
->stride
[0]*vf
->priv
->field
,
170 mpi
->planes
[1], mpi
->planes
[2], NULL
};
171 int src_stride
[MP_MAX_PLANES
] = {mpi
->stride
[0]*2, mpi
->stride
[1], mpi
->stride
[2], 0};
174 // hope we'll get DR buffer:
175 dmpi
=vf_get_image(vf
->next
, IMGFMT_YUY2
,
176 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
179 switch(vf
->priv
->field
) {
182 sws_scale(vf
->priv
->ctx
, src
, src_stride
,
183 0, mpi
->h
/2, dmpi
->planes
, dmpi
->stride
);
186 halfpack(dmpi
->planes
[0], mpi
->planes
, dmpi
->stride
[0],
187 mpi
->stride
, mpi
->w
, mpi
->h
);
190 return vf_next_put_image(vf
,dmpi
, pts
);
193 static int config(struct vf_instance
*vf
,
194 int width
, int height
, int d_width
, int d_height
,
195 unsigned int flags
, unsigned int outfmt
)
197 if (vf
->priv
->field
< 2) {
198 sws_freeContext(vf
->priv
->ctx
);
199 // get unscaled 422p -> yuy2 conversion
201 sws_getContext(width
, height
/ 2, PIX_FMT_YUV422P
,
202 width
, height
/ 2, PIX_FMT_YUYV422
,
203 SWS_POINT
| SWS_PRINT_INFO
| get_sws_cpuflags(),
206 /* FIXME - also support UYVY output? */
207 return vf_next_config(vf
, width
, height
/2, d_width
, d_height
, flags
, IMGFMT_YUY2
);
211 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
213 /* FIXME - really any YUV 4:2:0 input format should work */
218 return vf_next_query_format(vf
,IMGFMT_YUY2
);
223 static void uninit(struct vf_instance
*vf
)
225 sws_freeContext(vf
->priv
->ctx
);
229 static int vf_open(vf_instance_t
*vf
, char *args
)
232 vf
->query_format
=query_format
;
233 vf
->put_image
=put_image
;
236 vf
->priv
= calloc(1, sizeof (struct vf_priv_s
));
238 if (args
) sscanf(args
, "%d", &vf
->priv
->field
);
240 halfpack
= halfpack_C
;
242 if(gCpuCaps
.hasMMX
) halfpack
= halfpack_MMX
;
247 const vf_info_t vf_info_halfpack
= {
248 "yuv planar 4:2:0 -> packed 4:2:2, half height",