11 #include "img_format.h"
15 #include "libvo/fastmemcpy.h"
16 #include "postproc/rgb2rgb.h"
18 typedef void (pack_func_t
)(unsigned char *dst
, unsigned char *y
,
19 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
);
26 static void pack_nn_C(unsigned char *dst
, unsigned char *y
,
27 unsigned char *u
, unsigned char *v
, int w
)
30 for (j
= w
/2; j
; j
--) {
38 static void pack_li_0_C(unsigned char *dst
, unsigned char *y
,
39 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
42 for (j
= w
/2; j
; j
--) {
44 *dst
++ = (u
[us
+us
] + 7*u
[0])>>3;
46 *dst
++ = (v
[vs
+vs
] + 7*v
[0])>>3;
51 static void pack_li_1_C(unsigned char *dst
, unsigned char *y
,
52 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
55 for (j
= w
/2; j
; j
--) {
57 *dst
++ = (3*u
[us
+us
] + 5*u
[0])>>3;
59 *dst
++ = (3*v
[vs
+vs
] + 5*v
[0])>>3;
65 static void pack_nn_MMX(unsigned char *dst
, unsigned char *y
,
66 unsigned char *u
, unsigned char *v
, int w
)
72 "movq (%0), %%mm1 \n\t"
73 "movq (%0), %%mm2 \n\t"
74 "movq (%1), %%mm4 \n\t"
75 "movq (%2), %%mm6 \n\t"
76 "punpcklbw %%mm6, %%mm4 \n\t"
77 "punpcklbw %%mm4, %%mm1 \n\t"
78 "punpckhbw %%mm4, %%mm2 \n\t"
83 "movq %%mm1, (%3) \n\t"
84 "movq %%mm2, 8(%3) \n\t"
90 : "r" (y
), "r" (u
), "r" (v
), "r" (dst
), "r" (w
/8)
93 pack_nn_C(dst
, y
, u
, v
, (w
&7));
96 static void pack_li_0_MMX(unsigned char *dst
, unsigned char *y
,
97 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
100 "push %%"REG_BP
" \n\t"
102 "mov %6, %%"REG_BP
" \n\t"
104 "movl 4(%%"REG_d
"), %%"REG_BP
" \n\t"
105 "movl (%%"REG_d
"), %%"REG_d
" \n\t"
107 "pxor %%mm0, %%mm0 \n\t"
111 "movq (%%"REG_S
"), %%mm1 \n\t"
112 "movq (%%"REG_S
"), %%mm2 \n\t"
114 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
115 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
116 "punpcklbw %%mm0, %%mm4 \n\t"
117 "punpcklbw %%mm0, %%mm6 \n\t"
118 "movq (%%"REG_a
"), %%mm3 \n\t"
119 "movq (%%"REG_b
"), %%mm5 \n\t"
120 "punpcklbw %%mm0, %%mm3 \n\t"
121 "punpcklbw %%mm0, %%mm5 \n\t"
122 "paddw %%mm3, %%mm4 \n\t"
123 "paddw %%mm5, %%mm6 \n\t"
124 "paddw %%mm3, %%mm4 \n\t"
125 "paddw %%mm5, %%mm6 \n\t"
126 "paddw %%mm3, %%mm4 \n\t"
127 "paddw %%mm5, %%mm6 \n\t"
128 "paddw %%mm3, %%mm4 \n\t"
129 "paddw %%mm5, %%mm6 \n\t"
130 "paddw %%mm3, %%mm4 \n\t"
131 "paddw %%mm5, %%mm6 \n\t"
132 "paddw %%mm3, %%mm4 \n\t"
133 "paddw %%mm5, %%mm6 \n\t"
134 "paddw %%mm3, %%mm4 \n\t"
135 "paddw %%mm5, %%mm6 \n\t"
136 "psrlw $3, %%mm4 \n\t"
137 "psrlw $3, %%mm6 \n\t"
138 "packuswb %%mm4, %%mm4 \n\t"
139 "packuswb %%mm6, %%mm6 \n\t"
140 "punpcklbw %%mm6, %%mm4 \n\t"
141 "punpcklbw %%mm4, %%mm1 \n\t"
142 "punpckhbw %%mm4, %%mm2 \n\t"
144 "movq %%mm1, (%%"REG_D
") \n\t"
145 "movq %%mm2, 8(%%"REG_D
") \n\t"
147 "movq 8(%%"REG_S
"), %%mm1 \n\t"
148 "movq 8(%%"REG_S
"), %%mm2 \n\t"
150 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
151 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
152 "punpckhbw %%mm0, %%mm4 \n\t"
153 "punpckhbw %%mm0, %%mm6 \n\t"
154 "movq (%%"REG_a
"), %%mm3 \n\t"
155 "movq (%%"REG_b
"), %%mm5 \n\t"
156 "punpckhbw %%mm0, %%mm3 \n\t"
157 "punpckhbw %%mm0, %%mm5 \n\t"
158 "paddw %%mm3, %%mm4 \n\t"
159 "paddw %%mm5, %%mm6 \n\t"
160 "paddw %%mm3, %%mm4 \n\t"
161 "paddw %%mm5, %%mm6 \n\t"
162 "paddw %%mm3, %%mm4 \n\t"
163 "paddw %%mm5, %%mm6 \n\t"
164 "paddw %%mm3, %%mm4 \n\t"
165 "paddw %%mm5, %%mm6 \n\t"
166 "paddw %%mm3, %%mm4 \n\t"
167 "paddw %%mm5, %%mm6 \n\t"
168 "paddw %%mm3, %%mm4 \n\t"
169 "paddw %%mm5, %%mm6 \n\t"
170 "paddw %%mm3, %%mm4 \n\t"
171 "paddw %%mm5, %%mm6 \n\t"
172 "psrlw $3, %%mm4 \n\t"
173 "psrlw $3, %%mm6 \n\t"
174 "packuswb %%mm4, %%mm4 \n\t"
175 "packuswb %%mm6, %%mm6 \n\t"
176 "punpcklbw %%mm6, %%mm4 \n\t"
177 "punpcklbw %%mm4, %%mm1 \n\t"
178 "punpckhbw %%mm4, %%mm2 \n\t"
180 "add $16, %%"REG_S
" \n\t"
181 "add $8, %%"REG_a
" \n\t"
182 "add $8, %%"REG_b
" \n\t"
184 "movq %%mm1, 16(%%"REG_D
") \n\t"
185 "movq %%mm2, 24(%%"REG_D
") \n\t"
186 "add $32, %%"REG_D
" \n\t"
191 "pop %%"REG_BP
" \n\t"
193 : "S" (y
), "D" (dst
), "a" (u
), "b" (v
), "c" (w
/16),
195 "d" ((long)us
), "r" ((long)vs
)
201 pack_li_0_C(dst
, y
, u
, v
, (w
&15), us
, vs
);
204 static void pack_li_1_MMX(unsigned char *dst
, unsigned char *y
,
205 unsigned char *u
, unsigned char *v
, int w
, int us
, int vs
)
208 "push %%"REG_BP
" \n\t"
210 "mov %6, %%"REG_BP
" \n\t"
212 "movl 4(%%"REG_d
"), %%"REG_BP
" \n\t"
213 "movl (%%"REG_d
"), %%"REG_d
" \n\t"
215 "pxor %%mm0, %%mm0 \n\t"
219 "movq (%%"REG_S
"), %%mm1 \n\t"
220 "movq (%%"REG_S
"), %%mm2 \n\t"
222 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
223 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
224 "punpcklbw %%mm0, %%mm4 \n\t"
225 "punpcklbw %%mm0, %%mm6 \n\t"
226 "movq (%%"REG_a
"), %%mm3 \n\t"
227 "movq (%%"REG_b
"), %%mm5 \n\t"
228 "punpcklbw %%mm0, %%mm3 \n\t"
229 "punpcklbw %%mm0, %%mm5 \n\t"
230 "movq %%mm4, %%mm7 \n\t"
231 "paddw %%mm4, %%mm4 \n\t"
232 "paddw %%mm7, %%mm4 \n\t"
233 "movq %%mm6, %%mm7 \n\t"
234 "paddw %%mm6, %%mm6 \n\t"
235 "paddw %%mm7, %%mm6 \n\t"
236 "paddw %%mm3, %%mm4 \n\t"
237 "paddw %%mm5, %%mm6 \n\t"
238 "paddw %%mm3, %%mm4 \n\t"
239 "paddw %%mm5, %%mm6 \n\t"
240 "paddw %%mm3, %%mm4 \n\t"
241 "paddw %%mm5, %%mm6 \n\t"
242 "paddw %%mm3, %%mm4 \n\t"
243 "paddw %%mm5, %%mm6 \n\t"
244 "paddw %%mm3, %%mm4 \n\t"
245 "paddw %%mm5, %%mm6 \n\t"
246 "psrlw $3, %%mm4 \n\t"
247 "psrlw $3, %%mm6 \n\t"
248 "packuswb %%mm4, %%mm4 \n\t"
249 "packuswb %%mm6, %%mm6 \n\t"
250 "punpcklbw %%mm6, %%mm4 \n\t"
251 "punpcklbw %%mm4, %%mm1 \n\t"
252 "punpckhbw %%mm4, %%mm2 \n\t"
254 "movq %%mm1, (%%"REG_D
") \n\t"
255 "movq %%mm2, 8(%%"REG_D
") \n\t"
257 "movq 8(%%"REG_S
"), %%mm1 \n\t"
258 "movq 8(%%"REG_S
"), %%mm2 \n\t"
260 "movq (%%"REG_a
",%%"REG_d
",2), %%mm4 \n\t"
261 "movq (%%"REG_b
",%%"REG_BP
",2), %%mm6 \n\t"
262 "punpckhbw %%mm0, %%mm4 \n\t"
263 "punpckhbw %%mm0, %%mm6 \n\t"
264 "movq (%%"REG_a
"), %%mm3 \n\t"
265 "movq (%%"REG_b
"), %%mm5 \n\t"
266 "punpckhbw %%mm0, %%mm3 \n\t"
267 "punpckhbw %%mm0, %%mm5 \n\t"
268 "movq %%mm4, %%mm7 \n\t"
269 "paddw %%mm4, %%mm4 \n\t"
270 "paddw %%mm7, %%mm4 \n\t"
271 "movq %%mm6, %%mm7 \n\t"
272 "paddw %%mm6, %%mm6 \n\t"
273 "paddw %%mm7, %%mm6 \n\t"
274 "paddw %%mm3, %%mm4 \n\t"
275 "paddw %%mm5, %%mm6 \n\t"
276 "paddw %%mm3, %%mm4 \n\t"
277 "paddw %%mm5, %%mm6 \n\t"
278 "paddw %%mm3, %%mm4 \n\t"
279 "paddw %%mm5, %%mm6 \n\t"
280 "paddw %%mm3, %%mm4 \n\t"
281 "paddw %%mm5, %%mm6 \n\t"
282 "paddw %%mm3, %%mm4 \n\t"
283 "paddw %%mm5, %%mm6 \n\t"
284 "psrlw $3, %%mm4 \n\t"
285 "psrlw $3, %%mm6 \n\t"
286 "packuswb %%mm4, %%mm4 \n\t"
287 "packuswb %%mm6, %%mm6 \n\t"
288 "punpcklbw %%mm6, %%mm4 \n\t"
289 "punpcklbw %%mm4, %%mm1 \n\t"
290 "punpckhbw %%mm4, %%mm2 \n\t"
292 "add $16, %%"REG_S
" \n\t"
293 "add $8, %%"REG_a
" \n\t"
294 "add $8, %%"REG_b
" \n\t"
296 "movq %%mm1, 16(%%"REG_D
") \n\t"
297 "movq %%mm2, 24(%%"REG_D
") \n\t"
298 "add $32, %%"REG_D
" \n\t"
303 "pop %%"REG_BP
" \n\t"
305 : "S" (y
), "D" (dst
), "a" (u
), "b" (v
), "c" (w
/16),
307 "d" ((long)us
), "r" ((long)vs
)
313 pack_li_1_C(dst
, y
, u
, v
, (w
&15), us
, vs
);
317 static pack_func_t
*pack_nn
;
318 static pack_func_t
*pack_li_0
;
319 static pack_func_t
*pack_li_1
;
321 static void ilpack(unsigned char *dst
, unsigned char *src
[3],
322 int dststride
, int srcstride
[3], int w
, int h
, pack_func_t
*pack
[2])
325 unsigned char *y
, *u
, *v
;
326 int ys
= srcstride
[0], us
= srcstride
[1], vs
= srcstride
[2];
333 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
334 y
+= ys
; dst
+= dststride
;
335 pack_nn(dst
, y
, u
+us
, v
+vs
, w
, 0, 0);
336 y
+= ys
; dst
+= dststride
;
337 for (i
=2; i
<h
-2; i
++) {
339 b
= (i
&1) ^ ((i
&2)>>1);
340 pack
[b
](dst
, y
, u
, v
, w
, us
*a
, vs
*a
);
351 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
352 y
+= ys
; dst
+= dststride
; u
+= us
; v
+= vs
;
353 pack_nn(dst
, y
, u
, v
, w
, 0, 0);
357 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
)
361 // hope we'll get DR buffer:
362 dmpi
=vf_get_image(vf
->next
, IMGFMT_YUY2
,
363 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
366 ilpack(dmpi
->planes
[0], mpi
->planes
, dmpi
->stride
[0], mpi
->stride
, mpi
->w
, mpi
->h
, vf
->priv
->pack
);
368 return vf_next_put_image(vf
,dmpi
, pts
);
371 static int config(struct vf_instance_s
* vf
,
372 int width
, int height
, int d_width
, int d_height
,
373 unsigned int flags
, unsigned int outfmt
)
375 /* FIXME - also support UYVY output? */
376 return vf_next_config(vf
, width
, height
, d_width
, d_height
, flags
, IMGFMT_YUY2
);
380 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
)
382 /* FIXME - really any YUV 4:2:0 input format should work */
387 return vf_next_query_format(vf
,IMGFMT_YUY2
);
392 static int open(vf_instance_t
*vf
, char* args
)
395 vf
->query_format
=query_format
;
396 vf
->put_image
=put_image
;
397 vf
->priv
= calloc(1, sizeof(struct vf_priv_s
));
399 if (args
) sscanf(args
, "%d", &vf
->priv
->mode
);
401 pack_nn
= (pack_func_t
*)pack_nn_C
;
402 pack_li_0
= pack_li_0_C
;
403 pack_li_1
= pack_li_1_C
;
405 if(gCpuCaps
.hasMMX
) {
406 pack_nn
= (pack_func_t
*)pack_nn_MMX
;
407 pack_li_0
= pack_li_0_MMX
;
408 pack_li_1
= pack_li_1_MMX
;
412 switch(vf
->priv
->mode
) {
414 vf
->priv
->pack
[0] = vf
->priv
->pack
[1] = pack_nn
;
417 mp_msg(MSGT_VFILTER
, MSGL_WARN
,
418 "ilpack: unknown mode %d (fallback to linear)\n",
421 vf
->priv
->pack
[0] = pack_li_0
;
422 vf
->priv
->pack
[1] = pack_li_1
;
429 vf_info_t vf_info_ilpack
= {
430 "4:2:0 planar -> 4:2:2 packed reinterlacer",