ao_pulse: support native mute control
[mplayer.git] / libmpcodecs / vf_ivtc.c
blob966292ff148de7d70969ca0cec9fb0d1c4eae467
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"
34 struct metrics {
35 /* difference: total, even lines, odd lines */
36 int d, e, o;
37 /* noise: temporal, spacial (current), spacial (past) */
38 int t, s, p;
41 struct frameinfo {
42 /* peak, relative, mean */
43 struct metrics p, r, m;
46 struct vf_priv_s {
47 struct frameinfo fi[2];
48 mp_image_t *dmpi;
49 int first;
50 int drop, lastdrop, dropnext;
51 int inframes, outframes;
52 struct vf_detc_pts_buf ptsbuf;
55 enum {
56 F_DROP,
57 F_MERGE,
58 F_NEXT,
59 F_SHOW
62 #if HAVE_MMX && HAVE_EBX_AVAILABLE
63 static void block_diffs_MMX(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
65 int i;
66 short out[24]; // output buffer for the partial metrics from the mmx code
68 __asm__ (
69 "movl $4, %%ecx \n\t"
70 "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
71 "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
72 "pxor %%mm7, %%mm7 \n\t" // all zeros
74 ASMALIGN(4)
75 "1: \n\t"
77 // Even 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, %%mm4 \n\t"
92 "paddw %%mm1, %%mm4 \n\t"
93 "paddw %%mm2, %%mm4 \n\t"
94 "paddw %%mm3, %%mm4 \n\t"
96 // Odd difference
97 "movq (%%"REG_S"), %%mm0 \n\t"
98 "movq (%%"REG_S"), %%mm2 \n\t"
99 "add %%"REG_a", %%"REG_S" \n\t"
100 "movq (%%"REG_D"), %%mm1 \n\t"
101 "add %%"REG_b", %%"REG_D" \n\t"
102 "psubusb %%mm1, %%mm2 \n\t"
103 "psubusb %%mm0, %%mm1 \n\t"
104 "movq %%mm2, %%mm0 \n\t"
105 "movq %%mm1, %%mm3 \n\t"
106 "punpcklbw %%mm7, %%mm0 \n\t"
107 "punpcklbw %%mm7, %%mm1 \n\t"
108 "punpckhbw %%mm7, %%mm2 \n\t"
109 "punpckhbw %%mm7, %%mm3 \n\t"
110 "paddw %%mm0, %%mm5 \n\t"
111 "paddw %%mm1, %%mm5 \n\t"
112 "paddw %%mm2, %%mm5 \n\t"
113 "paddw %%mm3, %%mm5 \n\t"
115 "decl %%ecx \n\t"
116 "jnz 1b \n\t"
117 "movq %%mm4, (%%"REG_d") \n\t"
118 "movq %%mm5, 8(%%"REG_d") \n\t"
120 : "S" (old), "D" (new), "a" (os), "b" (ns), "d" (out)
121 : "memory"
123 m->e = out[0]+out[1]+out[2]+out[3];
124 m->o = out[4]+out[5]+out[6]+out[7];
125 m->d = m->e + m->o;
127 __asm__ (
128 // First loop to measure first four columns
129 "movl $4, %%ecx \n\t"
130 "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
131 "pxor %%mm5, %%mm5 \n\t" // Temporal noise
132 "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
134 ASMALIGN(4)
135 "2: \n\t"
137 "movq (%%"REG_S"), %%mm0 \n\t"
138 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
139 "add %%"REG_a", %%"REG_S" \n\t"
140 "add %%"REG_a", %%"REG_S" \n\t"
141 "movq (%%"REG_D"), %%mm2 \n\t"
142 "movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
143 "add %%"REG_b", %%"REG_D" \n\t"
144 "add %%"REG_b", %%"REG_D" \n\t"
145 "punpcklbw %%mm7, %%mm0 \n\t"
146 "punpcklbw %%mm7, %%mm1 \n\t"
147 "punpcklbw %%mm7, %%mm2 \n\t"
148 "punpcklbw %%mm7, %%mm3 \n\t"
149 "paddw %%mm1, %%mm4 \n\t"
150 "paddw %%mm1, %%mm5 \n\t"
151 "paddw %%mm3, %%mm6 \n\t"
152 "psubw %%mm0, %%mm4 \n\t"
153 "psubw %%mm2, %%mm5 \n\t"
154 "psubw %%mm2, %%mm6 \n\t"
156 "decl %%ecx \n\t"
157 "jnz 2b \n\t"
159 "movq %%mm0, %%mm1 \n\t"
160 "movq %%mm0, %%mm2 \n\t"
161 "movq %%mm0, %%mm3 \n\t"
162 "pcmpgtw %%mm4, %%mm1 \n\t"
163 "pcmpgtw %%mm5, %%mm2 \n\t"
164 "pcmpgtw %%mm6, %%mm3 \n\t"
165 "pxor %%mm1, %%mm4 \n\t"
166 "pxor %%mm2, %%mm5 \n\t"
167 "pxor %%mm3, %%mm6 \n\t"
168 "psubw %%mm1, %%mm4 \n\t"
169 "psubw %%mm2, %%mm5 \n\t"
170 "psubw %%mm3, %%mm6 \n\t"
171 "movq %%mm4, (%%"REG_d") \n\t"
172 "movq %%mm5, 16(%%"REG_d") \n\t"
173 "movq %%mm6, 32(%%"REG_d") \n\t"
175 "mov %%"REG_a", %%"REG_c" \n\t"
176 "shl $3, %%"REG_c" \n\t"
177 "sub %%"REG_c", %%"REG_S" \n\t"
178 "mov %%"REG_b", %%"REG_c" \n\t"
179 "shl $3, %%"REG_c" \n\t"
180 "sub %%"REG_c", %%"REG_D" \n\t"
182 // Second loop for the last four columns
183 "movl $4, %%ecx \n\t"
184 "pxor %%mm4, %%mm4 \n\t"
185 "pxor %%mm5, %%mm5 \n\t"
186 "pxor %%mm6, %%mm6 \n\t"
188 ASMALIGN(4)
189 "3: \n\t"
191 "movq (%%"REG_S"), %%mm0 \n\t"
192 "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
193 "add %%"REG_a", %%"REG_S" \n\t"
194 "add %%"REG_a", %%"REG_S" \n\t"
195 "movq (%%"REG_D"), %%mm2 \n\t"
196 "movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
197 "add %%"REG_b", %%"REG_D" \n\t"
198 "add %%"REG_b", %%"REG_D" \n\t"
199 "punpckhbw %%mm7, %%mm0 \n\t"
200 "punpckhbw %%mm7, %%mm1 \n\t"
201 "punpckhbw %%mm7, %%mm2 \n\t"
202 "punpckhbw %%mm7, %%mm3 \n\t"
203 "paddw %%mm1, %%mm4 \n\t"
204 "paddw %%mm1, %%mm5 \n\t"
205 "paddw %%mm3, %%mm6 \n\t"
206 "psubw %%mm0, %%mm4 \n\t"
207 "psubw %%mm2, %%mm5 \n\t"
208 "psubw %%mm2, %%mm6 \n\t"
210 "decl %%ecx \n\t"
211 "jnz 3b \n\t"
213 "movq %%mm0, %%mm1 \n\t"
214 "movq %%mm0, %%mm2 \n\t"
215 "movq %%mm0, %%mm3 \n\t"
216 "pcmpgtw %%mm4, %%mm1 \n\t"
217 "pcmpgtw %%mm5, %%mm2 \n\t"
218 "pcmpgtw %%mm6, %%mm3 \n\t"
219 "pxor %%mm1, %%mm4 \n\t"
220 "pxor %%mm2, %%mm5 \n\t"
221 "pxor %%mm3, %%mm6 \n\t"
222 "psubw %%mm1, %%mm4 \n\t"
223 "psubw %%mm2, %%mm5 \n\t"
224 "psubw %%mm3, %%mm6 \n\t"
225 "movq %%mm4, 8(%%"REG_d") \n\t"
226 "movq %%mm5, 24(%%"REG_d") \n\t"
227 "movq %%mm6, 40(%%"REG_d") \n\t"
229 "emms \n\t"
231 : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
232 : "memory"
234 m->p = m->t = m->s = 0;
235 for (i=0; i<8; i++) {
236 m->p += out[i];
237 m->t += out[8+i];
238 m->s += out[16+i];
240 //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);
242 #endif
244 //#define MAG(a) ((a)*(a))
245 //#define MAG(a) (abs(a))
246 #define MAG(a) (((a)^((a)>>31))-((a)>>31))
248 //#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
249 //#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
250 #define LOWPASS(s) ((s)[0])
253 static void block_diffs_C(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
255 int x, y, e=0, o=0, s=0, p=0, t=0;
256 unsigned char *oldp, *newp;
257 m->s = m->p = m->t = 0;
258 for (x = 8; x; x--) {
259 oldp = old++;
260 newp = new++;
261 s = p = t = 0;
262 for (y = 4; y; y--) {
263 e += MAG(newp[0]-oldp[0]);
264 o += MAG(newp[ns]-oldp[os]);
265 s += newp[ns]-newp[0];
266 p += oldp[os]-oldp[0];
267 t += oldp[os]-newp[0];
268 oldp += os<<1;
269 newp += ns<<1;
271 m->s += MAG(s);
272 m->p += MAG(p);
273 m->t += MAG(t);
275 m->e = e;
276 m->o = o;
277 m->d = e+o;
280 static void (*block_diffs)(struct metrics *, unsigned char *, unsigned char *, int, int);
282 #define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
284 static void diff_planes(struct frameinfo *fi,
285 unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
287 int x, y;
288 struct metrics l;
289 struct metrics *peak=&fi->p, *rel=&fi->r, *mean=&fi->m;
290 memset(peak, 0, sizeof(struct metrics));
291 memset(rel, 0, sizeof(struct metrics));
292 memset(mean, 0, sizeof(struct metrics));
293 for (y = 0; y < h-7; y += 8) {
294 for (x = 8; x < w-8-7; x += 8) {
295 block_diffs(&l, old+x+y*os, new+x+y*ns, os, ns);
296 mean->d += l.d;
297 mean->e += l.e;
298 mean->o += l.o;
299 mean->s += l.s;
300 mean->p += l.p;
301 mean->t += l.t;
302 MAXUP(peak->d, l.d);
303 MAXUP(peak->e, l.e);
304 MAXUP(peak->o, l.o);
305 MAXUP(peak->s, l.s);
306 MAXUP(peak->p, l.p);
307 MAXUP(peak->t, l.t);
308 MAXUP(rel->e, l.e-l.o);
309 MAXUP(rel->o, l.o-l.e);
310 MAXUP(rel->s, l.s-l.t);
311 MAXUP(rel->p, l.p-l.t);
312 MAXUP(rel->t, l.t-l.p);
313 MAXUP(rel->d, l.t-l.s); /* hack */
316 x = (w/8-2)*(h/8);
317 mean->d /= x;
318 mean->e /= x;
319 mean->o /= x;
320 mean->s /= x;
321 mean->p /= x;
322 mean->t /= x;
325 static void diff_fields(struct frameinfo *fi, mp_image_t *old, mp_image_t *new)
327 diff_planes(fi, old->planes[0], new->planes[0],
328 new->w, new->h, old->stride[0], new->stride[0]);
331 static void stats(struct frameinfo *f)
333 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",
334 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);
337 static int foo(struct vf_priv_s *p, mp_image_t *new, mp_image_t *cur)
339 struct frameinfo *f = p->fi;
341 f[0] = f[1];
342 diff_fields(&f[1], cur, new);
343 stats(&f[1]);
345 // Immediately drop this frame if it's already been used.
346 if (p->dropnext) {
347 p->dropnext = 0;
348 return F_DROP;
351 // Sometimes a pulldown frame comes all by itself, so both
352 // its top and bottom field are duplicates from the adjacent
353 // two frames. We can just drop such a frame, but we
354 // immediately show the next frame instead to keep the frame
355 // drops evenly spaced during normal 3:2 pulldown sequences.
356 if ((3*f[1].r.o < f[1].r.e) && (f[1].r.s < f[1].r.d)) {
357 p->dropnext = 1;
358 return F_NEXT;
361 // If none of these conditions hold, we will consider the frame
362 // progressive and just show it as-is.
363 if (!( (3*f[0].r.e < f[0].r.o) ||
364 ((2*f[0].r.d < f[0].r.s) && (f[0].r.s > 1200)) ||
365 ((2*f[1].r.t < f[1].r.p) && (f[1].r.p > 1200)) ))
366 return F_SHOW;
368 // Otherwise, we have to decide whether to merge or drop.
369 // If the noise metric only increases minimally, we're off
370 // to a good start...
371 if (((2*f[1].r.t < 3*f[1].r.p) && (f[1].r.t < 3600)) ||
372 (f[1].r.t < 900) || (f[1].r.d < 900)) {
373 // ...and if noise decreases or the duplicate even field
374 // is detected, we go ahead with the merge.
375 if ((3*f[0].r.e < f[0].r.o) || (2*f[1].r.t < f[1].r.p)) {
376 p->dropnext = 1;
377 return F_MERGE;
380 return F_DROP;
385 static void copy_image(mp_image_t *dmpi, mp_image_t *mpi, int field)
387 switch (field) {
388 case 0:
389 my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
390 dmpi->stride[0]*2, mpi->stride[0]*2);
391 if (mpi->flags & MP_IMGFLAG_PLANAR) {
392 my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
393 mpi->chroma_width, mpi->chroma_height/2,
394 dmpi->stride[1]*2, mpi->stride[1]*2);
395 my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
396 mpi->chroma_width, mpi->chroma_height/2,
397 dmpi->stride[2]*2, mpi->stride[2]*2);
399 break;
400 case 1:
401 my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
402 mpi->planes[0]+mpi->stride[0], mpi->w, mpi->h/2,
403 dmpi->stride[0]*2, mpi->stride[0]*2);
404 if (mpi->flags & MP_IMGFLAG_PLANAR) {
405 my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
406 mpi->planes[1]+mpi->stride[1],
407 mpi->chroma_width, mpi->chroma_height/2,
408 dmpi->stride[1]*2, mpi->stride[1]*2);
409 my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
410 mpi->planes[2]+mpi->stride[2],
411 mpi->chroma_width, mpi->chroma_height/2,
412 dmpi->stride[2]*2, mpi->stride[2]*2);
414 break;
415 case 2:
416 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
417 dmpi->stride[0], mpi->stride[0]);
418 if (mpi->flags & MP_IMGFLAG_PLANAR) {
419 memcpy_pic(dmpi->planes[1], mpi->planes[1],
420 mpi->chroma_width, mpi->chroma_height,
421 dmpi->stride[1], mpi->stride[1]);
422 memcpy_pic(dmpi->planes[2], mpi->planes[2],
423 mpi->chroma_width, mpi->chroma_height,
424 dmpi->stride[2], mpi->stride[2]);
426 break;
430 static int do_put_image(struct vf_instance *vf, mp_image_t *dmpi, double pts)
432 struct vf_priv_s *p = vf->priv;
433 int dropflag=0;
435 if (!p->dropnext) switch (p->drop) {
436 case 0:
437 dropflag = 0;
438 break;
439 case 1:
440 dropflag = (++p->lastdrop >= 5);
441 break;
442 case 2:
443 dropflag = (++p->lastdrop >= 5) && (4*p->inframes <= 5*p->outframes);
444 break;
447 if (dropflag) {
448 //mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
449 // p->outframes, p->inframes, (float)p->outframes/p->inframes);
450 mp_msg(MSGT_VFILTER, MSGL_V, "!");
451 p->lastdrop = 0;
452 vf_detc_adjust_pts(&p->ptsbuf, pts, 0, 1);
453 return 0;
456 p->outframes++;
457 return vf_next_put_image(vf, dmpi, vf_detc_adjust_pts(&p->ptsbuf, pts, 0, 0));
460 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
462 int ret=0;
463 struct vf_priv_s *p = vf->priv;
465 p->inframes++;
467 if (p->first) { /* hack */
468 p->first = 0;
469 vf_detc_adjust_pts(&p->ptsbuf, pts, 0, 1);
470 return 1;
473 if (!p->dmpi) p->dmpi = vf_get_image(vf->next, mpi->imgfmt,
474 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
475 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
476 mpi->width, mpi->height);
477 /* FIXME -- not correct, off by one frame! */
478 p->dmpi->qscale = mpi->qscale;
479 p->dmpi->qstride = mpi->qstride;
480 p->dmpi->qscale_type = mpi->qscale_type;
482 switch (foo(p, mpi, p->dmpi)) {
483 case F_DROP:
484 copy_image(p->dmpi, mpi, 2);
485 ret = 0;
486 p->lastdrop = 0;
487 mp_msg(MSGT_VFILTER, MSGL_V, "DROP\n");
488 vf_detc_adjust_pts(&p->ptsbuf, pts, 0, 1);
489 break;
490 case F_MERGE:
491 copy_image(p->dmpi, mpi, 0);
492 ret = do_put_image(vf, p->dmpi, pts);
493 copy_image(p->dmpi, mpi, 1);
494 mp_msg(MSGT_VFILTER, MSGL_V, "MERGE\n");
495 p->dmpi = NULL;
496 break;
497 case F_NEXT:
498 copy_image(p->dmpi, mpi, 2);
499 ret = do_put_image(vf, p->dmpi, pts);
500 mp_msg(MSGT_VFILTER, MSGL_V, "NEXT\n");
501 p->dmpi = NULL;
502 break;
503 case F_SHOW:
504 ret = do_put_image(vf, p->dmpi, pts);
505 copy_image(p->dmpi, mpi, 2);
506 mp_msg(MSGT_VFILTER, MSGL_V, "OK\n");
507 p->dmpi = NULL;
508 break;
510 return ret;
513 static int query_format(struct vf_instance *vf, unsigned int fmt)
515 switch (fmt) {
516 case IMGFMT_YV12:
517 case IMGFMT_IYUV:
518 case IMGFMT_I420:
519 return vf_next_query_format(vf, fmt);
521 return 0;
524 static void uninit(struct vf_instance *vf)
526 free(vf->priv);
529 static int vf_open(vf_instance_t *vf, char *args)
531 struct vf_priv_s *p;
532 vf->put_image = put_image;
533 vf->query_format = query_format;
534 vf->uninit = uninit;
535 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
536 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
537 p->drop = 0;
538 p->first = 1;
539 if (args) sscanf(args, "%d", &p->drop);
540 block_diffs = block_diffs_C;
541 #if HAVE_MMX && HAVE_EBX_AVAILABLE
542 if(gCpuCaps.hasMMX) block_diffs = block_diffs_MMX;
543 #endif
544 vf_detc_init_pts_buf(&p->ptsbuf);
545 return 1;
548 const vf_info_t vf_info_ivtc = {
549 "inverse telecine, take 2",
550 "ivtc",
551 "Rich Felker",
553 vf_open,
554 NULL