Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / libmpcodecs / vf_tfields.c
blobff4dd6243462bea9fc95a40dd7b59a0d3a9b70c9
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 "mp_msg.h"
25 #include "cpudetect.h"
27 #include "img_format.h"
28 #include "mp_image.h"
29 #include "vf.h"
31 #include "libvo/fastmemcpy.h"
33 struct vf_priv_s {
34 int mode;
35 int parity;
36 int buffered_i;
37 mp_image_t *buffered_mpi;
38 double buffered_pts;
41 static void deint(unsigned char *dest, int ds, unsigned char *src, int ss, int w, int h, int field)
43 int x, y;
44 src += ss;
45 dest += ds;
46 h--;
47 if (field) {
48 fast_memcpy(dest - ds, src - ss, w);
49 src += ss;
50 dest += ds;
51 h--;
53 for (y=h/2; y > 0; y--) {
54 dest[0] = src[0];
55 for (x=1; x<w-1; x++) {
56 if (((src[x-ss] < src[x]) && (src[x+ss] < src[x])) ||
57 ((src[x-ss] > src[x]) && (src[x+ss] > src[x]))) {
58 //dest[x] = (src[x+ss] + src[x-ss])>>1;
59 dest[x] = ((src[x+ss]<<1) + (src[x-ss]<<1)
60 + src[x+ss+1] + src[x-ss+1]
61 + src[x+ss-1] + src[x-ss-1])>>3;
63 else dest[x] = src[x];
65 dest[w-1] = src[w-1];
66 dest += ds<<1;
67 src += ss<<1;
69 if (h & 1)
70 fast_memcpy(dest, src, w);
73 #if HAVE_AMD3DNOW
74 static void qpel_li_3DNOW(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
76 int i, j, ssd=ss;
77 long crap1, crap2;
78 if (up) {
79 ssd = -ss;
80 fast_memcpy(d, s, w);
81 d += ds;
82 s += ss;
84 for (i=h-1; i; i--) {
85 __asm__ volatile(
86 "1: \n\t"
87 "movq (%%"REG_S"), %%mm0 \n\t"
88 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
89 "pavgusb %%mm0, %%mm1 \n\t"
90 "add $8, %%"REG_S" \n\t"
91 "pavgusb %%mm0, %%mm1 \n\t"
92 "movq %%mm1, (%%"REG_D") \n\t"
93 "add $8, %%"REG_D" \n\t"
94 "decl %%ecx \n\t"
95 "jnz 1b \n\t"
96 : "=S"(crap1), "=D"(crap2)
97 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
99 for (j=w-(w&7); j<w; j++)
100 d[j] = (s[j+ssd] + 3*s[j])>>2;
101 d += ds;
102 s += ss;
104 if (!up) fast_memcpy(d, s, w);
105 __asm__ volatile("emms \n\t" : : : "memory");
107 #endif
109 #if HAVE_MMX2
110 static void qpel_li_MMX2(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
112 int i, j, ssd=ss;
113 long crap1, crap2;
114 if (up) {
115 ssd = -ss;
116 fast_memcpy(d, s, w);
117 d += ds;
118 s += ss;
120 for (i=h-1; i; i--) {
121 __asm__ volatile(
122 "pxor %%mm7, %%mm7 \n\t"
123 "2: \n\t"
124 "movq (%%"REG_S"), %%mm0 \n\t"
125 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
126 "pavgb %%mm0, %%mm1 \n\t"
127 "add $8, %%"REG_S" \n\t"
128 "pavgb %%mm0, %%mm1 \n\t"
129 "movq %%mm1, (%%"REG_D") \n\t"
130 "add $8, %%"REG_D" \n\t"
131 "decl %%ecx \n\t"
132 "jnz 2b \n\t"
133 : "=S"(crap1), "=D"(crap2)
134 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
136 for (j=w-(w&7); j<w; j++)
137 d[j] = (s[j+ssd] + 3*s[j])>>2;
138 d += ds;
139 s += ss;
141 if (!up) fast_memcpy(d, s, w);
142 __asm__ volatile("emms \n\t" : : : "memory");
144 #endif
146 #if HAVE_MMX
147 static void qpel_li_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
149 int i, j, ssd=ss;
150 int crap1, crap2;
151 if (up) {
152 ssd = -ss;
153 fast_memcpy(d, s, w);
154 d += ds;
155 s += ss;
157 for (i=h-1; i; i--) {
158 __asm__ volatile(
159 "pxor %%mm7, %%mm7 \n\t"
160 "3: \n\t"
161 "movq (%%"REG_S"), %%mm0 \n\t"
162 "movq (%%"REG_S"), %%mm1 \n\t"
163 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
164 "movq (%%"REG_S",%%"REG_a"), %%mm3 \n\t"
165 "add $8, %%"REG_S" \n\t"
166 "punpcklbw %%mm7, %%mm0 \n\t"
167 "punpckhbw %%mm7, %%mm1 \n\t"
168 "punpcklbw %%mm7, %%mm2 \n\t"
169 "punpckhbw %%mm7, %%mm3 \n\t"
170 "paddw %%mm0, %%mm2 \n\t"
171 "paddw %%mm1, %%mm3 \n\t"
172 "paddw %%mm0, %%mm2 \n\t"
173 "paddw %%mm1, %%mm3 \n\t"
174 "paddw %%mm0, %%mm2 \n\t"
175 "paddw %%mm1, %%mm3 \n\t"
176 "psrlw $2, %%mm2 \n\t"
177 "psrlw $2, %%mm3 \n\t"
178 "packsswb %%mm3, %%mm2 \n\t"
179 "movq %%mm2, (%%"REG_D") \n\t"
180 "add $8, %%"REG_D" \n\t"
181 "decl %%ecx \n\t"
182 "jnz 3b \n\t"
183 : "=S"(crap1), "=D"(crap2)
184 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd)
186 for (j=w-(w&7); j<w; j++)
187 d[j] = (s[j+ssd] + 3*s[j])>>2;
188 d += ds;
189 s += ss;
191 if (!up) fast_memcpy(d, s, w);
192 __asm__ volatile("emms \n\t" : : : "memory");
195 #if HAVE_EBX_AVAILABLE
196 static void qpel_4tap_MMX(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
198 int i, j, ssd=ss;
199 static const short filter[] = {
200 29, 29, 29, 29, 110, 110, 110, 110,
201 9, 9, 9, 9, 3, 3, 3, 3,
202 64, 64, 64, 64 };
203 int crap1, crap2;
204 if (up) {
205 ssd = -ss;
206 fast_memcpy(d, s, w);
207 d += ds; s += ss;
209 for (j=0; j<w; j++)
210 d[j] = (s[j+ssd] + 3*s[j])>>2;
211 d += ds; s += ss;
212 for (i=h-3; i; i--) {
213 __asm__ volatile(
214 "pxor %%mm0, %%mm0 \n\t"
215 "movq (%%"REG_d"), %%mm4 \n\t"
216 "movq 8(%%"REG_d"), %%mm5 \n\t"
217 "movq 16(%%"REG_d"), %%mm6 \n\t"
218 "movq 24(%%"REG_d"), %%mm7 \n\t"
219 "4: \n\t"
221 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
222 "movq (%%"REG_S"), %%mm2 \n\t"
223 "movq (%%"REG_S",%%"REG_b"), %%mm3 \n\t"
224 "punpcklbw %%mm0, %%mm1 \n\t"
225 "punpcklbw %%mm0, %%mm2 \n\t"
226 "pmullw %%mm4, %%mm1 \n\t"
227 "punpcklbw %%mm0, %%mm3 \n\t"
228 "pmullw %%mm5, %%mm2 \n\t"
229 "paddusw %%mm2, %%mm1 \n\t"
230 "pmullw %%mm6, %%mm3 \n\t"
231 "movq (%%"REG_S",%%"REG_a",2), %%mm2 \n\t"
232 "psubusw %%mm3, %%mm1 \n\t"
233 "punpcklbw %%mm0, %%mm2 \n\t"
234 "pmullw %%mm7, %%mm2 \n\t"
235 "psubusw %%mm2, %%mm1 \n\t"
236 "psrlw $7, %%mm1 \n\t"
238 "movq (%%"REG_S",%%"REG_a"), %%mm2 \n\t"
239 "movq (%%"REG_S"), %%mm3 \n\t"
240 "punpckhbw %%mm0, %%mm2 \n\t"
241 "punpckhbw %%mm0, %%mm3 \n\t"
242 "pmullw %%mm4, %%mm2 \n\t"
243 "pmullw %%mm5, %%mm3 \n\t"
244 "paddusw %%mm3, %%mm2 \n\t"
245 "movq (%%"REG_S",%%"REG_b"), %%mm3 \n\t"
246 "punpckhbw %%mm0, %%mm3 \n\t"
247 "pmullw %%mm6, %%mm3 \n\t"
248 "psubusw %%mm3, %%mm2 \n\t"
249 "movq (%%"REG_S",%%"REG_a",2), %%mm3 \n\t"
250 "punpckhbw %%mm0, %%mm3 \n\t"
251 "add $8, %%"REG_S" \n\t"
252 "pmullw %%mm7, %%mm3 \n\t"
253 "psubusw %%mm3, %%mm2 \n\t"
254 "psrlw $7, %%mm2 \n\t"
256 "packuswb %%mm2, %%mm1 \n\t"
257 "movq %%mm1, (%%"REG_D") \n\t"
258 "add $8, %%"REG_D" \n\t"
259 "decl %%ecx \n\t"
260 "jnz 4b \n\t"
261 : "=S"(crap1), "=D"(crap2)
262 : "c"(w>>3), "S"(s), "D"(d), "a"((long)ssd), "b"((long)-ssd), "d"(filter)
264 for (j=w-(w&7); j<w; j++)
265 d[j] = (-9*s[j-ssd] + 111*s[j] + 29*s[j+ssd] - 3*s[j+ssd+ssd])>>7;
266 d += ds;
267 s += ss;
269 for (j=0; j<w; j++)
270 d[j] = (s[j+ssd] + 3*s[j])>>2;
271 d += ds; s += ss;
272 if (!up) fast_memcpy(d, s, w);
273 __asm__ volatile("emms \n\t" : : : "memory");
275 #endif /* HAVE_EBX_AVAILABLE */
276 #endif
278 static inline int clamp(int a)
280 // If a<512, this is equivalent to:
281 // return (a<0) ? 0 : ( (a>255) ? 255 : a);
282 return (~(a>>31)) & (a | ((a<<23)>>31));
285 static void qpel_li_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
287 int i, j, ssd=ss;
288 if (up) {
289 ssd = -ss;
290 fast_memcpy(d, s, w);
291 d += ds;
292 s += ss;
294 for (i=h-1; i; i--) {
295 for (j=0; j<w; j++)
296 d[j] = (s[j+ssd] + 3*s[j])>>2;
297 d += ds;
298 s += ss;
300 if (!up) fast_memcpy(d, s, w);
303 static void qpel_4tap_C(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up)
305 int i, j, ssd=ss;
306 if (up) {
307 ssd = -ss;
308 fast_memcpy(d, s, w);
309 d += ds; s += ss;
311 for (j=0; j<w; j++)
312 d[j] = (s[j+ssd] + 3*s[j] + 2)>>2;
313 d += ds; s += ss;
314 for (i=h-3; i; i--) {
315 for (j=0; j<w; j++)
316 d[j] = clamp((-9*s[j-ssd] + 111*s[j] + 29*s[j+ssd] - 3*s[j+ssd+ssd] + 64)>>7);
317 d += ds; s += ss;
319 for (j=0; j<w; j++)
320 d[j] = (s[j+ssd] + 3*s[j] + 2)>>2;
321 d += ds; s += ss;
322 if (!up) fast_memcpy(d, s, w);
325 static void (*qpel_li)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
326 static void (*qpel_4tap)(unsigned char *d, unsigned char *s, int w, int h, int ds, int ss, int up);
328 static int continue_buffered_image(struct vf_instance *vf);
329 extern int correct_pts;
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 static double calc_pts(double base_pts, int field)
341 // FIXME this assumes 25 fps / 50 fields per second
342 return base_pts + 0.02 * field;
345 static int continue_buffered_image(struct vf_instance *vf)
347 int i=vf->priv->buffered_i;
348 double pts = vf->priv->buffered_pts;
349 mp_image_t *mpi = vf->priv->buffered_mpi;
350 int ret=0;
351 mp_image_t *dmpi;
352 void (*qpel)(unsigned char *, unsigned char *, int, int, int, int, int);
353 int bpp=1;
354 int tff;
356 if (i == 0)
357 vf_queue_frame(vf, continue_buffered_image);
359 if (!(mpi->flags & MP_IMGFLAG_PLANAR)) bpp = mpi->bpp/8;
360 if (vf->priv->parity < 0) {
361 if (mpi->fields & MP_IMGFIELD_ORDERED)
362 tff = mpi->fields & MP_IMGFIELD_TOP_FIRST;
363 else
364 tff = 1;
366 else tff = (vf->priv->parity&1)^1;
368 switch (vf->priv->mode) {
369 case 2:
370 qpel = qpel_li;
371 break;
372 case 3:
373 // TODO: add 3tap filter
374 qpel = qpel_4tap;
375 break;
376 case 4:
377 qpel = qpel_4tap;
378 break;
381 switch (vf->priv->mode) {
382 case 0:
383 for (; i<2; i++) {
384 dmpi = vf_get_image(vf->next, mpi->imgfmt,
385 MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE,
386 mpi->width, mpi->height/2);
387 dmpi->planes[0] = mpi->planes[0] + (i^!tff)*mpi->stride[0];
388 dmpi->stride[0] = 2*mpi->stride[0];
389 if (mpi->flags & MP_IMGFLAG_PLANAR) {
390 dmpi->planes[1] = mpi->planes[1] + (i^!tff)*mpi->stride[1];
391 dmpi->planes[2] = mpi->planes[2] + (i^!tff)*mpi->stride[2];
392 dmpi->stride[1] = 2*mpi->stride[1];
393 dmpi->stride[2] = 2*mpi->stride[2];
395 ret |= vf_next_put_image(vf, dmpi, calc_pts(pts, i));
396 if (correct_pts)
397 break;
398 else
399 if (!i) vf_extra_flip(vf);
401 break;
402 case 1:
403 for (; i<2; i++) {
404 dmpi = vf_get_image(vf->next, mpi->imgfmt,
405 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
406 mpi->width, mpi->height);
407 my_memcpy_pic(dmpi->planes[0] + (i^!tff)*dmpi->stride[0],
408 mpi->planes[0] + (i^!tff)*mpi->stride[0],
409 mpi->w*bpp, mpi->h/2, dmpi->stride[0]*2, mpi->stride[0]*2);
410 deint(dmpi->planes[0], dmpi->stride[0], mpi->planes[0], mpi->stride[0], mpi->w, mpi->h, (i^!tff));
411 if (mpi->flags & MP_IMGFLAG_PLANAR) {
412 my_memcpy_pic(dmpi->planes[1] + (i^!tff)*dmpi->stride[1],
413 mpi->planes[1] + (i^!tff)*mpi->stride[1],
414 mpi->chroma_width, mpi->chroma_height/2,
415 dmpi->stride[1]*2, mpi->stride[1]*2);
416 my_memcpy_pic(dmpi->planes[2] + (i^!tff)*dmpi->stride[2],
417 mpi->planes[2] + (i^!tff)*mpi->stride[2],
418 mpi->chroma_width, mpi->chroma_height/2,
419 dmpi->stride[2]*2, mpi->stride[2]*2);
420 deint(dmpi->planes[1], dmpi->stride[1], mpi->planes[1], mpi->stride[1],
421 mpi->chroma_width, mpi->chroma_height, (i^!tff));
422 deint(dmpi->planes[2], dmpi->stride[2], mpi->planes[2], mpi->stride[2],
423 mpi->chroma_width, mpi->chroma_height, (i^!tff));
425 ret |= vf_next_put_image(vf, dmpi, calc_pts(pts, i));
426 if (correct_pts)
427 break;
428 else
429 if (!i) vf_extra_flip(vf);
431 break;
432 case 2:
433 case 3:
434 case 4:
435 for (; i<2; i++) {
436 dmpi = vf_get_image(vf->next, mpi->imgfmt,
437 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
438 mpi->width, mpi->height/2);
439 qpel(dmpi->planes[0], mpi->planes[0] + (i^!tff)*mpi->stride[0],
440 mpi->w*bpp, mpi->h/2, dmpi->stride[0], mpi->stride[0]*2, (i^!tff));
441 if (mpi->flags & MP_IMGFLAG_PLANAR) {
442 qpel(dmpi->planes[1],
443 mpi->planes[1] + (i^!tff)*mpi->stride[1],
444 mpi->chroma_width, mpi->chroma_height/2,
445 dmpi->stride[1], mpi->stride[1]*2, (i^!tff));
446 qpel(dmpi->planes[2],
447 mpi->planes[2] + (i^!tff)*mpi->stride[2],
448 mpi->chroma_width, mpi->chroma_height/2,
449 dmpi->stride[2], mpi->stride[2]*2, (i^!tff));
451 ret |= vf_next_put_image(vf, dmpi, calc_pts(pts, i));
452 if (correct_pts)
453 break;
454 else
455 if (!i) vf_extra_flip(vf);
457 break;
459 vf->priv->buffered_i = 1;
460 return ret;
463 static int query_format(struct vf_instance *vf, unsigned int fmt)
465 /* FIXME - figure out which formats exactly work */
466 switch (fmt) {
467 default:
468 if (vf->priv->mode == 1)
469 return 0;
470 case IMGFMT_YV12:
471 case IMGFMT_IYUV:
472 case IMGFMT_I420:
473 return vf_next_query_format(vf, fmt);
475 return 0;
478 static int config(struct vf_instance *vf,
479 int width, int height, int d_width, int d_height,
480 unsigned int flags, unsigned int outfmt)
482 switch (vf->priv->mode) {
483 case 0:
484 case 2:
485 case 3:
486 case 4:
487 return vf_next_config(vf,width,height/2,d_width,d_height,flags,outfmt);
488 case 1:
489 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
491 return 0;
494 static void uninit(struct vf_instance *vf)
496 free(vf->priv);
499 static int vf_open(vf_instance_t *vf, char *args)
501 struct vf_priv_s *p;
502 vf->config = config;
503 vf->put_image = put_image;
504 vf->query_format = query_format;
505 vf->uninit = uninit;
506 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
507 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
508 vf->priv->mode = 4;
509 vf->priv->parity = -1;
510 if (args) sscanf(args, "%d:%d", &vf->priv->mode, &vf->priv->parity);
511 qpel_li = qpel_li_C;
512 qpel_4tap = qpel_4tap_C;
513 #if HAVE_MMX
514 if(gCpuCaps.hasMMX) qpel_li = qpel_li_MMX;
515 #if HAVE_EBX_AVAILABLE
516 if(gCpuCaps.hasMMX) qpel_4tap = qpel_4tap_MMX;
517 #endif
518 #endif
519 #if HAVE_MMX2
520 if(gCpuCaps.hasMMX2) qpel_li = qpel_li_MMX2;
521 #endif
522 #if HAVE_AMD3DNOW
523 if(gCpuCaps.has3DNow) qpel_li = qpel_li_3DNOW;
524 #endif
525 return 1;
528 const vf_info_t vf_info_tfields = {
529 "temporal field separation",
530 "tfields",
531 "Rich Felker",
533 vf_open,
534 NULL