Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_ivtc.c
blobb6db4c18a83e32c088009c38e0c7d4b8ff15da3b
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 #ifdef HAVE_MMX
44 static void block_diffs_MMX(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
46 int i;
47 short out[24]; // output buffer for the partial metrics from the mmx code
49 asm (
50 "movl $4, %%ecx \n\t"
51 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
52 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
53 "pxor %%mm7, %%mm7 \n\t" // all zeros
55 ASMALIGN(4)
56 "1: \n\t"
58 // Even difference
59 "movq (%%"REG_S"), %%mm0 \n\t"
60 "movq (%%"REG_S"), %%mm2 \n\t"
61 "add %%"REG_a", %%"REG_S" \n\t"
62 "movq (%%"REG_D"), %%mm1 \n\t"
63 "add %%"REG_b", %%"REG_D" \n\t"
64 "psubusb %%mm1, %%mm2 \n\t"
65 "psubusb %%mm0, %%mm1 \n\t"
66 "movq %%mm2, %%mm0 \n\t"
67 "movq %%mm1, %%mm3 \n\t"
68 "punpcklbw %%mm7, %%mm0 \n\t"
69 "punpcklbw %%mm7, %%mm1 \n\t"
70 "punpckhbw %%mm7, %%mm2 \n\t"
71 "punpckhbw %%mm7, %%mm3 \n\t"
72 "paddw %%mm0, %%mm4 \n\t"
73 "paddw %%mm1, %%mm4 \n\t"
74 "paddw %%mm2, %%mm4 \n\t"
75 "paddw %%mm3, %%mm4 \n\t"
77 // Odd difference
78 "movq (%%"REG_S"), %%mm0 \n\t"
79 "movq (%%"REG_S"), %%mm2 \n\t"
80 "add %%"REG_a", %%"REG_S" \n\t"
81 "movq (%%"REG_D"), %%mm1 \n\t"
82 "add %%"REG_b", %%"REG_D" \n\t"
83 "psubusb %%mm1, %%mm2 \n\t"
84 "psubusb %%mm0, %%mm1 \n\t"
85 "movq %%mm2, %%mm0 \n\t"
86 "movq %%mm1, %%mm3 \n\t"
87 "punpcklbw %%mm7, %%mm0 \n\t"
88 "punpcklbw %%mm7, %%mm1 \n\t"
89 "punpckhbw %%mm7, %%mm2 \n\t"
90 "punpckhbw %%mm7, %%mm3 \n\t"
91 "paddw %%mm0, %%mm5 \n\t"
92 "paddw %%mm1, %%mm5 \n\t"
93 "paddw %%mm2, %%mm5 \n\t"
94 "paddw %%mm3, %%mm5 \n\t"
96 "decl %%ecx \n\t"
97 "jnz 1b \n\t"
98 "movq %%mm4, (%%"REG_d") \n\t"
99 "movq %%mm5, 8(%%"REG_d") \n\t"
101 : "S" (old), "D" (new), "a" (os), "b" (ns), "d" (out)
102 : "memory"
104 m->e = out[0]+out[1]+out[2]+out[3];
105 m->o = out[4]+out[5]+out[6]+out[7];
106 m->d = m->e + m->o;
108 asm (
109 // First loop to measure first four columns
110 "movl $4, %%ecx \n\t"
111 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
112 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
113 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
115 ASMALIGN(4)
116 "2: \n\t"
118 "movq (%%"REG_S"), %%mm0 \n\t"
119 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
120 "add %%"REG_a", %%"REG_S" \n\t"
121 "add %%"REG_a", %%"REG_S" \n\t"
122 "movq (%%"REG_D"), %%mm2 \n\t"
123 "movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
124 "add %%"REG_b", %%"REG_D" \n\t"
125 "add %%"REG_b", %%"REG_D" \n\t"
126 "punpcklbw %%mm7, %%mm0 \n\t"
127 "punpcklbw %%mm7, %%mm1 \n\t"
128 "punpcklbw %%mm7, %%mm2 \n\t"
129 "punpcklbw %%mm7, %%mm3 \n\t"
130 "paddw %%mm1, %%mm4 \n\t"
131 "paddw %%mm1, %%mm5 \n\t"
132 "paddw %%mm3, %%mm6 \n\t"
133 "psubw %%mm0, %%mm4 \n\t"
134 "psubw %%mm2, %%mm5 \n\t"
135 "psubw %%mm2, %%mm6 \n\t"
137 "decl %%ecx \n\t"
138 "jnz 2b \n\t"
140 "movq %%mm0, %%mm1 \n\t"
141 "movq %%mm0, %%mm2 \n\t"
142 "movq %%mm0, %%mm3 \n\t"
143 "pcmpgtw %%mm4, %%mm1 \n\t"
144 "pcmpgtw %%mm5, %%mm2 \n\t"
145 "pcmpgtw %%mm6, %%mm3 \n\t"
146 "pxor %%mm1, %%mm4 \n\t"
147 "pxor %%mm2, %%mm5 \n\t"
148 "pxor %%mm3, %%mm6 \n\t"
149 "psubw %%mm1, %%mm4 \n\t"
150 "psubw %%mm2, %%mm5 \n\t"
151 "psubw %%mm3, %%mm6 \n\t"
152 "movq %%mm4, (%%"REG_d") \n\t"
153 "movq %%mm5, 16(%%"REG_d") \n\t"
154 "movq %%mm6, 32(%%"REG_d") \n\t"
156 "mov %%"REG_a", %%"REG_c" \n\t"
157 "shl $3, %%"REG_c" \n\t"
158 "sub %%"REG_c", %%"REG_S" \n\t"
159 "mov %%"REG_b", %%"REG_c" \n\t"
160 "shl $3, %%"REG_c" \n\t"
161 "sub %%"REG_c", %%"REG_D" \n\t"
163 // Second loop for the last four columns
164 "movl $4, %%ecx \n\t"
165 "pxor %%mm4, %%mm4 \n\t"
166 "pxor %%mm5, %%mm5 \n\t"
167 "pxor %%mm6, %%mm6 \n\t"
169 ASMALIGN(4)
170 "3: \n\t"
172 "movq (%%"REG_S"), %%mm0 \n\t"
173 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
174 "add %%"REG_a", %%"REG_S" \n\t"
175 "add %%"REG_a", %%"REG_S" \n\t"
176 "movq (%%"REG_D"), %%mm2 \n\t"
177 "movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
178 "add %%"REG_b", %%"REG_D" \n\t"
179 "add %%"REG_b", %%"REG_D" \n\t"
180 "punpckhbw %%mm7, %%mm0 \n\t"
181 "punpckhbw %%mm7, %%mm1 \n\t"
182 "punpckhbw %%mm7, %%mm2 \n\t"
183 "punpckhbw %%mm7, %%mm3 \n\t"
184 "paddw %%mm1, %%mm4 \n\t"
185 "paddw %%mm1, %%mm5 \n\t"
186 "paddw %%mm3, %%mm6 \n\t"
187 "psubw %%mm0, %%mm4 \n\t"
188 "psubw %%mm2, %%mm5 \n\t"
189 "psubw %%mm2, %%mm6 \n\t"
191 "decl %%ecx \n\t"
192 "jnz 3b \n\t"
194 "movq %%mm0, %%mm1 \n\t"
195 "movq %%mm0, %%mm2 \n\t"
196 "movq %%mm0, %%mm3 \n\t"
197 "pcmpgtw %%mm4, %%mm1 \n\t"
198 "pcmpgtw %%mm5, %%mm2 \n\t"
199 "pcmpgtw %%mm6, %%mm3 \n\t"
200 "pxor %%mm1, %%mm4 \n\t"
201 "pxor %%mm2, %%mm5 \n\t"
202 "pxor %%mm3, %%mm6 \n\t"
203 "psubw %%mm1, %%mm4 \n\t"
204 "psubw %%mm2, %%mm5 \n\t"
205 "psubw %%mm3, %%mm6 \n\t"
206 "movq %%mm4, 8(%%"REG_d") \n\t"
207 "movq %%mm5, 24(%%"REG_d") \n\t"
208 "movq %%mm6, 40(%%"REG_d") \n\t"
210 "emms \n\t"
212 : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
213 : "memory"
215 m->p = m->t = m->s = 0;
216 for (i=0; i<8; i++) {
217 m->p += out[i];
218 m->t += out[8+i];
219 m->s += out[16+i];
221 //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);
223 #endif
225 //#define MAG(a) ((a)*(a))
226 //#define MAG(a) (abs(a))
227 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
229 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
230 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
231 #define LOWPASS(s) ((s)[0])
234 static void block_diffs_C(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
236 int x, y, e=0, o=0, s=0, p=0, t=0;
237 unsigned char *oldp, *newp;
238 m->s = m->p = m->t = 0;
239 for (x = 8; x; x--) {
240 oldp = old++;
241 newp = new++;
242 s = p = t = 0;
243 for (y = 4; y; y--) {
244 e += MAG(newp[0]-oldp[0]);
245 o += MAG(newp[ns]-oldp[os]);
246 s += newp[ns]-newp[0];
247 p += oldp[os]-oldp[0];
248 t += oldp[os]-newp[0];
249 oldp += os<<1;
250 newp += ns<<1;
252 m->s += MAG(s);
253 m->p += MAG(p);
254 m->t += MAG(t);
256 m->e = e;
257 m->o = o;
258 m->d = e+o;
261 static void (*block_diffs)(struct metrics *, unsigned char *, unsigned char *, int, int);
263 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
265 static void diff_planes(struct frameinfo *fi,
266 unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
268 int x, y;
269 struct metrics l;
270 struct metrics *peak=&fi->p, *rel=&fi->r, *mean=&fi->m;
271 memset(peak, 0, sizeof(struct metrics));
272 memset(rel, 0, sizeof(struct metrics));
273 memset(mean, 0, sizeof(struct metrics));
274 for (y = 0; y < h-7; y += 8) {
275 for (x = 8; x < w-8-7; x += 8) {
276 block_diffs(&l, old+x+y*os, new+x+y*ns, os, ns);
277 mean->d += l.d;
278 mean->e += l.e;
279 mean->o += l.o;
280 mean->s += l.s;
281 mean->p += l.p;
282 mean->t += l.t;
283 MAXUP(peak->d, l.d);
284 MAXUP(peak->e, l.e);
285 MAXUP(peak->o, l.o);
286 MAXUP(peak->s, l.s);
287 MAXUP(peak->p, l.p);
288 MAXUP(peak->t, l.t);
289 MAXUP(rel->e, l.e-l.o);
290 MAXUP(rel->o, l.o-l.e);
291 MAXUP(rel->s, l.s-l.t);
292 MAXUP(rel->p, l.p-l.t);
293 MAXUP(rel->t, l.t-l.p);
294 MAXUP(rel->d, l.t-l.s); /* hack */
297 x = (w/8-2)*(h/8);
298 mean->d /= x;
299 mean->e /= x;
300 mean->o /= x;
301 mean->s /= x;
302 mean->p /= x;
303 mean->t /= x;
306 static void diff_fields(struct frameinfo *fi, mp_image_t *old, mp_image_t *new)
308 diff_planes(fi, old->planes[0], new->planes[0],
309 new->w, new->h, old->stride[0], new->stride[0]);
312 static void stats(struct frameinfo *f)
314 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",
315 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);
318 static int foo(struct vf_priv_s *p, mp_image_t *new, mp_image_t *cur)
320 struct frameinfo *f = p->fi;
322 f[0] = f[1];
323 diff_fields(&f[1], cur, new);
324 stats(&f[1]);
326 // Immediately drop this frame if it's already been used.
327 if (p->dropnext) {
328 p->dropnext = 0;
329 return F_DROP;
332 // Sometimes a pulldown frame comes all by itself, so both
333 // its top and bottom field are duplicates from the adjacent
334 // two frames. We can just drop such a frame, but we
335 // immediately show the next frame instead to keep the frame
336 // drops evenly spaced during normal 3:2 pulldown sequences.
337 if ((3*f[1].r.o < f[1].r.e) && (f[1].r.s < f[1].r.d)) {
338 p->dropnext = 1;
339 return F_NEXT;
342 // If none of these conditions hold, we will consider the frame
343 // progressive and just show it as-is.
344 if (!( (3*f[0].r.e < f[0].r.o) ||
345 ((2*f[0].r.d < f[0].r.s) && (f[0].r.s > 1200)) ||
346 ((2*f[1].r.t < f[1].r.p) && (f[1].r.p > 1200)) ))
347 return F_SHOW;
349 // Otherwise, we have to decide whether to merge or drop.
350 // If the noise metric only increases minimally, we're off
351 // to a good start...
352 if (((2*f[1].r.t < 3*f[1].r.p) && (f[1].r.t < 3600)) ||
353 (f[1].r.t < 900) || (f[1].r.d < 900)) {
354 // ...and if noise decreases or the duplicate even field
355 // is detected, we go ahead with the merge.
356 if ((3*f[0].r.e < f[0].r.o) || (2*f[1].r.t < f[1].r.p)) {
357 p->dropnext = 1;
358 return F_MERGE;
361 return F_DROP;
366 static void copy_image(mp_image_t *dmpi, mp_image_t *mpi, int field)
368 switch (field) {
369 case 0:
370 my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
371 dmpi->stride[0]*2, mpi->stride[0]*2);
372 if (mpi->flags & MP_IMGFLAG_PLANAR) {
373 my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
374 mpi->chroma_width, mpi->chroma_height/2,
375 dmpi->stride[1]*2, mpi->stride[1]*2);
376 my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
377 mpi->chroma_width, mpi->chroma_height/2,
378 dmpi->stride[2]*2, mpi->stride[2]*2);
380 break;
381 case 1:
382 my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
383 mpi->planes[0]+mpi->stride[0], mpi->w, mpi->h/2,
384 dmpi->stride[0]*2, mpi->stride[0]*2);
385 if (mpi->flags & MP_IMGFLAG_PLANAR) {
386 my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
387 mpi->planes[1]+mpi->stride[1],
388 mpi->chroma_width, mpi->chroma_height/2,
389 dmpi->stride[1]*2, mpi->stride[1]*2);
390 my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
391 mpi->planes[2]+mpi->stride[2],
392 mpi->chroma_width, mpi->chroma_height/2,
393 dmpi->stride[2]*2, mpi->stride[2]*2);
395 break;
396 case 2:
397 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
398 dmpi->stride[0], mpi->stride[0]);
399 if (mpi->flags & MP_IMGFLAG_PLANAR) {
400 memcpy_pic(dmpi->planes[1], mpi->planes[1],
401 mpi->chroma_width, mpi->chroma_height,
402 dmpi->stride[1], mpi->stride[1]);
403 memcpy_pic(dmpi->planes[2], mpi->planes[2],
404 mpi->chroma_width, mpi->chroma_height,
405 dmpi->stride[2], mpi->stride[2]);
407 break;
411 static int do_put_image(struct vf_instance_s* vf, mp_image_t *dmpi)
413 struct vf_priv_s *p = vf->priv;
414 int dropflag=0;
416 if (!p->dropnext) switch (p->drop) {
417 case 0:
418 dropflag = 0;
419 break;
420 case 1:
421 dropflag = (++p->lastdrop >= 5);
422 break;
423 case 2:
424 dropflag = (++p->lastdrop >= 5) && (4*p->inframes <= 5*p->outframes);
425 break;
428 if (dropflag) {
429 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
430 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
431 mp_msg(MSGT_VFILTER, MSGL_V, "!");
432 p->lastdrop = 0;
433 return 0;
436 p->outframes++;
437 return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
440 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
442 int ret=0;
443 struct vf_priv_s *p = vf->priv;
445 p->inframes++;
447 if (p->first) { /* hack */
448 p->first = 0;
449 return 1;
452 if (!p->dmpi) p->dmpi = vf_get_image(vf->next, mpi->imgfmt,
453 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
454 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
455 mpi->width, mpi->height);
456 /* FIXME -- not correct, off by one frame! */
457 p->dmpi->qscale = mpi->qscale;
458 p->dmpi->qstride = mpi->qstride;
459 p->dmpi->qscale_type = mpi->qscale_type;
461 switch (foo(p, mpi, p->dmpi)) {
462 case F_DROP:
463 copy_image(p->dmpi, mpi, 2);
464 ret = 0;
465 p->lastdrop = 0;
466 mp_msg(MSGT_VFILTER, MSGL_V, "DROP\n");
467 break;
468 case F_MERGE:
469 copy_image(p->dmpi, mpi, 0);
470 ret = do_put_image(vf, p->dmpi);
471 copy_image(p->dmpi, mpi, 1);
472 mp_msg(MSGT_VFILTER, MSGL_V, "MERGE\n");
473 p->dmpi = NULL;
474 break;
475 case F_NEXT:
476 copy_image(p->dmpi, mpi, 2);
477 ret = do_put_image(vf, p->dmpi);
478 mp_msg(MSGT_VFILTER, MSGL_V, "NEXT\n");
479 p->dmpi = NULL;
480 break;
481 case F_SHOW:
482 ret = do_put_image(vf, p->dmpi);
483 copy_image(p->dmpi, mpi, 2);
484 mp_msg(MSGT_VFILTER, MSGL_V, "OK\n");
485 p->dmpi = NULL;
486 break;
488 return ret;
491 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
493 switch (fmt) {
494 case IMGFMT_YV12:
495 case IMGFMT_IYUV:
496 case IMGFMT_I420:
497 return vf_next_query_format(vf, fmt);
499 return 0;
502 static void uninit(struct vf_instance_s* vf)
504 free(vf->priv);
507 static int open(vf_instance_t *vf, char* args)
509 struct vf_priv_s *p;
510 vf->put_image = put_image;
511 vf->query_format = query_format;
512 vf->uninit = uninit;
513 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
514 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
515 p->drop = 0;
516 p->first = 1;
517 if (args) sscanf(args, "%d", &p->drop);
518 block_diffs = block_diffs_C;
519 #ifdef HAVE_MMX
520 if(gCpuCaps.hasMMX) block_diffs = block_diffs_MMX;
521 #endif
522 return 1;
525 const vf_info_t vf_info_ivtc = {
526 "inverse telecine, take 2",
527 "ivtc",
528 "Rich Felker",
530 open,
531 NULL