adding the code documentation guide lines
[mplayer/glamo.git] / libmpcodecs / vf_ivtc.c
blob804f68a084414b8dd1808303eb0bd8e31d0b8356
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"
16 struct metrics {
17 /* difference: total, even lines, odd lines */
18 int d, e, o;
19 /* noise: temporal, spacial (current), spacial (past) */
20 int t, s, p;
23 struct frameinfo {
24 /* peak, relative, mean */
25 struct metrics p, r, m;
28 struct vf_priv_s {
29 struct frameinfo fi[2];
30 mp_image_t *dmpi;
31 int first;
32 int drop, lastdrop, dropnext;
33 int inframes, outframes;
36 enum {
37 F_DROP,
38 F_MERGE,
39 F_NEXT,
40 F_SHOW
43 static inline void *my_memcpy_pic(void * dst, void * src, int bytesPerLine, int height, int dstStride, int srcStride)
45 int i;
46 void *retval=dst;
48 for(i=0; i<height; i++)
50 memcpy(dst, src, bytesPerLine);
51 src+= srcStride;
52 dst+= dstStride;
55 return retval;
58 #ifdef HAVE_MMX
59 static void block_diffs_MMX(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
61 int i;
62 short out[24]; // output buffer for the partial metrics from the mmx code
64 asm (
65 "movl $4, %%ecx \n\t"
66 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
67 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
68 "pxor %%mm7, %%mm7 \n\t" // all zeros
70 ".balign 16 \n\t"
71 "1: \n\t"
73 // Even difference
74 "movq (%%esi), %%mm0 \n\t"
75 "movq (%%esi), %%mm2 \n\t"
76 "addl %%eax, %%esi \n\t"
77 "movq (%%edi), %%mm1 \n\t"
78 "addl %%ebx, %%edi \n\t"
79 "psubusb %%mm1, %%mm2 \n\t"
80 "psubusb %%mm0, %%mm1 \n\t"
81 "movq %%mm2, %%mm0 \n\t"
82 "movq %%mm1, %%mm3 \n\t"
83 "punpcklbw %%mm7, %%mm0 \n\t"
84 "punpcklbw %%mm7, %%mm1 \n\t"
85 "punpckhbw %%mm7, %%mm2 \n\t"
86 "punpckhbw %%mm7, %%mm3 \n\t"
87 "paddw %%mm0, %%mm4 \n\t"
88 "paddw %%mm1, %%mm4 \n\t"
89 "paddw %%mm2, %%mm4 \n\t"
90 "paddw %%mm3, %%mm4 \n\t"
92 // Odd difference
93 "movq (%%esi), %%mm0 \n\t"
94 "movq (%%esi), %%mm2 \n\t"
95 "addl %%eax, %%esi \n\t"
96 "movq (%%edi), %%mm1 \n\t"
97 "addl %%ebx, %%edi \n\t"
98 "psubusb %%mm1, %%mm2 \n\t"
99 "psubusb %%mm0, %%mm1 \n\t"
100 "movq %%mm2, %%mm0 \n\t"
101 "movq %%mm1, %%mm3 \n\t"
102 "punpcklbw %%mm7, %%mm0 \n\t"
103 "punpcklbw %%mm7, %%mm1 \n\t"
104 "punpckhbw %%mm7, %%mm2 \n\t"
105 "punpckhbw %%mm7, %%mm3 \n\t"
106 "paddw %%mm0, %%mm5 \n\t"
107 "paddw %%mm1, %%mm5 \n\t"
108 "paddw %%mm2, %%mm5 \n\t"
109 "paddw %%mm3, %%mm5 \n\t"
111 "decl %%ecx \n\t"
112 "jnz 1b \n\t"
113 "movq %%mm4, (%%edx) \n\t"
114 "movq %%mm5, 8(%%edx) \n\t"
116 : "S" (old), "D" (new), "a" (os), "b" (ns), "d" (out)
117 : "memory"
119 m->e = out[0]+out[1]+out[2]+out[3];
120 m->o = out[4]+out[5]+out[6]+out[7];
121 m->d = m->e + m->o;
123 asm (
124 // First loop to measure first four columns
125 "movl $4, %%ecx \n\t"
126 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
127 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
128 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
130 ".balign 16 \n\t"
131 "2: \n\t"
133 "movq (%%esi), %%mm0 \n\t"
134 "movq (%%esi,%%eax), %%mm1 \n\t"
135 "addl %%eax, %%esi \n\t"
136 "addl %%eax, %%esi \n\t"
137 "movq (%%edi), %%mm2 \n\t"
138 "movq (%%edi,%%ebx), %%mm3 \n\t"
139 "addl %%ebx, %%edi \n\t"
140 "addl %%ebx, %%edi \n\t"
141 "punpcklbw %%mm7, %%mm0 \n\t"
142 "punpcklbw %%mm7, %%mm1 \n\t"
143 "punpcklbw %%mm7, %%mm2 \n\t"
144 "punpcklbw %%mm7, %%mm3 \n\t"
145 "paddw %%mm1, %%mm4 \n\t"
146 "paddw %%mm1, %%mm5 \n\t"
147 "paddw %%mm3, %%mm6 \n\t"
148 "psubw %%mm0, %%mm4 \n\t"
149 "psubw %%mm2, %%mm5 \n\t"
150 "psubw %%mm2, %%mm6 \n\t"
152 "decl %%ecx \n\t"
153 "jnz 2b \n\t"
155 "movq %%mm0, %%mm1 \n\t"
156 "movq %%mm0, %%mm2 \n\t"
157 "movq %%mm0, %%mm3 \n\t"
158 "pcmpgtw %%mm4, %%mm1 \n\t"
159 "pcmpgtw %%mm5, %%mm2 \n\t"
160 "pcmpgtw %%mm6, %%mm3 \n\t"
161 "pxor %%mm1, %%mm4 \n\t"
162 "pxor %%mm2, %%mm5 \n\t"
163 "pxor %%mm3, %%mm6 \n\t"
164 "psubw %%mm1, %%mm4 \n\t"
165 "psubw %%mm2, %%mm5 \n\t"
166 "psubw %%mm3, %%mm6 \n\t"
167 "movq %%mm4, (%%edx) \n\t"
168 "movq %%mm5, 16(%%edx) \n\t"
169 "movq %%mm6, 32(%%edx) \n\t"
171 "movl %%eax, %%ecx \n\t"
172 "shll $3, %%ecx \n\t"
173 "subl %%ecx, %%esi \n\t"
174 "movl %%ebx, %%ecx \n\t"
175 "shll $3, %%ecx \n\t"
176 "subl %%ecx, %%edi \n\t"
178 // Second loop for the last four columns
179 "movl $4, %%ecx \n\t"
180 "pxor %%mm4, %%mm4 \n\t"
181 "pxor %%mm5, %%mm5 \n\t"
182 "pxor %%mm6, %%mm6 \n\t"
184 ".balign 16 \n\t"
185 "3: \n\t"
187 "movq (%%esi), %%mm0 \n\t"
188 "movq (%%esi,%%eax), %%mm1 \n\t"
189 "addl %%eax, %%esi \n\t"
190 "addl %%eax, %%esi \n\t"
191 "movq (%%edi), %%mm2 \n\t"
192 "movq (%%edi,%%ebx), %%mm3 \n\t"
193 "addl %%ebx, %%edi \n\t"
194 "addl %%ebx, %%edi \n\t"
195 "punpckhbw %%mm7, %%mm0 \n\t"
196 "punpckhbw %%mm7, %%mm1 \n\t"
197 "punpckhbw %%mm7, %%mm2 \n\t"
198 "punpckhbw %%mm7, %%mm3 \n\t"
199 "paddw %%mm1, %%mm4 \n\t"
200 "paddw %%mm1, %%mm5 \n\t"
201 "paddw %%mm3, %%mm6 \n\t"
202 "psubw %%mm0, %%mm4 \n\t"
203 "psubw %%mm2, %%mm5 \n\t"
204 "psubw %%mm2, %%mm6 \n\t"
206 "decl %%ecx \n\t"
207 "jnz 3b \n\t"
209 "movq %%mm0, %%mm1 \n\t"
210 "movq %%mm0, %%mm2 \n\t"
211 "movq %%mm0, %%mm3 \n\t"
212 "pcmpgtw %%mm4, %%mm1 \n\t"
213 "pcmpgtw %%mm5, %%mm2 \n\t"
214 "pcmpgtw %%mm6, %%mm3 \n\t"
215 "pxor %%mm1, %%mm4 \n\t"
216 "pxor %%mm2, %%mm5 \n\t"
217 "pxor %%mm3, %%mm6 \n\t"
218 "psubw %%mm1, %%mm4 \n\t"
219 "psubw %%mm2, %%mm5 \n\t"
220 "psubw %%mm3, %%mm6 \n\t"
221 "movq %%mm4, 8(%%edx) \n\t"
222 "movq %%mm5, 24(%%edx) \n\t"
223 "movq %%mm6, 40(%%edx) \n\t"
225 "emms \n\t"
227 : "S" (old), "D" (new), "a" (os), "b" (ns), "d" (out)
228 : "memory"
230 m->p = m->t = m->s = 0;
231 for (i=0; i<8; i++) {
232 m->p += out[i];
233 m->t += out[8+i];
234 m->s += out[16+i];
236 //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);
238 #endif
240 //#define MAG(a) ((a)*(a))
241 //#define MAG(a) (abs(a))
242 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
244 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
245 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
246 #define LOWPASS(s) ((s)[0])
249 static void block_diffs_C(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
251 int x, y, e=0, o=0, s=0, p=0, t=0;
252 unsigned char *oldp, *newp;
253 m->s = m->p = m->t = 0;
254 for (x = 8; x; x--) {
255 oldp = old++;
256 newp = new++;
257 s = p = t = 0;
258 for (y = 4; y; y--) {
259 e += MAG(newp[0]-oldp[0]);
260 o += MAG(newp[ns]-oldp[os]);
261 s += newp[ns]-newp[0];
262 p += oldp[os]-oldp[0];
263 t += oldp[os]-newp[0];
264 oldp += os<<1;
265 newp += ns<<1;
267 m->s += MAG(s);
268 m->p += MAG(p);
269 m->t += MAG(t);
271 m->e = e;
272 m->o = o;
273 m->d = e+o;
276 static void (*block_diffs)(struct metrics *, unsigned char *, unsigned char *, int, int);
278 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
280 static void diff_planes(struct frameinfo *fi,
281 unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
283 int x, y;
284 struct metrics l;
285 struct metrics *peak=&fi->p, *rel=&fi->r, *mean=&fi->m;
286 memset(peak, 0, sizeof(struct metrics));
287 memset(rel, 0, sizeof(struct metrics));
288 memset(mean, 0, sizeof(struct metrics));
289 for (y = 0; y < h-7; y += 8) {
290 for (x = 8; x < w-8-7; x += 8) {
291 block_diffs(&l, old+x+y*os, new+x+y*ns, os, ns);
292 mean->d += l.d;
293 mean->e += l.e;
294 mean->o += l.o;
295 mean->s += l.s;
296 mean->p += l.p;
297 mean->t += l.t;
298 MAXUP(peak->d, l.d);
299 MAXUP(peak->e, l.e);
300 MAXUP(peak->o, l.o);
301 MAXUP(peak->s, l.s);
302 MAXUP(peak->p, l.p);
303 MAXUP(peak->t, l.t);
304 MAXUP(rel->e, l.e-l.o);
305 MAXUP(rel->o, l.o-l.e);
306 MAXUP(rel->s, l.s-l.t);
307 MAXUP(rel->p, l.p-l.t);
308 MAXUP(rel->t, l.t-l.p);
309 MAXUP(rel->d, l.t-l.s); /* hack */
312 x = (w/8-2)*(h/8);
313 mean->d /= x;
314 mean->e /= x;
315 mean->o /= x;
316 mean->s /= x;
317 mean->p /= x;
318 mean->t /= x;
321 static void diff_fields(struct frameinfo *fi, mp_image_t *old, mp_image_t *new)
323 diff_planes(fi, old->planes[0], new->planes[0],
324 new->w, new->h, old->stride[0], new->stride[0]);
327 static void stats(struct frameinfo *f)
329 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",
330 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);
333 static int foo(struct vf_priv_s *p, mp_image_t *new, mp_image_t *cur)
335 struct frameinfo *f = p->fi;
337 f[0] = f[1];
338 diff_fields(&f[1], cur, new);
339 stats(&f[1]);
341 // Immediately drop this frame if it's already been used.
342 if (p->dropnext) {
343 p->dropnext = 0;
344 return F_DROP;
347 // Sometimes a pulldown frame comes all by itself, so both
348 // its top and bottom field are duplicates from the adjacent
349 // two frames. We can just drop such a frame, but we
350 // immediately show the next frame instead to keep the frame
351 // drops evenly spaced during normal 3:2 pulldown sequences.
352 if ((3*f[1].r.o < f[1].r.e) && (f[1].r.s < f[1].r.d)) {
353 p->dropnext = 1;
354 return F_NEXT;
357 // If none of these conditions hold, we will consider the frame
358 // progressive and just show it as-is.
359 if (!( (3*f[0].r.e < f[0].r.o) ||
360 ((2*f[0].r.d < f[0].r.s) && (f[0].r.s > 1200)) ||
361 ((2*f[1].r.t < f[1].r.p) && (f[1].r.p > 1200)) ))
362 return F_SHOW;
364 // Otherwise, we have to decide whether to merge or drop.
365 // If the noise metric only increases minimally, we're off
366 // to a good start...
367 if (((2*f[1].r.t < 3*f[1].r.p) && (f[1].r.t < 3600)) ||
368 (f[1].r.t < 900) || (f[1].r.d < 900)) {
369 // ...and if noise decreases or the duplicate even field
370 // is detected, we go ahead with the merge.
371 if ((3*f[0].r.e < f[0].r.o) || (2*f[1].r.t < f[1].r.p)) {
372 p->dropnext = 1;
373 return F_MERGE;
376 return F_DROP;
381 static void copy_image(mp_image_t *dmpi, mp_image_t *mpi, int field)
383 switch (field) {
384 case 0:
385 my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
386 dmpi->stride[0]*2, mpi->stride[0]*2);
387 if (mpi->flags & MP_IMGFLAG_PLANAR) {
388 my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
389 mpi->chroma_width, mpi->chroma_height/2,
390 dmpi->stride[1]*2, mpi->stride[1]*2);
391 my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
392 mpi->chroma_width, mpi->chroma_height/2,
393 dmpi->stride[2]*2, mpi->stride[2]*2);
395 break;
396 case 1:
397 my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
398 mpi->planes[0]+mpi->stride[0], mpi->w, mpi->h/2,
399 dmpi->stride[0]*2, mpi->stride[0]*2);
400 if (mpi->flags & MP_IMGFLAG_PLANAR) {
401 my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
402 mpi->planes[1]+mpi->stride[1],
403 mpi->chroma_width, mpi->chroma_height/2,
404 dmpi->stride[1]*2, mpi->stride[1]*2);
405 my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
406 mpi->planes[2]+mpi->stride[2],
407 mpi->chroma_width, mpi->chroma_height/2,
408 dmpi->stride[2]*2, mpi->stride[2]*2);
410 break;
411 case 2:
412 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
413 dmpi->stride[0], mpi->stride[0]);
414 if (mpi->flags & MP_IMGFLAG_PLANAR) {
415 memcpy_pic(dmpi->planes[1], mpi->planes[1],
416 mpi->chroma_width, mpi->chroma_height,
417 dmpi->stride[1], mpi->stride[1]);
418 memcpy_pic(dmpi->planes[2], mpi->planes[2],
419 mpi->chroma_width, mpi->chroma_height,
420 dmpi->stride[2], mpi->stride[2]);
422 break;
426 static int do_put_image(struct vf_instance_s* vf, mp_image_t *dmpi)
428 struct vf_priv_s *p = vf->priv;
429 int dropflag;
431 switch (p->drop && !p->dropnext) {
432 case 0:
433 dropflag = 0;
434 break;
435 case 1:
436 dropflag = (++p->lastdrop >= 5);
437 break;
438 case 2:
439 dropflag = (++p->lastdrop >= 5) && (4*p->inframes <= 5*p->outframes);
440 break;
443 if (dropflag) {
444 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
445 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
446 mp_msg(MSGT_VFILTER, MSGL_V, "!");
447 p->lastdrop = 0;
448 return 0;
451 p->outframes++;
452 return vf_next_put_image(vf, dmpi);
455 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi)
457 int ret=0;
458 struct vf_priv_s *p = vf->priv;
460 p->inframes++;
462 if (p->first) { /* hack */
463 p->first = 0;
464 return 1;
467 if (!p->dmpi) p->dmpi = vf_get_image(vf->next, mpi->imgfmt,
468 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
469 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
470 mpi->width, mpi->height);
471 /* FIXME -- not correct, off by one frame! */
472 p->dmpi->qscale = mpi->qscale;
473 p->dmpi->qstride = mpi->qstride;
474 p->dmpi->qscale_type = mpi->qscale_type;
476 switch (foo(p, mpi, p->dmpi)) {
477 case F_DROP:
478 copy_image(p->dmpi, mpi, 2);
479 ret = 0;
480 p->lastdrop = 0;
481 mp_msg(MSGT_VFILTER, MSGL_V, "DROP\n");
482 break;
483 case F_MERGE:
484 copy_image(p->dmpi, mpi, 0);
485 ret = do_put_image(vf, p->dmpi);
486 copy_image(p->dmpi, mpi, 1);
487 mp_msg(MSGT_VFILTER, MSGL_V, "MERGE\n");
488 p->dmpi = NULL;
489 break;
490 case F_NEXT:
491 copy_image(p->dmpi, mpi, 2);
492 ret = do_put_image(vf, p->dmpi);
493 mp_msg(MSGT_VFILTER, MSGL_V, "NEXT\n");
494 p->dmpi = NULL;
495 break;
496 case F_SHOW:
497 ret = do_put_image(vf, p->dmpi);
498 copy_image(p->dmpi, mpi, 2);
499 mp_msg(MSGT_VFILTER, MSGL_V, "OK\n");
500 p->dmpi = NULL;
501 break;
503 return ret;
506 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
508 switch (fmt) {
509 case IMGFMT_YV12:
510 case IMGFMT_IYUV:
511 case IMGFMT_I420:
512 return vf_next_query_format(vf, fmt);
514 return 0;
517 static void uninit(struct vf_instance_s* vf)
519 free(vf->priv);
522 static int open(vf_instance_t *vf, char* args)
524 struct vf_priv_s *p;
525 vf->put_image = put_image;
526 vf->query_format = query_format;
527 vf->uninit = uninit;
528 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
529 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
530 p->drop = 0;
531 p->first = 1;
532 if (args) sscanf(args, "%d", &p->drop);
533 block_diffs = block_diffs_C;
534 #ifdef HAVE_MMX
535 if(gCpuCaps.hasMMX) block_diffs = block_diffs_MMX;
536 #endif
537 return 1;
540 vf_info_t vf_info_ivtc = {
541 "inverse telecine, take 2",
542 "ivtc",
543 "Rich Felker",
545 open,
546 NULL