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"
31 #include "libavutil/attributes.h"
33 typedef void (pack_func_t
)(unsigned char *dst
, unsigned char *y
,
34 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
);
41 static void pack_nn_C(unsigned char *dst
, unsigned char *y
,
42 unsigned char *u
, unsigned char *v
, int w
,
43 int av_unused us
, int av_unused vs
)
46 for (j
= w
/2; j
; j
--) {
54 static void pack_li_0_C(unsigned char *dst
, unsigned char *y
,
55 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
58 for (j
= w
/2; j
; j
--) {
60 *dst
++ = (u
[us
+us
] + 7*u
[0])>>3;
62 *dst
++ = (v
[vs
+vs
] + 7*v
[0])>>3;
67 static void pack_li_1_C(unsigned char *dst
, unsigned char *y
,
68 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
71 for (j
= w
/2; j
; j
--) {
73 *dst
++ = (3*u
[us
+us
] + 5*u
[0])>>3;
75 *dst
++ = (3*v
[vs
+vs
] + 5*v
[0])>>3;
81 static void pack_nn_MMX(unsigned char *dst
, unsigned char *y
,
82 unsigned char *u
, unsigned char *v
, int w
,
83 int av_unused us
, int av_unused vs
)
88 "movq (%0), %%mm1 \n\t"
89 "movq (%0), %%mm2 \n\t"
90 "movq (%1), %%mm4 \n\t"
91 "movq (%2), %%mm6 \n\t"
92 "punpcklbw %%mm6, %%mm4 \n\t"
93 "punpcklbw %%mm4, %%mm1 \n\t"
94 "punpckhbw %%mm4, %%mm2 \n\t"
99 "movq %%mm1, (%3) \n\t"
100 "movq %%mm2, 8(%3) \n\t"
106 : "r" (y
), "r" (u
), "r" (v
), "r" (dst
), "r" (w
/8)
109 pack_nn_C(dst
, y
, u
, v
, (w
&7), 0, 0);
112 #if HAVE_EBX_AVAILABLE
113 static void pack_li_0_MMX(unsigned char *dst
, unsigned char *y
,
114 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
117 "push %%"REG_BP
" \n\t"
119 "mov %6, %%"REG_BP
" \n\t"
121 "movl 4(%%"REG_d
"), %%"REG_BP
" \n\t"
122 "movl (%%"REG_d
"), %%"REG_d
" \n\t"
124 "pxor %%mm0, %%mm0 \n\t"
128 "movq (%%"REG_S
"), %%mm1 \n\t"
129 "movq (%%"REG_S
"), %%mm2 \n\t"
131 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
132 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
133 "punpcklbw %%mm0, %%mm4 \n\t"
134 "punpcklbw %%mm0, %%mm6 \n\t"
135 "movq (%%"REG_a
"), %%mm3 \n\t"
136 "movq (%%"REG_b
"), %%mm5 \n\t"
137 "punpcklbw %%mm0, %%mm3 \n\t"
138 "punpcklbw %%mm0, %%mm5 \n\t"
139 "paddw %%mm3, %%mm4 \n\t"
140 "paddw %%mm5, %%mm6 \n\t"
141 "paddw %%mm3, %%mm4 \n\t"
142 "paddw %%mm5, %%mm6 \n\t"
143 "paddw %%mm3, %%mm4 \n\t"
144 "paddw %%mm5, %%mm6 \n\t"
145 "paddw %%mm3, %%mm4 \n\t"
146 "paddw %%mm5, %%mm6 \n\t"
147 "paddw %%mm3, %%mm4 \n\t"
148 "paddw %%mm5, %%mm6 \n\t"
149 "paddw %%mm3, %%mm4 \n\t"
150 "paddw %%mm5, %%mm6 \n\t"
151 "paddw %%mm3, %%mm4 \n\t"
152 "paddw %%mm5, %%mm6 \n\t"
153 "psrlw $3, %%mm4 \n\t"
154 "psrlw $3, %%mm6 \n\t"
155 "packuswb %%mm4, %%mm4 \n\t"
156 "packuswb %%mm6, %%mm6 \n\t"
157 "punpcklbw %%mm6, %%mm4 \n\t"
158 "punpcklbw %%mm4, %%mm1 \n\t"
159 "punpckhbw %%mm4, %%mm2 \n\t"
161 "movq %%mm1, (%%"REG_D
") \n\t"
162 "movq %%mm2, 8(%%"REG_D
") \n\t"
164 "movq 8(%%"REG_S
"), %%mm1 \n\t"
165 "movq 8(%%"REG_S
"), %%mm2 \n\t"
167 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
168 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
169 "punpckhbw %%mm0, %%mm4 \n\t"
170 "punpckhbw %%mm0, %%mm6 \n\t"
171 "movq (%%"REG_a
"), %%mm3 \n\t"
172 "movq (%%"REG_b
"), %%mm5 \n\t"
173 "punpckhbw %%mm0, %%mm3 \n\t"
174 "punpckhbw %%mm0, %%mm5 \n\t"
175 "paddw %%mm3, %%mm4 \n\t"
176 "paddw %%mm5, %%mm6 \n\t"
177 "paddw %%mm3, %%mm4 \n\t"
178 "paddw %%mm5, %%mm6 \n\t"
179 "paddw %%mm3, %%mm4 \n\t"
180 "paddw %%mm5, %%mm6 \n\t"
181 "paddw %%mm3, %%mm4 \n\t"
182 "paddw %%mm5, %%mm6 \n\t"
183 "paddw %%mm3, %%mm4 \n\t"
184 "paddw %%mm5, %%mm6 \n\t"
185 "paddw %%mm3, %%mm4 \n\t"
186 "paddw %%mm5, %%mm6 \n\t"
187 "paddw %%mm3, %%mm4 \n\t"
188 "paddw %%mm5, %%mm6 \n\t"
189 "psrlw $3, %%mm4 \n\t"
190 "psrlw $3, %%mm6 \n\t"
191 "packuswb %%mm4, %%mm4 \n\t"
192 "packuswb %%mm6, %%mm6 \n\t"
193 "punpcklbw %%mm6, %%mm4 \n\t"
194 "punpcklbw %%mm4, %%mm1 \n\t"
195 "punpckhbw %%mm4, %%mm2 \n\t"
197 "add $16, %%"REG_S
" \n\t"
198 "add $8, %%"REG_a
" \n\t"
199 "add $8, %%"REG_b
" \n\t"
201 "movq %%mm1, 16(%%"REG_D
") \n\t"
202 "movq %%mm2, 24(%%"REG_D
") \n\t"
203 "add $32, %%"REG_D
" \n\t"
208 "pop %%"REG_BP
" \n\t"
210 : "S" (y
), "D" (dst
), "a" (u
), "b" (v
), "c" (w
/16),
212 "d" ((x86_reg
)us
), "r" ((x86_reg
)vs
)
218 pack_li_0_C(dst
, y
, u
, v
, (w
&15), us
, vs
);
221 static void pack_li_1_MMX(unsigned char *dst
, unsigned char *y
,
222 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
225 "push %%"REG_BP
" \n\t"
227 "mov %6, %%"REG_BP
" \n\t"
229 "movl 4(%%"REG_d
"), %%"REG_BP
" \n\t"
230 "movl (%%"REG_d
"), %%"REG_d
" \n\t"
232 "pxor %%mm0, %%mm0 \n\t"
236 "movq (%%"REG_S
"), %%mm1 \n\t"
237 "movq (%%"REG_S
"), %%mm2 \n\t"
239 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
240 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
241 "punpcklbw %%mm0, %%mm4 \n\t"
242 "punpcklbw %%mm0, %%mm6 \n\t"
243 "movq (%%"REG_a
"), %%mm3 \n\t"
244 "movq (%%"REG_b
"), %%mm5 \n\t"
245 "punpcklbw %%mm0, %%mm3 \n\t"
246 "punpcklbw %%mm0, %%mm5 \n\t"
247 "movq %%mm4, %%mm7 \n\t"
248 "paddw %%mm4, %%mm4 \n\t"
249 "paddw %%mm7, %%mm4 \n\t"
250 "movq %%mm6, %%mm7 \n\t"
251 "paddw %%mm6, %%mm6 \n\t"
252 "paddw %%mm7, %%mm6 \n\t"
253 "paddw %%mm3, %%mm4 \n\t"
254 "paddw %%mm5, %%mm6 \n\t"
255 "paddw %%mm3, %%mm4 \n\t"
256 "paddw %%mm5, %%mm6 \n\t"
257 "paddw %%mm3, %%mm4 \n\t"
258 "paddw %%mm5, %%mm6 \n\t"
259 "paddw %%mm3, %%mm4 \n\t"
260 "paddw %%mm5, %%mm6 \n\t"
261 "paddw %%mm3, %%mm4 \n\t"
262 "paddw %%mm5, %%mm6 \n\t"
263 "psrlw $3, %%mm4 \n\t"
264 "psrlw $3, %%mm6 \n\t"
265 "packuswb %%mm4, %%mm4 \n\t"
266 "packuswb %%mm6, %%mm6 \n\t"
267 "punpcklbw %%mm6, %%mm4 \n\t"
268 "punpcklbw %%mm4, %%mm1 \n\t"
269 "punpckhbw %%mm4, %%mm2 \n\t"
271 "movq %%mm1, (%%"REG_D
") \n\t"
272 "movq %%mm2, 8(%%"REG_D
") \n\t"
274 "movq 8(%%"REG_S
"), %%mm1 \n\t"
275 "movq 8(%%"REG_S
"), %%mm2 \n\t"
277 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
278 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
279 "punpckhbw %%mm0, %%mm4 \n\t"
280 "punpckhbw %%mm0, %%mm6 \n\t"
281 "movq (%%"REG_a
"), %%mm3 \n\t"
282 "movq (%%"REG_b
"), %%mm5 \n\t"
283 "punpckhbw %%mm0, %%mm3 \n\t"
284 "punpckhbw %%mm0, %%mm5 \n\t"
285 "movq %%mm4, %%mm7 \n\t"
286 "paddw %%mm4, %%mm4 \n\t"
287 "paddw %%mm7, %%mm4 \n\t"
288 "movq %%mm6, %%mm7 \n\t"
289 "paddw %%mm6, %%mm6 \n\t"
290 "paddw %%mm7, %%mm6 \n\t"
291 "paddw %%mm3, %%mm4 \n\t"
292 "paddw %%mm5, %%mm6 \n\t"
293 "paddw %%mm3, %%mm4 \n\t"
294 "paddw %%mm5, %%mm6 \n\t"
295 "paddw %%mm3, %%mm4 \n\t"
296 "paddw %%mm5, %%mm6 \n\t"
297 "paddw %%mm3, %%mm4 \n\t"
298 "paddw %%mm5, %%mm6 \n\t"
299 "paddw %%mm3, %%mm4 \n\t"
300 "paddw %%mm5, %%mm6 \n\t"
301 "psrlw $3, %%mm4 \n\t"
302 "psrlw $3, %%mm6 \n\t"
303 "packuswb %%mm4, %%mm4 \n\t"
304 "packuswb %%mm6, %%mm6 \n\t"
305 "punpcklbw %%mm6, %%mm4 \n\t"
306 "punpcklbw %%mm4, %%mm1 \n\t"
307 "punpckhbw %%mm4, %%mm2 \n\t"
309 "add $16, %%"REG_S
" \n\t"
310 "add $8, %%"REG_a
" \n\t"
311 "add $8, %%"REG_b
" \n\t"
313 "movq %%mm1, 16(%%"REG_D
") \n\t"
314 "movq %%mm2, 24(%%"REG_D
") \n\t"
315 "add $32, %%"REG_D
" \n\t"
320 "pop %%"REG_BP
" \n\t"
322 : "S" (y
), "D" (dst
), "a" (u
), "b" (v
), "c" (w
/16),
324 "d" ((x86_reg
)us
), "r" ((x86_reg
)vs
)
330 pack_li_1_C(dst
, y
, u
, v
, (w
&15), us
, vs
);
332 #endif /* HAVE_EBX_AVAILABLE */
335 static pack_func_t
*pack_nn
;
336 static pack_func_t
*pack_li_0
;
337 static pack_func_t
*pack_li_1
;
339 static void ilpack(unsigned char *dst
, unsigned char *src
[3],
340 int dststride
, int srcstride
[3], int w
, int h
, pack_func_t
*pack
[2])
343 unsigned char *y
, *u
, *v
;
344 int ys
= srcstride
[0], us
= srcstride
[1], vs
= srcstride
[2];
351 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
352 y
+= ys
; dst
+= dststride
;
353 pack_nn(dst
, y
, u
+us
, v
+vs
, w
, 0, 0);
354 y
+= ys
; dst
+= dststride
;
355 for (i
=2; i
<h
-2; i
++) {
357 b
= (i
&1) ^ ((i
&2)>>1);
358 pack
[b
](dst
, y
, u
, v
, w
, us
*a
, vs
*a
);
369 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
370 y
+= ys
; dst
+= dststride
; u
+= us
; v
+= vs
;
371 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
375 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
379 // hope we'll get DR buffer:
380 dmpi
=vf_get_image(vf
->next
, IMGFMT_YUY2
,
381 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
384 ilpack(dmpi
->planes
[0], mpi
->planes
, dmpi
->stride
[0], mpi
->stride
, mpi
->w
, mpi
->h
, vf
->priv
->pack
);
386 return vf_next_put_image(vf
,dmpi
, pts
);
389 static int config(struct vf_instance
*vf
,
390 int width
, int height
, int d_width
, int d_height
,
391 unsigned int flags
, unsigned int outfmt
)
393 /* FIXME - also support UYVY output? */
394 return vf_next_config(vf
, width
, height
, d_width
, d_height
, flags
, IMGFMT_YUY2
);
398 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
400 /* FIXME - really any YUV 4:2:0 input format should work */
405 return vf_next_query_format(vf
,IMGFMT_YUY2
);
410 static int vf_open(vf_instance_t
*vf
, char *args
)
413 vf
->query_format
=query_format
;
414 vf
->put_image
=put_image
;
415 vf
->priv
= calloc(1, sizeof(struct vf_priv_s
));
417 if (args
) sscanf(args
, "%d", &vf
->priv
->mode
);
420 pack_li_0
= pack_li_0_C
;
421 pack_li_1
= pack_li_1_C
;
423 if(gCpuCaps
.hasMMX
) {
424 pack_nn
= pack_nn_MMX
;
425 #if HAVE_EBX_AVAILABLE
426 pack_li_0
= pack_li_0_MMX
;
427 pack_li_1
= pack_li_1_MMX
;
432 switch(vf
->priv
->mode
) {
434 vf
->priv
->pack
[0] = vf
->priv
->pack
[1] = pack_nn
;
437 mp_msg(MSGT_VFILTER
, MSGL_WARN
,
438 "ilpack: unknown mode %d (fallback to linear)\n",
441 vf
->priv
->pack
[0] = pack_li_0
;
442 vf
->priv
->pack
[1] = pack_li_1
;
449 const vf_info_t vf_info_ilpack
= {
450 "4:2:0 planar -> 4:2:2 packed reinterlacer",