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"
32 typedef void (pack_func_t
)(unsigned char *dst
, unsigned char *y
,
33 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
);
40 static void pack_nn_C(unsigned char *dst
, unsigned char *y
,
41 unsigned char *u
, unsigned char *v
, int w
)
44 for (j
= w
/2; j
; j
--) {
52 static void pack_li_0_C(unsigned char *dst
, unsigned char *y
,
53 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
56 for (j
= w
/2; j
; j
--) {
58 *dst
++ = (u
[us
+us
] + 7*u
[0])>>3;
60 *dst
++ = (v
[vs
+vs
] + 7*v
[0])>>3;
65 static void pack_li_1_C(unsigned char *dst
, unsigned char *y
,
66 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
69 for (j
= w
/2; j
; j
--) {
71 *dst
++ = (3*u
[us
+us
] + 5*u
[0])>>3;
73 *dst
++ = (3*v
[vs
+vs
] + 5*v
[0])>>3;
79 static void pack_nn_MMX(unsigned char *dst
, unsigned char *y
,
80 unsigned char *u
, unsigned char *v
, int w
)
85 "movq (%0), %%mm1 \n\t"
86 "movq (%0), %%mm2 \n\t"
87 "movq (%1), %%mm4 \n\t"
88 "movq (%2), %%mm6 \n\t"
89 "punpcklbw %%mm6, %%mm4 \n\t"
90 "punpcklbw %%mm4, %%mm1 \n\t"
91 "punpckhbw %%mm4, %%mm2 \n\t"
96 "movq %%mm1, (%3) \n\t"
97 "movq %%mm2, 8(%3) \n\t"
103 : "r" (y
), "r" (u
), "r" (v
), "r" (dst
), "r" (w
/8)
106 pack_nn_C(dst
, y
, u
, v
, (w
&7));
109 #if HAVE_EBX_AVAILABLE
110 static void pack_li_0_MMX(unsigned char *dst
, unsigned char *y
,
111 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
114 "push %%"REG_BP
" \n\t"
116 "mov %6, %%"REG_BP
" \n\t"
118 "movl 4(%%"REG_d
"), %%"REG_BP
" \n\t"
119 "movl (%%"REG_d
"), %%"REG_d
" \n\t"
121 "pxor %%mm0, %%mm0 \n\t"
125 "movq (%%"REG_S
"), %%mm1 \n\t"
126 "movq (%%"REG_S
"), %%mm2 \n\t"
128 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
129 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
130 "punpcklbw %%mm0, %%mm4 \n\t"
131 "punpcklbw %%mm0, %%mm6 \n\t"
132 "movq (%%"REG_a
"), %%mm3 \n\t"
133 "movq (%%"REG_b
"), %%mm5 \n\t"
134 "punpcklbw %%mm0, %%mm3 \n\t"
135 "punpcklbw %%mm0, %%mm5 \n\t"
136 "paddw %%mm3, %%mm4 \n\t"
137 "paddw %%mm5, %%mm6 \n\t"
138 "paddw %%mm3, %%mm4 \n\t"
139 "paddw %%mm5, %%mm6 \n\t"
140 "paddw %%mm3, %%mm4 \n\t"
141 "paddw %%mm5, %%mm6 \n\t"
142 "paddw %%mm3, %%mm4 \n\t"
143 "paddw %%mm5, %%mm6 \n\t"
144 "paddw %%mm3, %%mm4 \n\t"
145 "paddw %%mm5, %%mm6 \n\t"
146 "paddw %%mm3, %%mm4 \n\t"
147 "paddw %%mm5, %%mm6 \n\t"
148 "paddw %%mm3, %%mm4 \n\t"
149 "paddw %%mm5, %%mm6 \n\t"
150 "psrlw $3, %%mm4 \n\t"
151 "psrlw $3, %%mm6 \n\t"
152 "packuswb %%mm4, %%mm4 \n\t"
153 "packuswb %%mm6, %%mm6 \n\t"
154 "punpcklbw %%mm6, %%mm4 \n\t"
155 "punpcklbw %%mm4, %%mm1 \n\t"
156 "punpckhbw %%mm4, %%mm2 \n\t"
158 "movq %%mm1, (%%"REG_D
") \n\t"
159 "movq %%mm2, 8(%%"REG_D
") \n\t"
161 "movq 8(%%"REG_S
"), %%mm1 \n\t"
162 "movq 8(%%"REG_S
"), %%mm2 \n\t"
164 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
165 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
166 "punpckhbw %%mm0, %%mm4 \n\t"
167 "punpckhbw %%mm0, %%mm6 \n\t"
168 "movq (%%"REG_a
"), %%mm3 \n\t"
169 "movq (%%"REG_b
"), %%mm5 \n\t"
170 "punpckhbw %%mm0, %%mm3 \n\t"
171 "punpckhbw %%mm0, %%mm5 \n\t"
172 "paddw %%mm3, %%mm4 \n\t"
173 "paddw %%mm5, %%mm6 \n\t"
174 "paddw %%mm3, %%mm4 \n\t"
175 "paddw %%mm5, %%mm6 \n\t"
176 "paddw %%mm3, %%mm4 \n\t"
177 "paddw %%mm5, %%mm6 \n\t"
178 "paddw %%mm3, %%mm4 \n\t"
179 "paddw %%mm5, %%mm6 \n\t"
180 "paddw %%mm3, %%mm4 \n\t"
181 "paddw %%mm5, %%mm6 \n\t"
182 "paddw %%mm3, %%mm4 \n\t"
183 "paddw %%mm5, %%mm6 \n\t"
184 "paddw %%mm3, %%mm4 \n\t"
185 "paddw %%mm5, %%mm6 \n\t"
186 "psrlw $3, %%mm4 \n\t"
187 "psrlw $3, %%mm6 \n\t"
188 "packuswb %%mm4, %%mm4 \n\t"
189 "packuswb %%mm6, %%mm6 \n\t"
190 "punpcklbw %%mm6, %%mm4 \n\t"
191 "punpcklbw %%mm4, %%mm1 \n\t"
192 "punpckhbw %%mm4, %%mm2 \n\t"
194 "add $16, %%"REG_S
" \n\t"
195 "add $8, %%"REG_a
" \n\t"
196 "add $8, %%"REG_b
" \n\t"
198 "movq %%mm1, 16(%%"REG_D
") \n\t"
199 "movq %%mm2, 24(%%"REG_D
") \n\t"
200 "add $32, %%"REG_D
" \n\t"
205 "pop %%"REG_BP
" \n\t"
207 : "S" (y
), "D" (dst
), "a" (u
), "b" (v
), "c" (w
/16),
209 "d" ((x86_reg
)us
), "r" ((x86_reg
)vs
)
215 pack_li_0_C(dst
, y
, u
, v
, (w
&15), us
, vs
);
218 static void pack_li_1_MMX(unsigned char *dst
, unsigned char *y
,
219 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
222 "push %%"REG_BP
" \n\t"
224 "mov %6, %%"REG_BP
" \n\t"
226 "movl 4(%%"REG_d
"), %%"REG_BP
" \n\t"
227 "movl (%%"REG_d
"), %%"REG_d
" \n\t"
229 "pxor %%mm0, %%mm0 \n\t"
233 "movq (%%"REG_S
"), %%mm1 \n\t"
234 "movq (%%"REG_S
"), %%mm2 \n\t"
236 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
237 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
238 "punpcklbw %%mm0, %%mm4 \n\t"
239 "punpcklbw %%mm0, %%mm6 \n\t"
240 "movq (%%"REG_a
"), %%mm3 \n\t"
241 "movq (%%"REG_b
"), %%mm5 \n\t"
242 "punpcklbw %%mm0, %%mm3 \n\t"
243 "punpcklbw %%mm0, %%mm5 \n\t"
244 "movq %%mm4, %%mm7 \n\t"
245 "paddw %%mm4, %%mm4 \n\t"
246 "paddw %%mm7, %%mm4 \n\t"
247 "movq %%mm6, %%mm7 \n\t"
248 "paddw %%mm6, %%mm6 \n\t"
249 "paddw %%mm7, %%mm6 \n\t"
250 "paddw %%mm3, %%mm4 \n\t"
251 "paddw %%mm5, %%mm6 \n\t"
252 "paddw %%mm3, %%mm4 \n\t"
253 "paddw %%mm5, %%mm6 \n\t"
254 "paddw %%mm3, %%mm4 \n\t"
255 "paddw %%mm5, %%mm6 \n\t"
256 "paddw %%mm3, %%mm4 \n\t"
257 "paddw %%mm5, %%mm6 \n\t"
258 "paddw %%mm3, %%mm4 \n\t"
259 "paddw %%mm5, %%mm6 \n\t"
260 "psrlw $3, %%mm4 \n\t"
261 "psrlw $3, %%mm6 \n\t"
262 "packuswb %%mm4, %%mm4 \n\t"
263 "packuswb %%mm6, %%mm6 \n\t"
264 "punpcklbw %%mm6, %%mm4 \n\t"
265 "punpcklbw %%mm4, %%mm1 \n\t"
266 "punpckhbw %%mm4, %%mm2 \n\t"
268 "movq %%mm1, (%%"REG_D
") \n\t"
269 "movq %%mm2, 8(%%"REG_D
") \n\t"
271 "movq 8(%%"REG_S
"), %%mm1 \n\t"
272 "movq 8(%%"REG_S
"), %%mm2 \n\t"
274 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
275 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
276 "punpckhbw %%mm0, %%mm4 \n\t"
277 "punpckhbw %%mm0, %%mm6 \n\t"
278 "movq (%%"REG_a
"), %%mm3 \n\t"
279 "movq (%%"REG_b
"), %%mm5 \n\t"
280 "punpckhbw %%mm0, %%mm3 \n\t"
281 "punpckhbw %%mm0, %%mm5 \n\t"
282 "movq %%mm4, %%mm7 \n\t"
283 "paddw %%mm4, %%mm4 \n\t"
284 "paddw %%mm7, %%mm4 \n\t"
285 "movq %%mm6, %%mm7 \n\t"
286 "paddw %%mm6, %%mm6 \n\t"
287 "paddw %%mm7, %%mm6 \n\t"
288 "paddw %%mm3, %%mm4 \n\t"
289 "paddw %%mm5, %%mm6 \n\t"
290 "paddw %%mm3, %%mm4 \n\t"
291 "paddw %%mm5, %%mm6 \n\t"
292 "paddw %%mm3, %%mm4 \n\t"
293 "paddw %%mm5, %%mm6 \n\t"
294 "paddw %%mm3, %%mm4 \n\t"
295 "paddw %%mm5, %%mm6 \n\t"
296 "paddw %%mm3, %%mm4 \n\t"
297 "paddw %%mm5, %%mm6 \n\t"
298 "psrlw $3, %%mm4 \n\t"
299 "psrlw $3, %%mm6 \n\t"
300 "packuswb %%mm4, %%mm4 \n\t"
301 "packuswb %%mm6, %%mm6 \n\t"
302 "punpcklbw %%mm6, %%mm4 \n\t"
303 "punpcklbw %%mm4, %%mm1 \n\t"
304 "punpckhbw %%mm4, %%mm2 \n\t"
306 "add $16, %%"REG_S
" \n\t"
307 "add $8, %%"REG_a
" \n\t"
308 "add $8, %%"REG_b
" \n\t"
310 "movq %%mm1, 16(%%"REG_D
") \n\t"
311 "movq %%mm2, 24(%%"REG_D
") \n\t"
312 "add $32, %%"REG_D
" \n\t"
317 "pop %%"REG_BP
" \n\t"
319 : "S" (y
), "D" (dst
), "a" (u
), "b" (v
), "c" (w
/16),
321 "d" ((x86_reg
)us
), "r" ((x86_reg
)vs
)
327 pack_li_1_C(dst
, y
, u
, v
, (w
&15), us
, vs
);
329 #endif /* HAVE_EBX_AVAILABLE */
332 static pack_func_t
*pack_nn
;
333 static pack_func_t
*pack_li_0
;
334 static pack_func_t
*pack_li_1
;
336 static void ilpack(unsigned char *dst
, unsigned char *src
[3],
337 int dststride
, int srcstride
[3], int w
, int h
, pack_func_t
*pack
[2])
340 unsigned char *y
, *u
, *v
;
341 int ys
= srcstride
[0], us
= srcstride
[1], vs
= srcstride
[2];
348 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
349 y
+= ys
; dst
+= dststride
;
350 pack_nn(dst
, y
, u
+us
, v
+vs
, w
, 0, 0);
351 y
+= ys
; dst
+= dststride
;
352 for (i
=2; i
<h
-2; i
++) {
354 b
= (i
&1) ^ ((i
&2)>>1);
355 pack
[b
](dst
, y
, u
, v
, w
, us
*a
, vs
*a
);
366 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
367 y
+= ys
; dst
+= dststride
; u
+= us
; v
+= vs
;
368 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
372 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
376 // hope we'll get DR buffer:
377 dmpi
=vf_get_image(vf
->next
, IMGFMT_YUY2
,
378 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
381 ilpack(dmpi
->planes
[0], mpi
->planes
, dmpi
->stride
[0], mpi
->stride
, mpi
->w
, mpi
->h
, vf
->priv
->pack
);
383 return vf_next_put_image(vf
,dmpi
, pts
);
386 static int config(struct vf_instance
*vf
,
387 int width
, int height
, int d_width
, int d_height
,
388 unsigned int flags
, unsigned int outfmt
)
390 /* FIXME - also support UYVY output? */
391 return vf_next_config(vf
, width
, height
, d_width
, d_height
, flags
, IMGFMT_YUY2
);
395 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
397 /* FIXME - really any YUV 4:2:0 input format should work */
402 return vf_next_query_format(vf
,IMGFMT_YUY2
);
407 static int vf_open(vf_instance_t
*vf
, char *args
)
410 vf
->query_format
=query_format
;
411 vf
->put_image
=put_image
;
412 vf
->priv
= calloc(1, sizeof(struct vf_priv_s
));
414 if (args
) sscanf(args
, "%d", &vf
->priv
->mode
);
416 pack_nn
= (pack_func_t
*)pack_nn_C
;
417 pack_li_0
= pack_li_0_C
;
418 pack_li_1
= pack_li_1_C
;
420 if(gCpuCaps
.hasMMX
) {
421 pack_nn
= (pack_func_t
*)pack_nn_MMX
;
422 #if HAVE_EBX_AVAILABLE
423 pack_li_0
= pack_li_0_MMX
;
424 pack_li_1
= pack_li_1_MMX
;
429 switch(vf
->priv
->mode
) {
431 vf
->priv
->pack
[0] = vf
->priv
->pack
[1] = pack_nn
;
434 mp_msg(MSGT_VFILTER
, MSGL_WARN
,
435 "ilpack: unknown mode %d (fallback to linear)\n",
438 vf
->priv
->pack
[0] = pack_li_0
;
439 vf
->priv
->pack
[1] = pack_li_1
;
446 const vf_info_t vf_info_ilpack
= {
447 "4:2:0 planar -> 4:2:2 packed reinterlacer",