configure: Fix compilation on Windows: MinGW unistd.h does not define NULL
[mplayer/glamo.git] / libmpcodecs / vf_tfields.c
blobee50d4f6ca24bf76ee01069cc36c74febb7e1768
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "config.h"
24 #include "options.h"
25 #include "mp_msg.h"
26 #include "cpudetect.h"
28 #include "img_format.h"
29 #include "mp_image.h"
30 #include "vf.h"
32 #include "libvo/fastmemcpy.h"
34 struct vf_priv_s {
35 int mode;
36 int parity;
37 int buffered_i;
38 mp_image_t *buffered_mpi;
39 double buffered_pts;
42 static void deint(unsigned char *dest, int ds, unsigned char *src, int ss, int w, int h, int field)
44 int x, y;
45 src += ss;
46 dest += ds;
47 h--;
48 if (field) {
49 fast_memcpy(dest - ds, src - ss, w);
50 src += ss;
51 dest += ds;
52 h--;
54 for (y=h/2; y > 0; y--) {
55 dest[0] = src[0];
56 for (x=1; x<w-1; x++) {
57 if (((src[x-ss] < src[x]) && (src[x+ss] < src[x])) ||
58 ((src[x-ss] > src[x]) && (src[x+ss] > src[x]))) {
59 //dest[x] = (src[x+ss] + src[x-ss])>>1;
60 dest[x] = ((src[x+ss]<<1) + (src[x-ss]<<1)
61 + src[x+ss+1] + src[x-ss+1]
62 + src[x+ss-1] + src[x-ss-1])>>3;
64 else dest[x] = src[x];
66 dest[w-1] = src[w-1];
67 dest += ds<<1;
68 src += ss<<1;
70 if (h & 1)
71 fast_memcpy(dest, src, w);
74 #if HAVE_AMD3DNOW
75 static void qpel_li_3DNOW(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
77 int i, j, ssd=ss;
78 long crap1, crap2;
79 if (up) {
80 ssd = -ss;
81 fast_memcpy(d, s, w);
82 d += ds;
83 s += ss;
85 for (i=h-1; i; i--) {
86 __asm__ volatile(
87 "1: \n\t"
88 "movq (%%"REG_S"), %%mm0 \n\t"
89 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
90 "pavgusb %%mm0, %%mm1 \n\t"
91 "add $8, %%"REG_S" \n\t"
92 "pavgusb %%mm0, %%mm1 \n\t"
93 "movq %%mm1, (%%"REG_D") \n\t"
94 "add $8, %%"REG_D" \n\t"
95 "decl %%ecx \n\t"
96 "jnz 1b \n\t"
97 : "=S"(crap1), "=D"(crap2)
98 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
100 for (j=w-(w&7); j<w; j++)
101 d[j] = (s[j+ssd] + 3*s[j])>>2;
102 d += ds;
103 s += ss;
105 if (!up) fast_memcpy(d, s, w);
106 __asm__ volatile("emms \n\t" : : : "memory");
108 #endif
110 #if HAVE_MMX2
111 static void qpel_li_MMX2(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
113 int i, j, ssd=ss;
114 long crap1, crap2;
115 if (up) {
116 ssd = -ss;
117 fast_memcpy(d, s, w);
118 d += ds;
119 s += ss;
121 for (i=h-1; i; i--) {
122 __asm__ volatile(
123 "pxor %%mm7, %%mm7 \n\t"
124 "2: \n\t"
125 "movq (%%"REG_S"), %%mm0 \n\t"
126 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
127 "pavgb %%mm0, %%mm1 \n\t"
128 "add $8, %%"REG_S" \n\t"
129 "pavgb %%mm0, %%mm1 \n\t"
130 "movq %%mm1, (%%"REG_D") \n\t"
131 "add $8, %%"REG_D" \n\t"
132 "decl %%ecx \n\t"
133 "jnz 2b \n\t"
134 : "=S"(crap1), "=D"(crap2)
135 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
137 for (j=w-(w&7); j<w; j++)
138 d[j] = (s[j+ssd] + 3*s[j])>>2;
139 d += ds;
140 s += ss;
142 if (!up) fast_memcpy(d, s, w);
143 __asm__ volatile("emms \n\t" : : : "memory");
145 #endif
147 #if HAVE_MMX
148 static void qpel_li_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
150 int i, j, ssd=ss;
151 int crap1, crap2;
152 if (up) {
153 ssd = -ss;
154 fast_memcpy(d, s, w);
155 d += ds;
156 s += ss;
158 for (i=h-1; i; i--) {
159 __asm__ volatile(
160 "pxor %%mm7, %%mm7 \n\t"
161 "3: \n\t"
162 "movq (%%"REG_S"), %%mm0 \n\t"
163 "movq (%%"REG_S"), %%mm1 \n\t"
164 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
165 "movq (%%"REG_S",%%"REG_a"), %%mm3 \n\t"
166 "add $8, %%"REG_S" \n\t"
167 "punpcklbw %%mm7, %%mm0 \n\t"
168 "punpckhbw %%mm7, %%mm1 \n\t"
169 "punpcklbw %%mm7, %%mm2 \n\t"
170 "punpckhbw %%mm7, %%mm3 \n\t"
171 "paddw %%mm0, %%mm2 \n\t"
172 "paddw %%mm1, %%mm3 \n\t"
173 "paddw %%mm0, %%mm2 \n\t"
174 "paddw %%mm1, %%mm3 \n\t"
175 "paddw %%mm0, %%mm2 \n\t"
176 "paddw %%mm1, %%mm3 \n\t"
177 "psrlw $2, %%mm2 \n\t"
178 "psrlw $2, %%mm3 \n\t"
179 "packsswb %%mm3, %%mm2 \n\t"
180 "movq %%mm2, (%%"REG_D") \n\t"
181 "add $8, %%"REG_D" \n\t"
182 "decl %%ecx \n\t"
183 "jnz 3b \n\t"
184 : "=S"(crap1), "=D"(crap2)
185 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
187 for (j=w-(w&7); j<w; j++)
188 d[j] = (s[j+ssd] + 3*s[j])>>2;
189 d += ds;
190 s += ss;
192 if (!up) fast_memcpy(d, s, w);
193 __asm__ volatile("emms \n\t" : : : "memory");
196 #if HAVE_EBX_AVAILABLE
197 static void qpel_4tap_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
199 int i, j, ssd=ss;
200 static const short filter[] = {
201 29, 29, 29, 29, 110, 110, 110, 110,
202 9, 9, 9, 9, 3, 3, 3, 3,
203 64, 64, 64, 64 };
204 int crap1, crap2;
205 if (up) {
206 ssd = -ss;
207 fast_memcpy(d, s, w);
208 d += ds; s += ss;
210 for (j=0; j<w; j++)
211 d[j] = (s[j+ssd] + 3*s[j])>>2;
212 d += ds; s += ss;
213 for (i=h-3; i; i--) {
214 __asm__ volatile(
215 "pxor %%mm0, %%mm0 \n\t"
216 "movq (%%"REG_d"), %%mm4 \n\t"
217 "movq 8(%%"REG_d"), %%mm5 \n\t"
218 "movq 16(%%"REG_d"), %%mm6 \n\t"
219 "movq 24(%%"REG_d"), %%mm7 \n\t"
220 "4: \n\t"
222 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
223 "movq (%%"REG_S"), %%mm2 \n\t"
224 "movq (%%"REG_S",%%"REG_b"), %%mm3 \n\t"
225 "punpcklbw %%mm0, %%mm1 \n\t"
226 "punpcklbw %%mm0, %%mm2 \n\t"
227 "pmullw %%mm4, %%mm1 \n\t"
228 "punpcklbw %%mm0, %%mm3 \n\t"
229 "pmullw %%mm5, %%mm2 \n\t"
230 "paddusw %%mm2, %%mm1 \n\t"
231 "pmullw %%mm6, %%mm3 \n\t"
232 "movq (%%"REG_S",%%"REG_a",2), %%mm2 \n\t"
233 "psubusw %%mm3, %%mm1 \n\t"
234 "punpcklbw %%mm0, %%mm2 \n\t"
235 "pmullw %%mm7, %%mm2 \n\t"
236 "psubusw %%mm2, %%mm1 \n\t"
237 "psrlw $7, %%mm1 \n\t"
239 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
240 "movq (%%"REG_S"), %%mm3 \n\t"
241 "punpckhbw %%mm0, %%mm2 \n\t"
242 "punpckhbw %%mm0, %%mm3 \n\t"
243 "pmullw %%mm4, %%mm2 \n\t"
244 "pmullw %%mm5, %%mm3 \n\t"
245 "paddusw %%mm3, %%mm2 \n\t"
246 "movq (%%"REG_S",%%"REG_b"), %%mm3 \n\t"
247 "punpckhbw %%mm0, %%mm3 \n\t"
248 "pmullw %%mm6, %%mm3 \n\t"
249 "psubusw %%mm3, %%mm2 \n\t"
250 "movq (%%"REG_S",%%"REG_a",2), %%mm3 \n\t"
251 "punpckhbw %%mm0, %%mm3 \n\t"
252 "add $8, %%"REG_S" \n\t"
253 "pmullw %%mm7, %%mm3 \n\t"
254 "psubusw %%mm3, %%mm2 \n\t"
255 "psrlw $7, %%mm2 \n\t"
257 "packuswb %%mm2, %%mm1 \n\t"
258 "movq %%mm1, (%%"REG_D") \n\t"
259 "add $8, %%"REG_D" \n\t"
260 "decl %%ecx \n\t"
261 "jnz 4b \n\t"
262 : "=S"(crap1), "=D"(crap2)
263 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd), "b"((long)-ssd), "d"(filter)
265 for (j=w-(w&7); j<w; j++)
266 d[j] = (-9*s[j-ssd] + 111*s[j] + 29*s[j+ssd] - 3*s[j+ssd+ssd])>>7;
267 d += ds;
268 s += ss;
270 for (j=0; j<w; j++)
271 d[j] = (s[j+ssd] + 3*s[j])>>2;
272 d += ds; s += ss;
273 if (!up) fast_memcpy(d, s, w);
274 __asm__ volatile("emms \n\t" : : : "memory");
276 #endif /* HAVE_EBX_AVAILABLE */
277 #endif
279 static inline int clamp(int a)
281 // If a<512, this is equivalent to:
282 // return (a<0) ? 0 : ( (a>255) ? 255 : a);
283 return (~(a>>31)) & (a | ((a<<23)>>31));
286 static void qpel_li_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
288 int i, j, ssd=ss;
289 if (up) {
290 ssd = -ss;
291 fast_memcpy(d, s, w);
292 d += ds;
293 s += ss;
295 for (i=h-1; i; i--) {
296 for (j=0; j<w; j++)
297 d[j] = (s[j+ssd] + 3*s[j])>>2;
298 d += ds;
299 s += ss;
301 if (!up) fast_memcpy(d, s, w);
304 static void qpel_4tap_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
306 int i, j, ssd=ss;
307 if (up) {
308 ssd = -ss;
309 fast_memcpy(d, s, w);
310 d += ds; s += ss;
312 for (j=0; j<w; j++)
313 d[j] = (s[j+ssd] + 3*s[j] + 2)>>2;
314 d += ds; s += ss;
315 for (i=h-3; i; i--) {
316 for (j=0; j<w; j++)
317 d[j] = clamp((-9*s[j-ssd] + 111*s[j] + 29*s[j+ssd] - 3*s[j+ssd+ssd] + 64)>>7);
318 d += ds; s += ss;
320 for (j=0; j<w; j++)
321 d[j] = (s[j+ssd] + 3*s[j] + 2)>>2;
322 d += ds; s += ss;
323 if (!up) fast_memcpy(d, s, w);
326 static void (*qpel_li)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
327 static void (*qpel_4tap)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
329 static int continue_buffered_image(struct vf_instance *vf);
331 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
333 vf->priv->buffered_mpi = mpi;
334 vf->priv->buffered_pts = pts;
335 vf->priv->buffered_i = 0;
336 return continue_buffered_image(vf);
339 extern const int under_mencoder;
341 static int continue_buffered_image(struct vf_instance *vf)
343 int i=vf->priv->buffered_i;
344 double pts = vf->priv->buffered_pts;
345 mp_image_t *mpi = vf->priv->buffered_mpi;
346 int ret=0;
347 mp_image_t *dmpi;
348 void (*qpel)(unsigned char *, unsigned char *, int, int, int, int, int);
349 int bpp=1;
350 int tff;
352 if (i == 0)
353 vf_queue_frame(vf, continue_buffered_image);
354 pts += i * .02; // XXX not right
356 if (!(mpi->flags & MP_IMGFLAG_PLANAR)) bpp = mpi->bpp/8;
357 if (vf->priv->parity < 0) {
358 if (mpi->fields & MP_IMGFIELD_ORDERED)
359 tff = mpi->fields & MP_IMGFIELD_TOP_FIRST;
360 else
361 tff = 1;
363 else tff = (vf->priv->parity&1)^1;
365 switch (vf->priv->mode) {
366 case 2:
367 qpel = qpel_li;
368 break;
369 case 3:
370 // TODO: add 3tap filter
371 qpel = qpel_4tap;
372 break;
373 case 4:
374 qpel = qpel_4tap;
375 break;
378 switch (vf->priv->mode) {
379 case 0:
380 for (; i<2; i++) {
381 dmpi = vf_get_image(vf->next, mpi->imgfmt,
382 MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE,
383 mpi->width, mpi->height/2);
384 dmpi->planes[0] = mpi->planes[0] + (i^!tff)*mpi->stride[0];
385 dmpi->stride[0] = 2*mpi->stride[0];
386 if (mpi->flags & MP_IMGFLAG_PLANAR) {
387 dmpi->planes[1] = mpi->planes[1] + (i^!tff)*mpi->stride[1];
388 dmpi->planes[2] = mpi->planes[2] + (i^!tff)*mpi->stride[2];
389 dmpi->stride[1] = 2*mpi->stride[1];
390 dmpi->stride[2] = 2*mpi->stride[2];
392 ret |= vf_next_put_image(vf, dmpi, pts);
393 if (!under_mencoder)
394 break;
396 break;
397 case 1:
398 for (; i<2; i++) {
399 dmpi = vf_get_image(vf->next, mpi->imgfmt,
400 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
401 mpi->width, mpi->height);
402 my_memcpy_pic(dmpi->planes[0] + (i^!tff)*dmpi->stride[0],
403 mpi->planes[0] + (i^!tff)*mpi->stride[0],
404 mpi->w*bpp, mpi->h/2, dmpi->stride[0]*2, mpi->stride[0]*2);
405 deint(dmpi->planes[0], dmpi->stride[0], mpi->planes[0], mpi->stride[0], mpi->w, mpi->h, (i^!tff));
406 if (mpi->flags & MP_IMGFLAG_PLANAR) {
407 my_memcpy_pic(dmpi->planes[1] + (i^!tff)*dmpi->stride[1],
408 mpi->planes[1] + (i^!tff)*mpi->stride[1],
409 mpi->chroma_width, mpi->chroma_height/2,
410 dmpi->stride[1]*2, mpi->stride[1]*2);
411 my_memcpy_pic(dmpi->planes[2] + (i^!tff)*dmpi->stride[2],
412 mpi->planes[2] + (i^!tff)*mpi->stride[2],
413 mpi->chroma_width, mpi->chroma_height/2,
414 dmpi->stride[2]*2, mpi->stride[2]*2);
415 deint(dmpi->planes[1], dmpi->stride[1], mpi->planes[1], mpi->stride[1],
416 mpi->chroma_width, mpi->chroma_height, (i^!tff));
417 deint(dmpi->planes[2], dmpi->stride[2], mpi->planes[2], mpi->stride[2],
418 mpi->chroma_width, mpi->chroma_height, (i^!tff));
420 ret |= vf_next_put_image(vf, dmpi, pts);
421 if (!under_mencoder)
422 break;
424 break;
425 case 2:
426 case 3:
427 case 4:
428 for (; i<2; i++) {
429 dmpi = vf_get_image(vf->next, mpi->imgfmt,
430 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
431 mpi->width, mpi->height/2);
432 qpel(dmpi->planes[0], mpi->planes[0] + (i^!tff)*mpi->stride[0],
433 mpi->w*bpp, mpi->h/2, dmpi->stride[0], mpi->stride[0]*2, (i^!tff));
434 if (mpi->flags & MP_IMGFLAG_PLANAR) {
435 qpel(dmpi->planes[1],
436 mpi->planes[1] + (i^!tff)*mpi->stride[1],
437 mpi->chroma_width, mpi->chroma_height/2,
438 dmpi->stride[1], mpi->stride[1]*2, (i^!tff));
439 qpel(dmpi->planes[2],
440 mpi->planes[2] + (i^!tff)*mpi->stride[2],
441 mpi->chroma_width, mpi->chroma_height/2,
442 dmpi->stride[2], mpi->stride[2]*2, (i^!tff));
444 ret |= vf_next_put_image(vf, dmpi, pts);
445 if (!under_mencoder)
446 break;
448 break;
450 vf->priv->buffered_i = 1;
451 return ret;
454 static int query_format(struct vf_instance *vf, unsigned int fmt)
456 /* FIXME - figure out which formats exactly work */
457 switch (fmt) {
458 default:
459 if (vf->priv->mode == 1)
460 return 0;
461 case IMGFMT_YV12:
462 case IMGFMT_IYUV:
463 case IMGFMT_I420:
464 return vf_next_query_format(vf, fmt);
466 return 0;
469 static int config(struct vf_instance *vf,
470 int width, int height, int d_width, int d_height,
471 unsigned int flags, unsigned int outfmt)
473 switch (vf->priv->mode) {
474 case 0:
475 case 2:
476 case 3:
477 case 4:
478 return vf_next_config(vf,width,height/2,d_width,d_height,flags,outfmt);
479 case 1:
480 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
482 return 0;
485 static void uninit(struct vf_instance *vf)
487 free(vf->priv);
490 static int vf_open(vf_instance_t *vf, char *args)
492 struct vf_priv_s *p;
493 vf->config = config;
494 vf->put_image = put_image;
495 vf->query_format = query_format;
496 vf->uninit = uninit;
497 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
498 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
499 vf->priv->mode = 4;
500 vf->priv->parity = -1;
501 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
502 qpel_li = qpel_li_C;
503 qpel_4tap = qpel_4tap_C;
504 #if HAVE_MMX
505 if(gCpuCaps.hasMMX) qpel_li = qpel_li_MMX;
506 #if HAVE_EBX_AVAILABLE
507 if(gCpuCaps.hasMMX) qpel_4tap = qpel_4tap_MMX;
508 #endif
509 #endif
510 #if HAVE_MMX2
511 if(gCpuCaps.hasMMX2) qpel_li = qpel_li_MMX2;
512 #endif
513 #if HAVE_AMD3DNOW
514 if(gCpuCaps.has3DNow) qpel_li = qpel_li_3DNOW;
515 #endif
516 return 1;
519 const vf_info_t vf_info_tfields = {
520 "temporal field separation",
521 "tfields",
522 "Rich Felker",
524 vf_open,
525 NULL