Fix bad uninit when switching DVB channels.
[mplayer/glamo.git] / libmpcodecs / vf_tfields.c
blob07d79546a683815da7ea37238e61e80184fc7a4c
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 inline void *my_memcpy_pic(void * dst, void * src, int bytesPerLine, int height, int dstStride, int srcStride)
25 int i;
26 void *retval=dst;
28 for(i=0; i<height; i++)
30 memcpy(dst, src, bytesPerLine);
31 src+= srcStride;
32 dst+= dstStride;
35 return retval;
38 static void deint(unsigned char *dest, int ds, unsigned char *src, int ss, int w, int h, int field)
40 int x, y;
41 src += ss;
42 dest += ds;
43 if (field) {
44 src += ss;
45 dest += ds;
46 h -= 2;
48 for (y=h/2; y; y--) {
49 for (x=0; x<w; x++) {
50 if (((src[x-ss] < src[x]) && (src[x+ss] < src[x])) ||
51 ((src[x-ss] > src[x]) && (src[x+ss] > src[x]))) {
52 //dest[x] = (src[x+ss] + src[x-ss])>>1;
53 dest[x] = ((src[x+ss]<<1) + (src[x-ss]<<1)
54 + src[x+ss+1] + src[x-ss+1]
55 + src[x+ss-1] + src[x-ss-1])>>3;
57 else dest[x] = src[x];
59 dest += ds<<1;
60 src += ss<<1;
64 #ifdef HAVE_3DNOW
65 static void qpel_li_3DNOW(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
67 int i, j, ssd=ss;
68 long crap1, crap2;
69 if (up) {
70 ssd = -ss;
71 memcpy(d, s, w);
72 d += ds;
73 s += ss;
75 for (i=h-1; i; i--) {
76 asm volatile(
77 "1: \n\t"
78 "movq (%%"REG_S"), %%mm0 \n\t"
79 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
80 "pavgusb %%mm0, %%mm1 \n\t"
81 "add $8, %%"REG_S" \n\t"
82 "pavgusb %%mm0, %%mm1 \n\t"
83 "movq %%mm1, (%%"REG_D") \n\t"
84 "add $8, %%"REG_D" \n\t"
85 "decl %%ecx \n\t"
86 "jnz 1b \n\t"
87 : "=S"(crap1), "=D"(crap2)
88 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
90 for (j=w-(w&7); j<w; j++)
91 d[j] = (s[j+ssd] + 3*s[j])>>2;
92 d += ds;
93 s += ss;
95 if (!up) memcpy(d, s, w);
96 asm volatile("emms \n\t" : : : "memory");
98 #endif
100 #ifdef HAVE_MMX2
101 static void qpel_li_MMX2(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
103 int i, j, ssd=ss;
104 long crap1, crap2;
105 if (up) {
106 ssd = -ss;
107 memcpy(d, s, w);
108 d += ds;
109 s += ss;
111 for (i=h-1; i; i--) {
112 asm volatile(
113 "pxor %%mm7, %%mm7 \n\t"
114 "2: \n\t"
115 "movq (%%"REG_S"), %%mm0 \n\t"
116 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
117 "pavgb %%mm0, %%mm1 \n\t"
118 "add $8, %%"REG_S" \n\t"
119 "pavgb %%mm0, %%mm1 \n\t"
120 "movq %%mm1, (%%"REG_D") \n\t"
121 "add $8, %%"REG_D" \n\t"
122 "decl %%ecx \n\t"
123 "jnz 2b \n\t"
124 : "=S"(crap1), "=D"(crap2)
125 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
127 for (j=w-(w&7); j<w; j++)
128 d[j] = (s[j+ssd] + 3*s[j])>>2;
129 d += ds;
130 s += ss;
132 if (!up) memcpy(d, s, w);
133 asm volatile("emms \n\t" : : : "memory");
135 #endif
137 #ifdef HAVE_MMX
138 static void qpel_li_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
140 int i, j, ssd=ss;
141 int crap1, crap2;
142 if (up) {
143 ssd = -ss;
144 memcpy(d, s, w);
145 d += ds;
146 s += ss;
148 for (i=h-1; i; i--) {
149 asm volatile(
150 "pxor %%mm7, %%mm7 \n\t"
151 "3: \n\t"
152 "movq (%%"REG_S"), %%mm0 \n\t"
153 "movq (%%"REG_S"), %%mm1 \n\t"
154 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
155 "movq (%%"REG_S",%%"REG_a"), %%mm3 \n\t"
156 "add $8, %%"REG_S" \n\t"
157 "punpcklbw %%mm7, %%mm0 \n\t"
158 "punpckhbw %%mm7, %%mm1 \n\t"
159 "punpcklbw %%mm7, %%mm2 \n\t"
160 "punpckhbw %%mm7, %%mm3 \n\t"
161 "paddw %%mm0, %%mm2 \n\t"
162 "paddw %%mm1, %%mm3 \n\t"
163 "paddw %%mm0, %%mm2 \n\t"
164 "paddw %%mm1, %%mm3 \n\t"
165 "paddw %%mm0, %%mm2 \n\t"
166 "paddw %%mm1, %%mm3 \n\t"
167 "psrlw $2, %%mm2 \n\t"
168 "psrlw $2, %%mm3 \n\t"
169 "packsswb %%mm3, %%mm2 \n\t"
170 "movq %%mm2, (%%"REG_D") \n\t"
171 "add $8, %%"REG_D" \n\t"
172 "decl %%ecx \n\t"
173 "jnz 3b \n\t"
174 : "=S"(crap1), "=D"(crap2)
175 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
177 for (j=w-(w&7); j<w; j++)
178 d[j] = (s[j+ssd] + 3*s[j])>>2;
179 d += ds;
180 s += ss;
182 if (!up) memcpy(d, s, w);
183 asm volatile("emms \n\t" : : : "memory");
186 static void qpel_4tap_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
188 int i, j, ssd=ss;
189 static const short filter[] = {
190 29, 29, 29, 29, 110, 110, 110, 110,
191 9, 9, 9, 9, 3, 3, 3, 3,
192 64, 64, 64, 64 };
193 int crap1, crap2;
194 if (up) {
195 ssd = -ss;
196 memcpy(d, s, w);
197 d += ds; s += ss;
199 for (j=0; j<w; j++)
200 d[j] = (s[j+ssd] + 3*s[j])>>2;
201 d += ds; s += ss;
202 for (i=h-3; i; i--) {
203 asm volatile(
204 "pxor %%mm0, %%mm0 \n\t"
205 "movq (%%"REG_d"), %%mm4 \n\t"
206 "movq 8(%%"REG_d"), %%mm5 \n\t"
207 "movq 16(%%"REG_d"), %%mm6 \n\t"
208 "movq 24(%%"REG_d"), %%mm7 \n\t"
209 "4: \n\t"
211 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
212 "movq (%%"REG_S"), %%mm2 \n\t"
213 "movq (%%"REG_S",%%"REG_b"), %%mm3 \n\t"
214 "punpcklbw %%mm0, %%mm1 \n\t"
215 "punpcklbw %%mm0, %%mm2 \n\t"
216 "pmullw %%mm4, %%mm1 \n\t"
217 "punpcklbw %%mm0, %%mm3 \n\t"
218 "pmullw %%mm5, %%mm2 \n\t"
219 "paddusw %%mm2, %%mm1 \n\t"
220 "pmullw %%mm6, %%mm3 \n\t"
221 "movq (%%"REG_S",%%"REG_a",2), %%mm2 \n\t"
222 "psubusw %%mm3, %%mm1 \n\t"
223 "punpcklbw %%mm0, %%mm2 \n\t"
224 "pmullw %%mm7, %%mm2 \n\t"
225 "psubusw %%mm2, %%mm1 \n\t"
226 "psrlw $7, %%mm1 \n\t"
228 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
229 "movq (%%"REG_S"), %%mm3 \n\t"
230 "punpckhbw %%mm0, %%mm2 \n\t"
231 "punpckhbw %%mm0, %%mm3 \n\t"
232 "pmullw %%mm4, %%mm2 \n\t"
233 "pmullw %%mm5, %%mm3 \n\t"
234 "paddusw %%mm3, %%mm2 \n\t"
235 "movq (%%"REG_S",%%"REG_b"), %%mm3 \n\t"
236 "punpckhbw %%mm0, %%mm3 \n\t"
237 "pmullw %%mm6, %%mm3 \n\t"
238 "psubusw %%mm3, %%mm2 \n\t"
239 "movq (%%"REG_S",%%"REG_a",2), %%mm3 \n\t"
240 "punpckhbw %%mm0, %%mm3 \n\t"
241 "add $8, %%"REG_S" \n\t"
242 "pmullw %%mm7, %%mm3 \n\t"
243 "psubusw %%mm3, %%mm2 \n\t"
244 "psrlw $7, %%mm2 \n\t"
246 "packuswb %%mm2, %%mm1 \n\t"
247 "movq %%mm1, (%%"REG_D") \n\t"
248 "add $8, %%"REG_D" \n\t"
249 "decl %%ecx \n\t"
250 "jnz 4b \n\t"
251 : "=S"(crap1), "=D"(crap2)
252 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd), "b"((long)-ssd), "d"(filter)
254 for (j=w-(w&7); j<w; j++)
255 d[j] = (-9*s[j-ssd] + 111*s[j] + 29*s[j+ssd] - 3*s[j+ssd+ssd])>>7;
256 d += ds;
257 s += ss;
259 for (j=0; j<w; j++)
260 d[j] = (s[j+ssd] + 3*s[j])>>2;
261 d += ds; s += ss;
262 if (!up) memcpy(d, s, w);
263 asm volatile("emms \n\t" : : : "memory");
265 #endif
267 static inline int clamp(int a)
269 // If a<512, this is equivalent to:
270 // return (a<0) ? 0 : ( (a>255) ? 255 : a);
271 return (~(a>>31)) & (a | ((a<<23)>>31));
274 static void qpel_li_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
276 int i, j, ssd=ss;
277 if (up) {
278 ssd = -ss;
279 memcpy(d, s, w);
280 d += ds;
281 s += ss;
283 for (i=h-1; i; i--) {
284 for (j=0; j<w; j++)
285 d[j] = (s[j+ssd] + 3*s[j])>>2;
286 d += ds;
287 s += ss;
289 if (!up) memcpy(d, s, w);
292 static void qpel_4tap_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
294 int i, j, ssd=ss;
295 if (up) {
296 ssd = -ss;
297 memcpy(d, s, w);
298 d += ds; s += ss;
300 for (j=0; j<w; j++)
301 d[j] = (s[j+ssd] + 3*s[j] + 2)>>2;
302 d += ds; s += ss;
303 for (i=h-3; i; i--) {
304 for (j=0; j<w; j++)
305 d[j] = clamp((-9*s[j-ssd] + 111*s[j] + 29*s[j+ssd] - 3*s[j+ssd+ssd] + 64)>>7);
306 d += ds; s += ss;
308 for (j=0; j<w; j++)
309 d[j] = (s[j+ssd] + 3*s[j] + 2)>>2;
310 d += ds; s += ss;
311 if (!up) memcpy(d, s, w);
314 static void (*qpel_li)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
315 static void (*qpel_4tap)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
317 static int continue_buffered_image(struct vf_instance_s *);
318 extern int correct_pts;
320 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
322 vf->priv->buffered_mpi = mpi;
323 vf->priv->buffered_pts = pts;
324 vf->priv->buffered_i = 0;
325 return continue_buffered_image(vf);
328 static int continue_buffered_image(struct vf_instance_s *vf)
330 int i=vf->priv->buffered_i;
331 double pts = vf->priv->buffered_pts;
332 mp_image_t *mpi = vf->priv->buffered_mpi;
333 int ret=0;
334 mp_image_t *dmpi;
335 void (*qpel)(unsigned char *, unsigned char *, int, int, int, int, int);
336 int bpp=1;
337 int tff;
339 if (i == 0)
340 vf_queue_frame(vf, continue_buffered_image);
341 pts += i * .02; // XXX not right
343 if (!(mpi->flags & MP_IMGFLAG_PLANAR)) bpp = mpi->bpp/8;
344 if (vf->priv->parity < 0) {
345 if (mpi->fields & MP_IMGFIELD_ORDERED)
346 tff = mpi->fields & MP_IMGFIELD_TOP_FIRST;
347 else
348 tff = 1;
350 else tff = (vf->priv->parity&1)^1;
352 switch (vf->priv->mode) {
353 case 2:
354 qpel = qpel_li;
355 break;
356 case 3:
357 // TODO: add 3tap filter
358 qpel = qpel_4tap;
359 break;
360 case 4:
361 qpel = qpel_4tap;
362 break;
365 switch (vf->priv->mode) {
366 case 0:
367 for (; i<2; i++) {
368 dmpi = vf_get_image(vf->next, mpi->imgfmt,
369 MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE,
370 mpi->width, mpi->height/2);
371 dmpi->planes[0] = mpi->planes[0] + (i^!tff)*mpi->stride[0];
372 dmpi->stride[0] = 2*mpi->stride[0];
373 if (mpi->flags & MP_IMGFLAG_PLANAR) {
374 dmpi->planes[1] = mpi->planes[1] + (i^!tff)*mpi->stride[1];
375 dmpi->planes[2] = mpi->planes[2] + (i^!tff)*mpi->stride[2];
376 dmpi->stride[1] = 2*mpi->stride[1];
377 dmpi->stride[2] = 2*mpi->stride[2];
379 ret |= vf_next_put_image(vf, dmpi, pts);
380 if (correct_pts)
381 break;
382 else
383 if (!i) vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
385 break;
386 case 1:
387 for (; i<2; i++) {
388 dmpi = vf_get_image(vf->next, mpi->imgfmt,
389 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
390 mpi->width, mpi->height);
391 my_memcpy_pic(dmpi->planes[0] + (i^!tff)*dmpi->stride[0],
392 mpi->planes[0] + (i^!tff)*mpi->stride[0],
393 mpi->w*bpp, mpi->h/2, dmpi->stride[0]*2, mpi->stride[0]*2);
394 deint(dmpi->planes[0], dmpi->stride[0], mpi->planes[0], mpi->stride[0], mpi->w, mpi->h, (i^!tff));
395 if (mpi->flags & MP_IMGFLAG_PLANAR) {
396 my_memcpy_pic(dmpi->planes[1] + (i^!tff)*dmpi->stride[1],
397 mpi->planes[1] + (i^!tff)*mpi->stride[1],
398 mpi->chroma_width, mpi->chroma_height/2,
399 dmpi->stride[1]*2, mpi->stride[1]*2);
400 my_memcpy_pic(dmpi->planes[2] + (i^!tff)*dmpi->stride[2],
401 mpi->planes[2] + (i^!tff)*mpi->stride[2],
402 mpi->chroma_width, mpi->chroma_height/2,
403 dmpi->stride[2]*2, mpi->stride[2]*2);
404 deint(dmpi->planes[1], dmpi->stride[1], mpi->planes[1], mpi->stride[1],
405 mpi->chroma_width, mpi->chroma_height, (i^!tff));
406 deint(dmpi->planes[2], dmpi->stride[2], mpi->planes[2], mpi->stride[2],
407 mpi->chroma_width, mpi->chroma_height, (i^!tff));
409 ret |= vf_next_put_image(vf, dmpi, pts);
410 if (correct_pts)
411 break;
412 else
413 if (!i) vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
415 break;
416 case 2:
417 case 3:
418 case 4:
419 for (; i<2; i++) {
420 dmpi = vf_get_image(vf->next, mpi->imgfmt,
421 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
422 mpi->width, mpi->height/2);
423 qpel(dmpi->planes[0], mpi->planes[0] + (i^!tff)*mpi->stride[0],
424 mpi->w*bpp, mpi->h/2, dmpi->stride[0], mpi->stride[0]*2, (i^!tff));
425 if (mpi->flags & MP_IMGFLAG_PLANAR) {
426 qpel(dmpi->planes[1],
427 mpi->planes[1] + (i^!tff)*mpi->stride[1],
428 mpi->chroma_width, mpi->chroma_height/2,
429 dmpi->stride[1], mpi->stride[1]*2, (i^!tff));
430 qpel(dmpi->planes[2],
431 mpi->planes[2] + (i^!tff)*mpi->stride[2],
432 mpi->chroma_width, mpi->chroma_height/2,
433 dmpi->stride[2], mpi->stride[2]*2, (i^!tff));
435 ret |= vf_next_put_image(vf, dmpi, pts);
436 if (correct_pts)
437 break;
438 else
439 if (!i) vf_next_control(vf, VFCTRL_FLIP_PAGE, NULL);
441 break;
443 vf->priv->buffered_i = 1;
444 return ret;
447 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
449 /* FIXME - figure out which other formats work */
450 switch (fmt) {
451 case IMGFMT_YV12:
452 case IMGFMT_IYUV:
453 case IMGFMT_I420:
454 return vf_next_query_format(vf, fmt);
456 return 0;
459 static int config(struct vf_instance_s* vf,
460 int width, int height, int d_width, int d_height,
461 unsigned int flags, unsigned int outfmt)
463 switch (vf->priv->mode) {
464 case 0:
465 case 2:
466 case 3:
467 case 4:
468 return vf_next_config(vf,width,height/2,d_width,d_height,flags,outfmt);
469 case 1:
470 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
472 return 0;
475 static void uninit(struct vf_instance_s* vf)
477 free(vf->priv);
480 static int open(vf_instance_t *vf, char* args)
482 struct vf_priv_s *p;
483 vf->config = config;
484 vf->put_image = put_image;
485 //vf->query_format = query_format;
486 vf->uninit = uninit;
487 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
488 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
489 vf->priv->mode = 4;
490 vf->priv->parity = -1;
491 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
492 qpel_li = qpel_li_C;
493 qpel_4tap = qpel_4tap_C;
494 #ifdef HAVE_MMX
495 if(gCpuCaps.hasMMX) qpel_li = qpel_li_MMX;
496 if(gCpuCaps.hasMMX) qpel_4tap = qpel_4tap_MMX;
497 #endif
498 #ifdef HAVE_MMX2
499 if(gCpuCaps.hasMMX2) qpel_li = qpel_li_MMX2;
500 #endif
501 #ifdef HAVE_3DNOW
502 if(gCpuCaps.has3DNow) qpel_li = qpel_li_3DNOW;
503 #endif
504 return 1;
507 vf_info_t vf_info_tfields = {
508 "temporal field separation",
509 "tfields",
510 "Rich Felker",
512 open,
513 NULL