typo fixes
[mplayer/greg.git] / libmpcodecs / vf_ivtc.c
blob50cabe0ee1c67b157d2024831b230f1a79d277f2
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"
8 #include "asmalign.h"
10 #include "img_format.h"
11 #include "mp_image.h"
12 #include "vf.h"
14 #include "libvo/fastmemcpy.h"
17 struct metrics {
18 /* difference: total, even lines, odd lines */
19 int d, e, o;
20 /* noise: temporal, spacial (current), spacial (past) */
21 int t, s, p;
24 struct frameinfo {
25 /* peak, relative, mean */
26 struct metrics p, r, m;
29 struct vf_priv_s {
30 struct frameinfo fi[2];
31 mp_image_t *dmpi;
32 int first;
33 int drop, lastdrop, dropnext;
34 int inframes, outframes;
37 enum {
38 F_DROP,
39 F_MERGE,
40 F_NEXT,
41 F_SHOW
44 static inline void *my_memcpy_pic(void * dst, void * src, int bytesPerLine, int height, int dstStride, int srcStride)
46 int i;
47 void *retval=dst;
49 for(i=0; i<height; i++)
51 memcpy(dst, src, bytesPerLine);
52 src+= srcStride;
53 dst+= dstStride;
56 return retval;
59 #ifdef HAVE_MMX
60 static void block_diffs_MMX(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
62 int i;
63 short out[24]; // output buffer for the partial metrics from the mmx code
65 asm (
66 "movl $4, %%ecx \n\t"
67 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
68 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
69 "pxor %%mm7, %%mm7 \n\t" // all zeros
71 ASMALIGN16
72 "1: \n\t"
74 // Even difference
75 "movq (%%"REG_S"), %%mm0 \n\t"
76 "movq (%%"REG_S"), %%mm2 \n\t"
77 "add %%"REG_a", %%"REG_S" \n\t"
78 "movq (%%"REG_D"), %%mm1 \n\t"
79 "add %%"REG_b", %%"REG_D" \n\t"
80 "psubusb %%mm1, %%mm2 \n\t"
81 "psubusb %%mm0, %%mm1 \n\t"
82 "movq %%mm2, %%mm0 \n\t"
83 "movq %%mm1, %%mm3 \n\t"
84 "punpcklbw %%mm7, %%mm0 \n\t"
85 "punpcklbw %%mm7, %%mm1 \n\t"
86 "punpckhbw %%mm7, %%mm2 \n\t"
87 "punpckhbw %%mm7, %%mm3 \n\t"
88 "paddw %%mm0, %%mm4 \n\t"
89 "paddw %%mm1, %%mm4 \n\t"
90 "paddw %%mm2, %%mm4 \n\t"
91 "paddw %%mm3, %%mm4 \n\t"
93 // Odd difference
94 "movq (%%"REG_S"), %%mm0 \n\t"
95 "movq (%%"REG_S"), %%mm2 \n\t"
96 "add %%"REG_a", %%"REG_S" \n\t"
97 "movq (%%"REG_D"), %%mm1 \n\t"
98 "add %%"REG_b", %%"REG_D" \n\t"
99 "psubusb %%mm1, %%mm2 \n\t"
100 "psubusb %%mm0, %%mm1 \n\t"
101 "movq %%mm2, %%mm0 \n\t"
102 "movq %%mm1, %%mm3 \n\t"
103 "punpcklbw %%mm7, %%mm0 \n\t"
104 "punpcklbw %%mm7, %%mm1 \n\t"
105 "punpckhbw %%mm7, %%mm2 \n\t"
106 "punpckhbw %%mm7, %%mm3 \n\t"
107 "paddw %%mm0, %%mm5 \n\t"
108 "paddw %%mm1, %%mm5 \n\t"
109 "paddw %%mm2, %%mm5 \n\t"
110 "paddw %%mm3, %%mm5 \n\t"
112 "decl %%ecx \n\t"
113 "jnz 1b \n\t"
114 "movq %%mm4, (%%"REG_d") \n\t"
115 "movq %%mm5, 8(%%"REG_d") \n\t"
117 : "S" (old), "D" (new), "a" (os), "b" (ns), "d" (out)
118 : "memory"
120 m->e = out[0]+out[1]+out[2]+out[3];
121 m->o = out[4]+out[5]+out[6]+out[7];
122 m->d = m->e + m->o;
124 asm (
125 // First loop to measure first four columns
126 "movl $4, %%ecx \n\t"
127 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
128 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
129 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
131 ASMALIGN16
132 "2: \n\t"
134 "movq (%%"REG_S"), %%mm0 \n\t"
135 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
136 "add %%"REG_a", %%"REG_S" \n\t"
137 "add %%"REG_a", %%"REG_S" \n\t"
138 "movq (%%"REG_D"), %%mm2 \n\t"
139 "movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
140 "add %%"REG_b", %%"REG_D" \n\t"
141 "add %%"REG_b", %%"REG_D" \n\t"
142 "punpcklbw %%mm7, %%mm0 \n\t"
143 "punpcklbw %%mm7, %%mm1 \n\t"
144 "punpcklbw %%mm7, %%mm2 \n\t"
145 "punpcklbw %%mm7, %%mm3 \n\t"
146 "paddw %%mm1, %%mm4 \n\t"
147 "paddw %%mm1, %%mm5 \n\t"
148 "paddw %%mm3, %%mm6 \n\t"
149 "psubw %%mm0, %%mm4 \n\t"
150 "psubw %%mm2, %%mm5 \n\t"
151 "psubw %%mm2, %%mm6 \n\t"
153 "decl %%ecx \n\t"
154 "jnz 2b \n\t"
156 "movq %%mm0, %%mm1 \n\t"
157 "movq %%mm0, %%mm2 \n\t"
158 "movq %%mm0, %%mm3 \n\t"
159 "pcmpgtw %%mm4, %%mm1 \n\t"
160 "pcmpgtw %%mm5, %%mm2 \n\t"
161 "pcmpgtw %%mm6, %%mm3 \n\t"
162 "pxor %%mm1, %%mm4 \n\t"
163 "pxor %%mm2, %%mm5 \n\t"
164 "pxor %%mm3, %%mm6 \n\t"
165 "psubw %%mm1, %%mm4 \n\t"
166 "psubw %%mm2, %%mm5 \n\t"
167 "psubw %%mm3, %%mm6 \n\t"
168 "movq %%mm4, (%%"REG_d") \n\t"
169 "movq %%mm5, 16(%%"REG_d") \n\t"
170 "movq %%mm6, 32(%%"REG_d") \n\t"
172 "mov %%"REG_a", %%"REG_c" \n\t"
173 "shl $3, %%"REG_c" \n\t"
174 "sub %%"REG_c", %%"REG_S" \n\t"
175 "mov %%"REG_b", %%"REG_c" \n\t"
176 "shl $3, %%"REG_c" \n\t"
177 "sub %%"REG_c", %%"REG_D" \n\t"
179 // Second loop for the last four columns
180 "movl $4, %%ecx \n\t"
181 "pxor %%mm4, %%mm4 \n\t"
182 "pxor %%mm5, %%mm5 \n\t"
183 "pxor %%mm6, %%mm6 \n\t"
185 ASMALIGN16
186 "3: \n\t"
188 "movq (%%"REG_S"), %%mm0 \n\t"
189 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
190 "add %%"REG_a", %%"REG_S" \n\t"
191 "add %%"REG_a", %%"REG_S" \n\t"
192 "movq (%%"REG_D"), %%mm2 \n\t"
193 "movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
194 "add %%"REG_b", %%"REG_D" \n\t"
195 "add %%"REG_b", %%"REG_D" \n\t"
196 "punpckhbw %%mm7, %%mm0 \n\t"
197 "punpckhbw %%mm7, %%mm1 \n\t"
198 "punpckhbw %%mm7, %%mm2 \n\t"
199 "punpckhbw %%mm7, %%mm3 \n\t"
200 "paddw %%mm1, %%mm4 \n\t"
201 "paddw %%mm1, %%mm5 \n\t"
202 "paddw %%mm3, %%mm6 \n\t"
203 "psubw %%mm0, %%mm4 \n\t"
204 "psubw %%mm2, %%mm5 \n\t"
205 "psubw %%mm2, %%mm6 \n\t"
207 "decl %%ecx \n\t"
208 "jnz 3b \n\t"
210 "movq %%mm0, %%mm1 \n\t"
211 "movq %%mm0, %%mm2 \n\t"
212 "movq %%mm0, %%mm3 \n\t"
213 "pcmpgtw %%mm4, %%mm1 \n\t"
214 "pcmpgtw %%mm5, %%mm2 \n\t"
215 "pcmpgtw %%mm6, %%mm3 \n\t"
216 "pxor %%mm1, %%mm4 \n\t"
217 "pxor %%mm2, %%mm5 \n\t"
218 "pxor %%mm3, %%mm6 \n\t"
219 "psubw %%mm1, %%mm4 \n\t"
220 "psubw %%mm2, %%mm5 \n\t"
221 "psubw %%mm3, %%mm6 \n\t"
222 "movq %%mm4, 8(%%"REG_d") \n\t"
223 "movq %%mm5, 24(%%"REG_d") \n\t"
224 "movq %%mm6, 40(%%"REG_d") \n\t"
226 "emms \n\t"
228 : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
229 : "memory"
231 m->p = m->t = m->s = 0;
232 for (i=0; i<8; i++) {
233 m->p += out[i];
234 m->t += out[8+i];
235 m->s += out[16+i];
237 //printf("e=%d o=%d d=%d p=%d t=%d s=%d\n", m->e, m->o, m->d, m->p, m->t, m->s);
239 #endif
241 //#define MAG(a) ((a)*(a))
242 //#define MAG(a) (abs(a))
243 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
245 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
246 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
247 #define LOWPASS(s) ((s)[0])
250 static void block_diffs_C(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
252 int x, y, e=0, o=0, s=0, p=0, t=0;
253 unsigned char *oldp, *newp;
254 m->s = m->p = m->t = 0;
255 for (x = 8; x; x--) {
256 oldp = old++;
257 newp = new++;
258 s = p = t = 0;
259 for (y = 4; y; y--) {
260 e += MAG(newp[0]-oldp[0]);
261 o += MAG(newp[ns]-oldp[os]);
262 s += newp[ns]-newp[0];
263 p += oldp[os]-oldp[0];
264 t += oldp[os]-newp[0];
265 oldp += os<<1;
266 newp += ns<<1;
268 m->s += MAG(s);
269 m->p += MAG(p);
270 m->t += MAG(t);
272 m->e = e;
273 m->o = o;
274 m->d = e+o;
277 static void (*block_diffs)(struct metrics *, unsigned char *, unsigned char *, int, int);
279 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
281 static void diff_planes(struct frameinfo *fi,
282 unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
284 int x, y;
285 struct metrics l;
286 struct metrics *peak=&fi->p, *rel=&fi->r, *mean=&fi->m;
287 memset(peak, 0, sizeof(struct metrics));
288 memset(rel, 0, sizeof(struct metrics));
289 memset(mean, 0, sizeof(struct metrics));
290 for (y = 0; y < h-7; y += 8) {
291 for (x = 8; x < w-8-7; x += 8) {
292 block_diffs(&l, old+x+y*os, new+x+y*ns, os, ns);
293 mean->d += l.d;
294 mean->e += l.e;
295 mean->o += l.o;
296 mean->s += l.s;
297 mean->p += l.p;
298 mean->t += l.t;
299 MAXUP(peak->d, l.d);
300 MAXUP(peak->e, l.e);
301 MAXUP(peak->o, l.o);
302 MAXUP(peak->s, l.s);
303 MAXUP(peak->p, l.p);
304 MAXUP(peak->t, l.t);
305 MAXUP(rel->e, l.e-l.o);
306 MAXUP(rel->o, l.o-l.e);
307 MAXUP(rel->s, l.s-l.t);
308 MAXUP(rel->p, l.p-l.t);
309 MAXUP(rel->t, l.t-l.p);
310 MAXUP(rel->d, l.t-l.s); /* hack */
313 x = (w/8-2)*(h/8);
314 mean->d /= x;
315 mean->e /= x;
316 mean->o /= x;
317 mean->s /= x;
318 mean->p /= x;
319 mean->t /= x;
322 static void diff_fields(struct frameinfo *fi, mp_image_t *old, mp_image_t *new)
324 diff_planes(fi, old->planes[0], new->planes[0],
325 new->w, new->h, old->stride[0], new->stride[0]);
328 static void stats(struct frameinfo *f)
330 mp_msg(MSGT_VFILTER, MSGL_V, " pd=%d re=%d ro=%d rp=%d rt=%d rs=%d rd=%d pp=%d pt=%d ps=%d\r",
331 f->p.d, f->r.e, f->r.o, f->r.p, f->r.t, f->r.s, f->r.d, f->p.p, f->p.t, f->p.s);
334 static int foo(struct vf_priv_s *p, mp_image_t *new, mp_image_t *cur)
336 struct frameinfo *f = p->fi;
338 f[0] = f[1];
339 diff_fields(&f[1], cur, new);
340 stats(&f[1]);
342 // Immediately drop this frame if it's already been used.
343 if (p->dropnext) {
344 p->dropnext = 0;
345 return F_DROP;
348 // Sometimes a pulldown frame comes all by itself, so both
349 // its top and bottom field are duplicates from the adjacent
350 // two frames. We can just drop such a frame, but we
351 // immediately show the next frame instead to keep the frame
352 // drops evenly spaced during normal 3:2 pulldown sequences.
353 if ((3*f[1].r.o < f[1].r.e) && (f[1].r.s < f[1].r.d)) {
354 p->dropnext = 1;
355 return F_NEXT;
358 // If none of these conditions hold, we will consider the frame
359 // progressive and just show it as-is.
360 if (!( (3*f[0].r.e < f[0].r.o) ||
361 ((2*f[0].r.d < f[0].r.s) && (f[0].r.s > 1200)) ||
362 ((2*f[1].r.t < f[1].r.p) && (f[1].r.p > 1200)) ))
363 return F_SHOW;
365 // Otherwise, we have to decide whether to merge or drop.
366 // If the noise metric only increases minimally, we're off
367 // to a good start...
368 if (((2*f[1].r.t < 3*f[1].r.p) && (f[1].r.t < 3600)) ||
369 (f[1].r.t < 900) || (f[1].r.d < 900)) {
370 // ...and if noise decreases or the duplicate even field
371 // is detected, we go ahead with the merge.
372 if ((3*f[0].r.e < f[0].r.o) || (2*f[1].r.t < f[1].r.p)) {
373 p->dropnext = 1;
374 return F_MERGE;
377 return F_DROP;
382 static void copy_image(mp_image_t *dmpi, mp_image_t *mpi, int field)
384 switch (field) {
385 case 0:
386 my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
387 dmpi->stride[0]*2, mpi->stride[0]*2);
388 if (mpi->flags & MP_IMGFLAG_PLANAR) {
389 my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
390 mpi->chroma_width, mpi->chroma_height/2,
391 dmpi->stride[1]*2, mpi->stride[1]*2);
392 my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
393 mpi->chroma_width, mpi->chroma_height/2,
394 dmpi->stride[2]*2, mpi->stride[2]*2);
396 break;
397 case 1:
398 my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
399 mpi->planes[0]+mpi->stride[0], mpi->w, mpi->h/2,
400 dmpi->stride[0]*2, mpi->stride[0]*2);
401 if (mpi->flags & MP_IMGFLAG_PLANAR) {
402 my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
403 mpi->planes[1]+mpi->stride[1],
404 mpi->chroma_width, mpi->chroma_height/2,
405 dmpi->stride[1]*2, mpi->stride[1]*2);
406 my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
407 mpi->planes[2]+mpi->stride[2],
408 mpi->chroma_width, mpi->chroma_height/2,
409 dmpi->stride[2]*2, mpi->stride[2]*2);
411 break;
412 case 2:
413 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
414 dmpi->stride[0], mpi->stride[0]);
415 if (mpi->flags & MP_IMGFLAG_PLANAR) {
416 memcpy_pic(dmpi->planes[1], mpi->planes[1],
417 mpi->chroma_width, mpi->chroma_height,
418 dmpi->stride[1], mpi->stride[1]);
419 memcpy_pic(dmpi->planes[2], mpi->planes[2],
420 mpi->chroma_width, mpi->chroma_height,
421 dmpi->stride[2], mpi->stride[2]);
423 break;
427 static int do_put_image(struct vf_instance_s* vf, mp_image_t *dmpi)
429 struct vf_priv_s *p = vf->priv;
430 int dropflag=0;
432 if (!p->dropnext) switch (p->drop) {
433 case 0:
434 dropflag = 0;
435 break;
436 case 1:
437 dropflag = (++p->lastdrop >= 5);
438 break;
439 case 2:
440 dropflag = (++p->lastdrop >= 5) && (4*p->inframes <= 5*p->outframes);
441 break;
444 if (dropflag) {
445 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
446 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
447 mp_msg(MSGT_VFILTER, MSGL_V, "!");
448 p->lastdrop = 0;
449 return 0;
452 p->outframes++;
453 return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
456 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
458 int ret=0;
459 struct vf_priv_s *p = vf->priv;
461 p->inframes++;
463 if (p->first) { /* hack */
464 p->first = 0;
465 return 1;
468 if (!p->dmpi) p->dmpi = vf_get_image(vf->next, mpi->imgfmt,
469 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
470 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
471 mpi->width, mpi->height);
472 /* FIXME -- not correct, off by one frame! */
473 p->dmpi->qscale = mpi->qscale;
474 p->dmpi->qstride = mpi->qstride;
475 p->dmpi->qscale_type = mpi->qscale_type;
477 switch (foo(p, mpi, p->dmpi)) {
478 case F_DROP:
479 copy_image(p->dmpi, mpi, 2);
480 ret = 0;
481 p->lastdrop = 0;
482 mp_msg(MSGT_VFILTER, MSGL_V, "DROP\n");
483 break;
484 case F_MERGE:
485 copy_image(p->dmpi, mpi, 0);
486 ret = do_put_image(vf, p->dmpi);
487 copy_image(p->dmpi, mpi, 1);
488 mp_msg(MSGT_VFILTER, MSGL_V, "MERGE\n");
489 p->dmpi = NULL;
490 break;
491 case F_NEXT:
492 copy_image(p->dmpi, mpi, 2);
493 ret = do_put_image(vf, p->dmpi);
494 mp_msg(MSGT_VFILTER, MSGL_V, "NEXT\n");
495 p->dmpi = NULL;
496 break;
497 case F_SHOW:
498 ret = do_put_image(vf, p->dmpi);
499 copy_image(p->dmpi, mpi, 2);
500 mp_msg(MSGT_VFILTER, MSGL_V, "OK\n");
501 p->dmpi = NULL;
502 break;
504 return ret;
507 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
509 switch (fmt) {
510 case IMGFMT_YV12:
511 case IMGFMT_IYUV:
512 case IMGFMT_I420:
513 return vf_next_query_format(vf, fmt);
515 return 0;
518 static void uninit(struct vf_instance_s* vf)
520 free(vf->priv);
523 static int open(vf_instance_t *vf, char* args)
525 struct vf_priv_s *p;
526 vf->put_image = put_image;
527 vf->query_format = query_format;
528 vf->uninit = uninit;
529 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
530 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
531 p->drop = 0;
532 p->first = 1;
533 if (args) sscanf(args, "%d", &p->drop);
534 block_diffs = block_diffs_C;
535 #ifdef HAVE_MMX
536 if(gCpuCaps.hasMMX) block_diffs = block_diffs_MMX;
537 #endif
538 return 1;
541 vf_info_t vf_info_ivtc = {
542 "inverse telecine, take 2",
543 "ivtc",
544 "Rich Felker",
546 open,
547 NULL