options: move -name and -title to option struct
[mplayer/greg.git] / libmpcodecs / vf_divtc.c
blobd632a4c60d18a1abcd53f2fbc78900c701209451
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>
22 #include <limits.h>
23 #include <math.h>
25 #include "config.h"
26 #include "mp_msg.h"
27 #include "cpudetect.h"
28 #include "libavutil/common.h"
29 #include "mpbswap.h"
31 #include "img_format.h"
32 #include "mp_image.h"
33 #include "vf.h"
35 #include "libvo/fastmemcpy.h"
37 const vf_info_t vf_info_divtc;
39 struct vf_priv_s
41 int deghost, pass, phase, window, fcount, bcount, frameno, misscount,
42 ocount, sum[5];
43 double threshold;
44 FILE *file;
45 char *bdata;
46 unsigned int *csdata;
47 int *history;
51 * diff_MMX and diff_C stolen from vf_decimate.c
54 #if HAVE_MMX && HAVE_EBX_AVAILABLE
55 static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
57 volatile short out[4];
58 __asm__ (
59 "movl $8, %%ecx \n\t"
60 "pxor %%mm4, %%mm4 \n\t"
61 "pxor %%mm7, %%mm7 \n\t"
63 ASMALIGN(4)
64 "1: \n\t"
66 "movq (%%"REG_S"), %%mm0 \n\t"
67 "movq (%%"REG_S"), %%mm2 \n\t"
68 "add %%"REG_a", %%"REG_S" \n\t"
69 "movq (%%"REG_D"), %%mm1 \n\t"
70 "add %%"REG_b", %%"REG_D" \n\t"
71 "psubusb %%mm1, %%mm2 \n\t"
72 "psubusb %%mm0, %%mm1 \n\t"
73 "movq %%mm2, %%mm0 \n\t"
74 "movq %%mm1, %%mm3 \n\t"
75 "punpcklbw %%mm7, %%mm0 \n\t"
76 "punpcklbw %%mm7, %%mm1 \n\t"
77 "punpckhbw %%mm7, %%mm2 \n\t"
78 "punpckhbw %%mm7, %%mm3 \n\t"
79 "paddw %%mm0, %%mm4 \n\t"
80 "paddw %%mm1, %%mm4 \n\t"
81 "paddw %%mm2, %%mm4 \n\t"
82 "paddw %%mm3, %%mm4 \n\t"
84 "decl %%ecx \n\t"
85 "jnz 1b \n\t"
86 "movq %%mm4, (%%"REG_d") \n\t"
87 "emms \n\t"
89 : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
90 : "%ecx", "memory"
92 return out[0]+out[1]+out[2]+out[3];
94 #endif
96 static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
98 int x, y, d=0;
100 for(y=8; y; y--, new+=ns, old+=os)
101 for(x=8; x; x--)
102 d+=abs(new[x]-old[x]);
104 return d;
107 static int (*diff)(unsigned char *, unsigned char *, int, int);
109 static int diff_plane(unsigned char *old, unsigned char *new,
110 int w, int h, int os, int ns, int arg)
112 int x, y, d, max=0, sum=0, n=0;
114 for(y=0; y<h-7; y+=8)
116 for(x=0; x<w-7; x+=8)
118 d=diff(old+x+y*os, new+x+y*ns, os, ns);
119 if(d>max) max=d;
120 sum+=d;
121 n++;
125 return (sum+n*max)/2;
129 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
130 int w, int h, int s, int zs, int arg)
132 unsigned int shift, sum;
133 unsigned char *e;
135 for(sum=0; h; h--, p+=s-w)
136 for(e=p+w, shift=32; p<e;)
137 sum^=(*p++)<<(shift=(shift-8)&31);
139 return sum;
143 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
144 int w, int h, int s, int zs, int arg)
146 unsigned int shift;
147 uint32_t sum, t;
148 unsigned char *e, *e2;
149 #if HAVE_FAST_64BIT
150 typedef uint64_t wsum_t;
151 #else
152 typedef uint32_t wsum_t;
153 #endif
154 wsum_t wsum;
156 for(sum=0; h; h--, p+=s-w)
158 for(shift=0, e=p+w; (int)p&(sizeof(wsum_t)-1) && p<e;)
159 sum^=*p++<<(shift=(shift-8)&31);
161 for(wsum=0, e2=e-sizeof(wsum_t)+1; p<e2; p+=sizeof(wsum_t))
162 wsum^=*(wsum_t *)p;
164 #if HAVE_FAST_64BIT
165 t=be2me_32((uint32_t)(wsum>>32^wsum));
166 #else
167 t=be2me_32(wsum);
168 #endif
170 for(sum^=(t<<shift|t>>(32-shift)); p<e;)
171 sum^=*p++<<(shift=(shift-8)&31);
174 return sum;
177 static int deghost_plane(unsigned char *d, unsigned char *s,
178 int w, int h, int ds, int ss, int threshold)
180 int t;
181 unsigned char *e;
183 for(; h; h--, s+=ss-w, d+=ds-w)
184 for(e=d+w; d<e; d++, s++)
185 if(abs(*d-*s)>=threshold)
186 *d=(t=(*d<<1)-*s)<0?0:t>255?255:t;
188 return 0;
191 static int copyop(unsigned char *d, unsigned char *s, int bpl, int h, int dstride, int sstride, int dummy) {
192 memcpy_pic(d, s, bpl, h, dstride, sstride);
193 return 0;
196 static int imgop(int(*planeop)(unsigned char *, unsigned char *,
197 int, int, int, int, int),
198 mp_image_t *dst, mp_image_t *src, int arg)
200 if(dst->flags&MP_IMGFLAG_PLANAR)
201 return planeop(dst->planes[0], src?src->planes[0]:0,
202 dst->w, dst->h,
203 dst->stride[0], src?src->stride[0]:0, arg)+
204 planeop(dst->planes[1], src?src->planes[1]:0,
205 dst->chroma_width, dst->chroma_height,
206 dst->stride[1], src?src->stride[1]:0, arg)+
207 planeop(dst->planes[2], src?src->planes[2]:0,
208 dst->chroma_width, dst->chroma_height,
209 dst->stride[2], src?src->stride[2]:0, arg);
211 return planeop(dst->planes[0], src?src->planes[0]:0,
212 dst->w*(dst->bpp/8), dst->h,
213 dst->stride[0], src?src->stride[0]:0, arg);
217 * Find the phase in which the telecine pattern fits best to the
218 * given 5 frame slice of frame difference measurements.
220 * If phase1 and phase2 are not negative, only the two specified
221 * phases are tested.
224 static int match(struct vf_priv_s *p, int *diffs,
225 int phase1, int phase2, double *strength)
227 const int pattern1[]={ -4, 1, 1, 1, 1 },
228 pattern2[]={ -2, -3, 4, 4, -3 }, *pattern;
229 int f, m, n, t[5];
231 pattern=p->deghost>0?pattern2:pattern1;
233 for(f=0; f<5; f++)
235 if(phase1<0 || phase2<0 || f==phase1 || f==phase2)
237 for(n=t[f]=0; n<5; n++)
238 t[f]+=diffs[n]*pattern[(n-f+5)%5];
240 else
241 t[f]=INT_MIN;
244 /* find the best match */
245 for(m=0, n=1; n<5; n++)
246 if(t[n]>t[m]) m=n;
248 if(strength)
250 /* the second best match */
251 for(f=m?0:1, n=f+1; n<5; n++)
252 if(n!=m && t[n]>t[f]) f=n;
254 *strength=(t[m]>0?(double)(t[m]-t[f])/t[m]:0.0);
257 return m;
260 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
262 mp_image_t *dmpi, *tmpi=0;
263 int n, m, f, newphase;
264 struct vf_priv_s *p=vf->priv;
265 unsigned int checksum;
266 double d;
268 dmpi=vf_get_image(vf->next, mpi->imgfmt,
269 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
270 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
271 mpi->width, mpi->height);
272 vf_clone_mpi_attributes(dmpi, mpi);
274 newphase=p->phase;
276 switch(p->pass)
278 case 1:
279 fprintf(p->file, "%08x %d\n",
280 (unsigned int)imgop((void *)checksum_plane, mpi, 0, 0),
281 p->frameno?imgop(diff_plane, dmpi, mpi, 0):0);
282 break;
284 case 2:
285 if(p->frameno/5>p->bcount)
287 mp_msg(MSGT_VFILTER, MSGL_ERR,
288 "\n%s: Log file ends prematurely! "
289 "Switching to one pass mode.\n", vf->info->name);
290 p->pass=0;
291 break;
294 checksum=(unsigned int)imgop((void *)checksum_plane, mpi, 0, 0);
296 if(checksum!=p->csdata[p->frameno])
298 for(f=0; f<100; f++)
299 if(p->frameno+f<p->fcount && p->csdata[p->frameno+f]==checksum)
300 break;
301 else if(p->frameno-f>=0 && p->csdata[p->frameno-f]==checksum)
303 f=-f;
304 break;
307 if(f<100)
309 mp_msg(MSGT_VFILTER, MSGL_INFO,
310 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
311 vf->info->name, f);
313 p->frameno+=f;
314 p->misscount=0;
316 else if(p->misscount++>=30)
318 mp_msg(MSGT_VFILTER, MSGL_ERR,
319 "\n%s: Sync with pass-1 lost! "
320 "Switching to one pass mode.\n", vf->info->name);
321 p->pass=0;
322 break;
326 n=(p->frameno)/5;
327 if(n>=p->bcount) n=p->bcount-1;
329 newphase=p->bdata[n];
330 break;
332 default:
333 if(p->frameno)
335 int *sump=p->sum+p->frameno%5,
336 *histp=p->history+p->frameno%p->window;
338 *sump-=*histp;
339 *sump+=(*histp=imgop(diff_plane, dmpi, mpi, 0));
342 m=match(p, p->sum, -1, -1, &d);
344 if(d>=p->threshold)
345 newphase=m;
348 n=p->ocount++%5;
350 if(newphase!=p->phase && ((p->phase+4)%5<n)==((newphase+4)%5<n))
352 p->phase=newphase;
353 mp_msg(MSGT_VFILTER, MSGL_STATUS,
354 "\n%s: Telecine phase %d.\n", vf->info->name, p->phase);
357 switch((p->frameno++-p->phase+10)%5)
359 case 0:
360 imgop(copyop, dmpi, mpi, 0);
361 return 0;
363 case 4:
364 if(p->deghost>0)
366 tmpi=vf_get_image(vf->next, mpi->imgfmt,
367 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
368 MP_IMGFLAG_READABLE,
369 mpi->width, mpi->height);
370 vf_clone_mpi_attributes(tmpi, mpi);
372 imgop(copyop, tmpi, mpi, 0);
373 imgop(deghost_plane, tmpi, dmpi, p->deghost);
374 imgop(copyop, dmpi, mpi, 0);
375 return vf_next_put_image(vf, tmpi, MP_NOPTS_VALUE);
379 imgop(copyop, dmpi, mpi, 0);
380 return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
383 static int analyze(struct vf_priv_s *p)
385 int *buf=0, *bp, bufsize=0, n, b, f, i, j, m, s;
386 unsigned int *cbuf=0, *cp;
387 char *pbuf;
388 char lbuf[256];
389 int sum[5];
390 double d;
392 /* read the file */
394 n=15;
395 while(fgets(lbuf, 256, p->file))
397 if(n>=bufsize-19)
399 bufsize=bufsize?bufsize*2:30000;
400 if((bp=realloc(buf, bufsize*sizeof *buf))) buf=bp;
401 if((cp=realloc(cbuf, bufsize*sizeof *cbuf))) cbuf=cp;
403 if(!bp || !cp)
405 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Not enough memory.\n",
406 vf_info_divtc.name);
407 free(buf);
408 free(cbuf);
409 return 0;
412 sscanf(lbuf, "%x %d", cbuf+n, buf+n);
413 n++;
416 if(!n)
418 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Empty 2-pass log file.\n",
419 vf_info_divtc.name);
420 free(buf);
421 free(cbuf);
422 return 0;
425 /* generate some dummy data past the beginning and end of the array */
427 buf+=15, cbuf+=15;
428 n-=15;
430 memcpy(buf-15, buf, 15*sizeof *buf);
431 memset(cbuf-15, 0, 15*sizeof *cbuf);
433 while(n%5)
434 buf[n]=buf[n-5], cbuf[n]=0, n++;
436 memcpy(buf+n, buf+n-15, 15*sizeof *buf);
437 memset(cbuf+n, 0, 15*sizeof *cbuf);
439 p->csdata=cbuf;
440 p->fcount=n;
442 /* array with one slot for each slice of 5 frames */
444 p->bdata=pbuf=malloc(p->bcount=b=(n/5));
445 memset(pbuf, 255, b);
447 /* resolve the automatic mode */
449 if(p->deghost<0)
451 int deghost=-p->deghost;
452 double s0=0.0, s1=0.0;
454 for(f=0; f<n; f+=5)
456 p->deghost=0; match(p, buf+f, -1, -1, &d); s0+=d;
457 p->deghost=1; match(p, buf+f, -1, -1, &d); s1+=d;
460 p->deghost=s1>s0?deghost:0;
462 mp_msg(MSGT_VFILTER, MSGL_INFO,
463 "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
464 vf_info_divtc.name,
465 p->deghost?"ON":"OFF",
466 10.0*log10(s1/s0));
469 /* analyze the data */
471 for(f=0; f<5; f++)
472 for(sum[f]=0, n=-15; n<20; n+=5)
473 sum[f]+=buf[n+f];
475 for(f=0; f<b; f++)
477 m=match(p, sum, -1, -1, &d);
479 if(d>=p->threshold)
480 pbuf[f]=m;
482 if(f<b-1)
483 for(n=0; n<5; n++)
484 sum[n]=sum[n]-buf[5*(f-3)+n]+buf[5*(f+4)+n];
487 /* fill in the gaps */
489 /* the beginning */
490 for(f=0; f<b && pbuf[f]==-1; f++);
492 if(f==b)
494 free(buf-15);
495 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: No telecine pattern found!\n",
496 vf_info_divtc.name);
497 return 0;
500 for(n=0; n<f; pbuf[n++]=pbuf[f]);
502 /* the end */
503 for(f=b-1; pbuf[f]==-1; f--);
504 for(n=f+1; n<b; pbuf[n++]=pbuf[f]);
506 /* the rest */
507 for(f=0;;)
509 while(f<b && pbuf[f]!=-1) f++;
510 if(f==b) break;
511 for(n=f; pbuf[n]==-1; n++);
513 if(pbuf[f-1]==pbuf[n])
515 /* just a gap */
516 while(f<n) pbuf[f++]=pbuf[n];
518 else
520 /* phase change, reanalyze the original data in the gap with zero
521 threshold for only the two phases that appear at the ends */
523 for(i=0; i<5; i++)
524 for(sum[i]=0, j=5*f-15; j<5*f; j+=5)
525 sum[i]+=buf[i+j];
527 for(i=f; i<n; i++)
529 pbuf[i]=match(p, sum, pbuf[f-1], pbuf[n], 0);
531 for(j=0; j<5; j++)
532 sum[j]=sum[j]-buf[5*(i-3)+j]+buf[5*(i+4)+j];
535 /* estimate the transition point by dividing the gap
536 in the same proportion as the number of matches of each kind */
538 for(i=f, m=f; i<n; i++)
539 if(pbuf[i]==pbuf[f-1]) m++;
541 /* find the transition of the right direction nearest to the
542 estimated point */
544 if(m>f && m<n)
546 for(j=m; j>f; j--)
547 if(pbuf[j-1]==pbuf[f-1] && pbuf[j]==pbuf[n]) break;
548 for(s=m; s<n; s++)
549 if(pbuf[s-1]==pbuf[f-1] && pbuf[s]==pbuf[n]) break;
551 m=(s-m<m-j)?s:j;
554 /* and rewrite the data to allow only this one transition */
556 for(i=f; i<m; i++)
557 pbuf[i]=pbuf[f-1];
559 for(; i<n; i++)
560 pbuf[i]=pbuf[n];
562 f=n;
566 free(buf-15);
568 return 1;
571 static int query_format(struct vf_instance *vf, unsigned int fmt)
573 switch(fmt)
575 case IMGFMT_444P: case IMGFMT_IYUV: case IMGFMT_RGB24:
576 case IMGFMT_422P: case IMGFMT_UYVY: case IMGFMT_BGR24:
577 case IMGFMT_411P: case IMGFMT_YUY2: case IMGFMT_IF09:
578 case IMGFMT_YV12: case IMGFMT_I420: case IMGFMT_YVU9:
579 case IMGFMT_IUYV: case IMGFMT_Y800: case IMGFMT_Y8:
580 return vf_next_query_format(vf,fmt);
583 return 0;
586 static void uninit(struct vf_instance *vf)
588 if(vf->priv)
590 if(vf->priv->file) fclose(vf->priv->file);
591 if(vf->priv->csdata) free(vf->priv->csdata-15);
592 free(vf->priv->bdata);
593 free(vf->priv->history);
594 free(vf->priv);
598 static int vf_open(vf_instance_t *vf, char *args)
600 struct vf_priv_s *p;
601 char *filename="framediff.log", *ap, *q, *a;
603 if(args && !(args=strdup(args)))
605 nomem:
606 mp_msg(MSGT_VFILTER, MSGL_FATAL,
607 "%s: Not enough memory.\n", vf->info->name);
608 fail:
609 uninit(vf);
610 free(args);
611 return 0;
614 vf->put_image=put_image;
615 vf->uninit=uninit;
616 vf->query_format=query_format;
617 vf->default_reqs=VFCAP_ACCEPT_STRIDE;
618 if(!(vf->priv=p=calloc(1, sizeof(struct vf_priv_s))))
619 goto nomem;
621 p->phase=5;
622 p->threshold=0.5;
623 p->window=30;
625 if((ap=args))
626 while(*ap)
628 q=ap;
629 if((ap=strchr(q, ':'))) *ap++=0; else ap=q+strlen(q);
630 if((a=strchr(q, '='))) *a++=0; else a=q+strlen(q);
632 switch(*q)
634 case 0: break;
635 case 'f': filename=a; break;
636 case 't': p->threshold=atof(a); break;
637 case 'w': p->window=5*(atoi(a)+4)/5; break;
638 case 'd': p->deghost=atoi(a); break;
639 case 'p':
640 if(q[1]=='h') p->phase=atoi(a);
641 else p->pass=atoi(a);
642 break;
644 case 'h':
645 mp_msg(MSGT_VFILTER, MSGL_INFO,
646 "\n%s options:\n\n"
647 "pass=1|2 - Use 2-pass mode.\n"
648 "file=filename - Set the 2-pass log file name "
649 "(default %s).\n"
650 "threshold=value - Set the pattern recognition "
651 "sensitivity (default %g).\n"
652 "deghost=value - Select deghosting threshold "
653 "(default %d).\n"
654 "window=numframes - Set the statistics window "
655 "for 1-pass mode (default %d).\n"
656 "phase=0|1|2|3|4 - Set the initial phase "
657 "for 1-pass mode (default %d).\n\n"
658 "The option names can be abbreviated to the shortest "
659 "unique prefix.\n\n",
660 vf->info->name, filename, p->threshold, p->deghost,
661 p->window, p->phase%5);
662 break;
664 default:
665 mp_msg(MSGT_VFILTER, MSGL_FATAL,
666 "%s: Unknown argument %s.\n", vf->info->name, q);
667 goto fail;
671 switch(p->pass)
673 case 1:
674 if(!(p->file=fopen(filename, "w")))
676 mp_msg(MSGT_VFILTER, MSGL_FATAL,
677 "%s: Can't create file %s.\n", vf->info->name, filename);
678 goto fail;
681 break;
683 case 2:
684 if(!(p->file=fopen(filename, "r")))
686 mp_msg(MSGT_VFILTER, MSGL_FATAL,
687 "%s: Can't open file %s.\n", vf->info->name, filename);
688 goto fail;
691 if(!analyze(p))
692 goto fail;
694 fclose(p->file);
695 p->file=0;
696 break;
699 if(p->window<5) p->window=5;
700 if(!(p->history=calloc(sizeof *p->history, p->window)))
701 goto nomem;
703 diff = diff_C;
704 #if HAVE_MMX && HAVE_EBX_AVAILABLE
705 if(gCpuCaps.hasMMX) diff = diff_MMX;
706 #endif
708 free(args);
709 return 1;
712 const vf_info_t vf_info_divtc =
714 "inverse telecine for deinterlaced video",
715 "divtc",
716 "Ville Saari",
718 vf_open,
719 NULL