typo fixes
[mplayer/greg.git] / libmpcodecs / vf_ilpack.c
blob7eb65c581819ce43010acba684c097e847ad18fb
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "cpudetect.h"
9 #include "asmalign.h"
11 #include "img_format.h"
12 #include "mp_image.h"
13 #include "vf.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);
21 struct vf_priv_s {
22 int mode;
23 pack_func_t *pack[2];
26 static void pack_nn_C(unsigned char *dst, unsigned char *y,
27 unsigned char *u, unsigned char *v, int w)
29 int j;
30 for (j = w/2; j; j--) {
31 *dst++ = *y++;
32 *dst++ = *u++;
33 *dst++ = *y++;
34 *dst++ = *v++;
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)
41 int j;
42 for (j = w/2; j; j--) {
43 *dst++ = *y++;
44 *dst++ = (u[us+us] + 7*u[0])>>3;
45 *dst++ = *y++;
46 *dst++ = (v[vs+vs] + 7*v[0])>>3;
47 u++; v++;
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)
54 int j;
55 for (j = w/2; j; j--) {
56 *dst++ = *y++;
57 *dst++ = (3*u[us+us] + 5*u[0])>>3;
58 *dst++ = *y++;
59 *dst++ = (3*v[vs+vs] + 5*v[0])>>3;
60 u++; v++;
64 #ifdef HAVE_MMX
65 static void pack_nn_MMX(unsigned char *dst, unsigned char *y,
66 unsigned char *u, unsigned char *v, int w)
68 int j;
69 asm volatile (""
70 ASMALIGN16
71 "1: \n\t"
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"
80 "add $8, %0 \n\t"
81 "add $4, %1 \n\t"
82 "add $4, %2 \n\t"
83 "movq %%mm1, (%3) \n\t"
84 "movq %%mm2, 8(%3) \n\t"
85 "add $16, %3 \n\t"
86 "decl %4 \n\t"
87 "jnz 1b \n\t"
88 "emms \n\t"
90 : "r" (y), "r" (u), "r" (v), "r" (dst), "r" (w/8)
91 : "memory"
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)
99 asm volatile (""
100 "push %%"REG_BP" \n\t"
101 #ifdef ARCH_X86_64
102 "mov %6, %%"REG_BP" \n\t"
103 #else
104 "movl 4(%%"REG_d"), %%"REG_BP" \n\t"
105 "movl (%%"REG_d"), %%"REG_d" \n\t"
106 #endif
107 "pxor %%mm0, %%mm0 \n\t"
109 ASMALIGN16
110 ".Lli0: \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"
188 "decl %%ecx \n\t"
189 "jnz .Lli0 \n\t"
190 "emms \n\t"
191 "pop %%"REG_BP" \n\t"
193 : "S" (y), "D" (dst), "a" (u), "b" (v), "c" (w/16),
194 #ifdef ARCH_X86_64
195 "d" ((long)us), "r" ((long)vs)
196 #else
197 "d" (&us)
198 #endif
199 : "memory"
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)
207 asm volatile (""
208 "push %%"REG_BP" \n\t"
209 #ifdef ARCH_X86_64
210 "mov %6, %%"REG_BP" \n\t"
211 #else
212 "movl 4(%%"REG_d"), %%"REG_BP" \n\t"
213 "movl (%%"REG_d"), %%"REG_d" \n\t"
214 #endif
215 "pxor %%mm0, %%mm0 \n\t"
217 ASMALIGN16
218 ".Lli1: \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"
300 "decl %%ecx \n\t"
301 "jnz .Lli1 \n\t"
302 "emms \n\t"
303 "pop %%"REG_BP" \n\t"
305 : "S" (y), "D" (dst), "a" (u), "b" (v), "c" (w/16),
306 #ifdef ARCH_X86_64
307 "d" ((long)us), "r" ((long)vs)
308 #else
309 "d" (&us)
310 #endif
311 : "memory"
313 pack_li_1_C(dst, y, u, v, (w&15), us, vs);
315 #endif
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])
324 int i;
325 unsigned char *y, *u, *v;
326 int ys = srcstride[0], us = srcstride[1], vs = srcstride[2];
327 int a, b;
329 y = src[0];
330 u = src[1];
331 v = src[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++) {
338 a = (i&2) ? 1 : -1;
339 b = (i&1) ^ ((i&2)>>1);
340 pack[b](dst, y, u, v, w, us*a, vs*a);
341 y += ys;
342 if ((i&3) == 1) {
343 u -= us;
344 v -= vs;
345 } else {
346 u += us;
347 v += vs;
349 dst += dststride;
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)
359 mp_image_t *dmpi;
361 // hope we'll get DR buffer:
362 dmpi=vf_get_image(vf->next, IMGFMT_YUY2,
363 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
364 mpi->w, mpi->h);
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 */
383 switch (fmt) {
384 case IMGFMT_YV12:
385 case IMGFMT_IYUV:
386 case IMGFMT_I420:
387 return vf_next_query_format(vf,IMGFMT_YUY2);
389 return 0;
392 static int open(vf_instance_t *vf, char* args)
394 vf->config=config;
395 vf->query_format=query_format;
396 vf->put_image=put_image;
397 vf->priv = calloc(1, sizeof(struct vf_priv_s));
398 vf->priv->mode = 1;
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;
404 #ifdef HAVE_MMX
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;
410 #endif
412 switch(vf->priv->mode) {
413 case 0:
414 vf->priv->pack[0] = vf->priv->pack[1] = pack_nn;
415 break;
416 default:
417 mp_msg(MSGT_VFILTER, MSGL_WARN,
418 "ilpack: unknown mode %d (fallback to linear)\n",
419 vf->priv->mode);
420 case 1:
421 vf->priv->pack[0] = pack_li_0;
422 vf->priv->pack[1] = pack_li_1;
423 break;
426 return 1;
429 vf_info_t vf_info_ilpack = {
430 "4:2:0 planar -> 4:2:2 packed reinterlacer",
431 "ilpack",
432 "Richard Felker",
434 open,
435 NULL