Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_tfields.c
blobb95b4f83ef9d3b6258d737f8da7415d65312b339
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "config.h"
6 #include "mp_msg.h"
7 #include "cpudetect.h"
9 #include "img_format.h"
10 #include "mp_image.h"
11 #include "vf.h"
13 #include "libvo/fastmemcpy.h"
15 struct vf_priv_s {
16 int mode;
17 int parity;
18 int buffered_i;
19 mp_image_t *buffered_mpi;
20 double buffered_pts;
23 static void deint(unsigned char *dest, int ds, unsigned char *src, int ss, int w, int h, int field)
25 int x, y;
26 src += ss;
27 dest += ds;
28 if (field) {
29 src += ss;
30 dest += ds;
31 h -= 2;
33 for (y=h/2; y; y--) {
34 for (x=0; x<w; x++) {
35 if (((src[x-ss] < src[x]) && (src[x+ss] < src[x])) ||
36 ((src[x-ss] > src[x]) && (src[x+ss] > src[x]))) {
37 //dest[x] = (src[x+ss] + src[x-ss])>>1;
38 dest[x] = ((src[x+ss]<<1) + (src[x-ss]<<1)
39 + src[x+ss+1] + src[x-ss+1]
40 + src[x+ss-1] + src[x-ss-1])>>3;
42 else dest[x] = src[x];
44 dest += ds<<1;
45 src += ss<<1;
49 #ifdef HAVE_3DNOW
50 static void qpel_li_3DNOW(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
52 int i, j, ssd=ss;
53 long crap1, crap2;
54 if (up) {
55 ssd = -ss;
56 fast_memcpy(d, s, w);
57 d += ds;
58 s += ss;
60 for (i=h-1; i; i--) {
61 asm volatile(
62 "1: \n\t"
63 "movq (%%"REG_S"), %%mm0 \n\t"
64 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
65 "pavgusb %%mm0, %%mm1 \n\t"
66 "add $8, %%"REG_S" \n\t"
67 "pavgusb %%mm0, %%mm1 \n\t"
68 "movq %%mm1, (%%"REG_D") \n\t"
69 "add $8, %%"REG_D" \n\t"
70 "decl %%ecx \n\t"
71 "jnz 1b \n\t"
72 : "=S"(crap1), "=D"(crap2)
73 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
75 for (j=w-(w&7); j<w; j++)
76 d[j] = (s[j+ssd] + 3*s[j])>>2;
77 d += ds;
78 s += ss;
80 if (!up) fast_memcpy(d, s, w);
81 asm volatile("emms \n\t" : : : "memory");
83 #endif
85 #ifdef HAVE_MMX2
86 static void qpel_li_MMX2(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
88 int i, j, ssd=ss;
89 long crap1, crap2;
90 if (up) {
91 ssd = -ss;
92 fast_memcpy(d, s, w);
93 d += ds;
94 s += ss;
96 for (i=h-1; i; i--) {
97 asm volatile(
98 "pxor %%mm7, %%mm7 \n\t"
99 "2: \n\t"
100 "movq (%%"REG_S"), %%mm0 \n\t"
101 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
102 "pavgb %%mm0, %%mm1 \n\t"
103 "add $8, %%"REG_S" \n\t"
104 "pavgb %%mm0, %%mm1 \n\t"
105 "movq %%mm1, (%%"REG_D") \n\t"
106 "add $8, %%"REG_D" \n\t"
107 "decl %%ecx \n\t"
108 "jnz 2b \n\t"
109 : "=S"(crap1), "=D"(crap2)
110 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
112 for (j=w-(w&7); j<w; j++)
113 d[j] = (s[j+ssd] + 3*s[j])>>2;
114 d += ds;
115 s += ss;
117 if (!up) fast_memcpy(d, s, w);
118 asm volatile("emms \n\t" : : : "memory");
120 #endif
122 #ifdef HAVE_MMX
123 static void qpel_li_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
125 int i, j, ssd=ss;
126 int crap1, crap2;
127 if (up) {
128 ssd = -ss;
129 fast_memcpy(d, s, w);
130 d += ds;
131 s += ss;
133 for (i=h-1; i; i--) {
134 asm volatile(
135 "pxor %%mm7, %%mm7 \n\t"
136 "3: \n\t"
137 "movq (%%"REG_S"), %%mm0 \n\t"
138 "movq (%%"REG_S"), %%mm1 \n\t"
139 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
140 "movq (%%"REG_S",%%"REG_a"), %%mm3 \n\t"
141 "add $8, %%"REG_S" \n\t"
142 "punpcklbw %%mm7, %%mm0 \n\t"
143 "punpckhbw %%mm7, %%mm1 \n\t"
144 "punpcklbw %%mm7, %%mm2 \n\t"
145 "punpckhbw %%mm7, %%mm3 \n\t"
146 "paddw %%mm0, %%mm2 \n\t"
147 "paddw %%mm1, %%mm3 \n\t"
148 "paddw %%mm0, %%mm2 \n\t"
149 "paddw %%mm1, %%mm3 \n\t"
150 "paddw %%mm0, %%mm2 \n\t"
151 "paddw %%mm1, %%mm3 \n\t"
152 "psrlw $2, %%mm2 \n\t"
153 "psrlw $2, %%mm3 \n\t"
154 "packsswb %%mm3, %%mm2 \n\t"
155 "movq %%mm2, (%%"REG_D") \n\t"
156 "add $8, %%"REG_D" \n\t"
157 "decl %%ecx \n\t"
158 "jnz 3b \n\t"
159 : "=S"(crap1), "=D"(crap2)
160 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
162 for (j=w-(w&7); j<w; j++)
163 d[j] = (s[j+ssd] + 3*s[j])>>2;
164 d += ds;
165 s += ss;
167 if (!up) fast_memcpy(d, s, w);
168 asm volatile("emms \n\t" : : : "memory");
171 static void qpel_4tap_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
173 int i, j, ssd=ss;
174 static const short filter[] = {
175 29, 29, 29, 29, 110, 110, 110, 110,
176 9, 9, 9, 9, 3, 3, 3, 3,
177 64, 64, 64, 64 };
178 int crap1, crap2;
179 if (up) {
180 ssd = -ss;
181 fast_memcpy(d, s, w);
182 d += ds; s += ss;
184 for (j=0; j<w; j++)
185 d[j] = (s[j+ssd] + 3*s[j])>>2;
186 d += ds; s += ss;
187 for (i=h-3; i; i--) {
188 asm volatile(
189 "pxor %%mm0, %%mm0 \n\t"
190 "movq (%%"REG_d"), %%mm4 \n\t"
191 "movq 8(%%"REG_d"), %%mm5 \n\t"
192 "movq 16(%%"REG_d"), %%mm6 \n\t"
193 "movq 24(%%"REG_d"), %%mm7 \n\t"
194 "4: \n\t"
196 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
197 "movq (%%"REG_S"), %%mm2 \n\t"
198 "movq (%%"REG_S",%%"REG_b"), %%mm3 \n\t"
199 "punpcklbw %%mm0, %%mm1 \n\t"
200 "punpcklbw %%mm0, %%mm2 \n\t"
201 "pmullw %%mm4, %%mm1 \n\t"
202 "punpcklbw %%mm0, %%mm3 \n\t"
203 "pmullw %%mm5, %%mm2 \n\t"
204 "paddusw %%mm2, %%mm1 \n\t"
205 "pmullw %%mm6, %%mm3 \n\t"
206 "movq (%%"REG_S",%%"REG_a",2), %%mm2 \n\t"
207 "psubusw %%mm3, %%mm1 \n\t"
208 "punpcklbw %%mm0, %%mm2 \n\t"
209 "pmullw %%mm7, %%mm2 \n\t"
210 "psubusw %%mm2, %%mm1 \n\t"
211 "psrlw $7, %%mm1 \n\t"
213 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
214 "movq (%%"REG_S"), %%mm3 \n\t"
215 "punpckhbw %%mm0, %%mm2 \n\t"
216 "punpckhbw %%mm0, %%mm3 \n\t"
217 "pmullw %%mm4, %%mm2 \n\t"
218 "pmullw %%mm5, %%mm3 \n\t"
219 "paddusw %%mm3, %%mm2 \n\t"
220 "movq (%%"REG_S",%%"REG_b"), %%mm3 \n\t"
221 "punpckhbw %%mm0, %%mm3 \n\t"
222 "pmullw %%mm6, %%mm3 \n\t"
223 "psubusw %%mm3, %%mm2 \n\t"
224 "movq (%%"REG_S",%%"REG_a",2), %%mm3 \n\t"
225 "punpckhbw %%mm0, %%mm3 \n\t"
226 "add $8, %%"REG_S" \n\t"
227 "pmullw %%mm7, %%mm3 \n\t"
228 "psubusw %%mm3, %%mm2 \n\t"
229 "psrlw $7, %%mm2 \n\t"
231 "packuswb %%mm2, %%mm1 \n\t"
232 "movq %%mm1, (%%"REG_D") \n\t"
233 "add $8, %%"REG_D" \n\t"
234 "decl %%ecx \n\t"
235 "jnz 4b \n\t"
236 : "=S"(crap1), "=D"(crap2)
237 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd), "b"((long)-ssd), "d"(filter)
239 for (j=w-(w&7); j<w; j++)
240 d[j] = (-9*s[j-ssd] + 111*s[j] + 29*s[j+ssd] - 3*s[j+ssd+ssd])>>7;
241 d += ds;
242 s += ss;
244 for (j=0; j<w; j++)
245 d[j] = (s[j+ssd] + 3*s[j])>>2;
246 d += ds; s += ss;
247 if (!up) fast_memcpy(d, s, w);
248 asm volatile("emms \n\t" : : : "memory");
250 #endif
252 static inline int clamp(int a)
254 // If a<512, this is equivalent to:
255 // return (a<0) ? 0 : ( (a>255) ? 255 : a);
256 return (~(a>>31)) & (a | ((a<<23)>>31));
259 static void qpel_li_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
261 int i, j, ssd=ss;
262 if (up) {
263 ssd = -ss;
264 fast_memcpy(d, s, w);
265 d += ds;
266 s += ss;
268 for (i=h-1; i; i--) {
269 for (j=0; j<w; j++)
270 d[j] = (s[j+ssd] + 3*s[j])>>2;
271 d += ds;
272 s += ss;
274 if (!up) fast_memcpy(d, s, w);
277 static void qpel_4tap_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
279 int i, j, ssd=ss;
280 if (up) {
281 ssd = -ss;
282 fast_memcpy(d, s, w);
283 d += ds; s += ss;
285 for (j=0; j<w; j++)
286 d[j] = (s[j+ssd] + 3*s[j] + 2)>>2;
287 d += ds; s += ss;
288 for (i=h-3; i; i--) {
289 for (j=0; j<w; j++)
290 d[j] = clamp((-9*s[j-ssd] + 111*s[j] + 29*s[j+ssd] - 3*s[j+ssd+ssd] + 64)>>7);
291 d += ds; s += ss;
293 for (j=0; j<w; j++)
294 d[j] = (s[j+ssd] + 3*s[j] + 2)>>2;
295 d += ds; s += ss;
296 if (!up) fast_memcpy(d, s, w);
299 static void (*qpel_li)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
300 static void (*qpel_4tap)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
302 static int continue_buffered_image(struct vf_instance_s *);
303 extern int correct_pts;
305 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
307 vf->priv->buffered_mpi = mpi;
308 vf->priv->buffered_pts = pts;
309 vf->priv->buffered_i = 0;
310 return continue_buffered_image(vf);
313 static int continue_buffered_image(struct vf_instance_s *vf)
315 int i=vf->priv->buffered_i;
316 double pts = vf->priv->buffered_pts;
317 mp_image_t *mpi = vf->priv->buffered_mpi;
318 int ret=0;
319 mp_image_t *dmpi;
320 void (*qpel)(unsigned char *, unsigned char *, int, int, int, int, int);
321 int bpp=1;
322 int tff;
324 if (i == 0)
325 vf_queue_frame(vf, continue_buffered_image);
326 pts += i * .02; // XXX not right
328 if (!(mpi->flags & MP_IMGFLAG_PLANAR)) bpp = mpi->bpp/8;
329 if (vf->priv->parity < 0) {
330 if (mpi->fields & MP_IMGFIELD_ORDERED)
331 tff = mpi->fields & MP_IMGFIELD_TOP_FIRST;
332 else
333 tff = 1;
335 else tff = (vf->priv->parity&1)^1;
337 switch (vf->priv->mode) {
338 case 2:
339 qpel = qpel_li;
340 break;
341 case 3:
342 // TODO: add 3tap filter
343 qpel = qpel_4tap;
344 break;
345 case 4:
346 qpel = qpel_4tap;
347 break;
350 switch (vf->priv->mode) {
351 case 0:
352 for (; i<2; i++) {
353 dmpi = vf_get_image(vf->next, mpi->imgfmt,
354 MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE,
355 mpi->width, mpi->height/2);
356 dmpi->planes[0] = mpi->planes[0] + (i^!tff)*mpi->stride[0];
357 dmpi->stride[0] = 2*mpi->stride[0];
358 if (mpi->flags & MP_IMGFLAG_PLANAR) {
359 dmpi->planes[1] = mpi->planes[1] + (i^!tff)*mpi->stride[1];
360 dmpi->planes[2] = mpi->planes[2] + (i^!tff)*mpi->stride[2];
361 dmpi->stride[1] = 2*mpi->stride[1];
362 dmpi->stride[2] = 2*mpi->stride[2];
364 ret |= vf_next_put_image(vf, dmpi, pts);
365 if (correct_pts)
366 break;
367 else
368 if (!i) vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
370 break;
371 case 1:
372 for (; i<2; i++) {
373 dmpi = vf_get_image(vf->next, mpi->imgfmt,
374 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
375 mpi->width, mpi->height);
376 my_memcpy_pic(dmpi->planes[0] + (i^!tff)*dmpi->stride[0],
377 mpi->planes[0] + (i^!tff)*mpi->stride[0],
378 mpi->w*bpp, mpi->h/2, dmpi->stride[0]*2, mpi->stride[0]*2);
379 deint(dmpi->planes[0], dmpi->stride[0], mpi->planes[0], mpi->stride[0], mpi->w, mpi->h, (i^!tff));
380 if (mpi->flags & MP_IMGFLAG_PLANAR) {
381 my_memcpy_pic(dmpi->planes[1] + (i^!tff)*dmpi->stride[1],
382 mpi->planes[1] + (i^!tff)*mpi->stride[1],
383 mpi->chroma_width, mpi->chroma_height/2,
384 dmpi->stride[1]*2, mpi->stride[1]*2);
385 my_memcpy_pic(dmpi->planes[2] + (i^!tff)*dmpi->stride[2],
386 mpi->planes[2] + (i^!tff)*mpi->stride[2],
387 mpi->chroma_width, mpi->chroma_height/2,
388 dmpi->stride[2]*2, mpi->stride[2]*2);
389 deint(dmpi->planes[1], dmpi->stride[1], mpi->planes[1], mpi->stride[1],
390 mpi->chroma_width, mpi->chroma_height, (i^!tff));
391 deint(dmpi->planes[2], dmpi->stride[2], mpi->planes[2], mpi->stride[2],
392 mpi->chroma_width, mpi->chroma_height, (i^!tff));
394 ret |= vf_next_put_image(vf, dmpi, pts);
395 if (correct_pts)
396 break;
397 else
398 if (!i) vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
400 break;
401 case 2:
402 case 3:
403 case 4:
404 for (; i<2; i++) {
405 dmpi = vf_get_image(vf->next, mpi->imgfmt,
406 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
407 mpi->width, mpi->height/2);
408 qpel(dmpi->planes[0], mpi->planes[0] + (i^!tff)*mpi->stride[0],
409 mpi->w*bpp, mpi->h/2, dmpi->stride[0], mpi->stride[0]*2, (i^!tff));
410 if (mpi->flags & MP_IMGFLAG_PLANAR) {
411 qpel(dmpi->planes[1],
412 mpi->planes[1] + (i^!tff)*mpi->stride[1],
413 mpi->chroma_width, mpi->chroma_height/2,
414 dmpi->stride[1], mpi->stride[1]*2, (i^!tff));
415 qpel(dmpi->planes[2],
416 mpi->planes[2] + (i^!tff)*mpi->stride[2],
417 mpi->chroma_width, mpi->chroma_height/2,
418 dmpi->stride[2], mpi->stride[2]*2, (i^!tff));
420 ret |= vf_next_put_image(vf, dmpi, pts);
421 if (correct_pts)
422 break;
423 else
424 if (!i) vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
426 break;
428 vf->priv->buffered_i = 1;
429 return ret;
432 #if 0
433 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
435 /* FIXME - figure out which other formats work */
436 switch (fmt) {
437 case IMGFMT_YV12:
438 case IMGFMT_IYUV:
439 case IMGFMT_I420:
440 return vf_next_query_format(vf, fmt);
442 return 0;
444 #endif
446 static int config(struct vf_instance_s* vf,
447 int width, int height, int d_width, int d_height,
448 unsigned int flags, unsigned int outfmt)
450 switch (vf->priv->mode) {
451 case 0:
452 case 2:
453 case 3:
454 case 4:
455 return vf_next_config(vf,width,height/2,d_width,d_height,flags,outfmt);
456 case 1:
457 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
459 return 0;
462 static void uninit(struct vf_instance_s* vf)
464 free(vf->priv);
467 static int open(vf_instance_t *vf, char* args)
469 struct vf_priv_s *p;
470 vf->config = config;
471 vf->put_image = put_image;
472 //vf->query_format = query_format;
473 vf->uninit = uninit;
474 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
475 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
476 vf->priv->mode = 4;
477 vf->priv->parity = -1;
478 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
479 qpel_li = qpel_li_C;
480 qpel_4tap = qpel_4tap_C;
481 #ifdef HAVE_MMX
482 if(gCpuCaps.hasMMX) qpel_li = qpel_li_MMX;
483 if(gCpuCaps.hasMMX) qpel_4tap = qpel_4tap_MMX;
484 #endif
485 #ifdef HAVE_MMX2
486 if(gCpuCaps.hasMMX2) qpel_li = qpel_li_MMX2;
487 #endif
488 #ifdef HAVE_3DNOW
489 if(gCpuCaps.has3DNow) qpel_li = qpel_li_3DNOW;
490 #endif
491 return 1;
494 const vf_info_t vf_info_tfields = {
495 "temporal field separation",
496 "tfields",
497 "Rich Felker",
499 open,
500 NULL