typo fixes
[mplayer/greg.git] / libmpcodecs / vf_divtc.c
blob2efd7a33be83dd0b022f407b2205f86c9149d098
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <math.h>
7 #include "config.h"
8 #include "mp_msg.h"
9 #include "cpudetect.h"
10 #include "bswap.h"
11 #include "asmalign.h"
13 #include "img_format.h"
14 #include "mp_image.h"
15 #include "vf.h"
17 #include "libvo/fastmemcpy.h"
19 vf_info_t vf_info_divtc;
21 struct vf_priv_s
23 int deghost, pass, phase, window, fcount, bcount, frameno, misscount,
24 ocount, sum[5];
25 double threshold;
26 FILE *file;
27 char *bdata;
28 unsigned int *csdata;
29 int *history;
33 * diff_MMX and diff_C stolen from vf_decimate.c
36 #ifdef HAVE_MMX
37 static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
39 volatile short out[4];
40 asm (
41 "movl $8, %%ecx \n\t"
42 "pxor %%mm4, %%mm4 \n\t"
43 "pxor %%mm7, %%mm7 \n\t"
45 ASMALIGN16
46 "1: \n\t"
48 "movq (%%"REG_S"), %%mm0 \n\t"
49 "movq (%%"REG_S"), %%mm2 \n\t"
50 "add %%"REG_a", %%"REG_S" \n\t"
51 "movq (%%"REG_D"), %%mm1 \n\t"
52 "add %%"REG_b", %%"REG_D" \n\t"
53 "psubusb %%mm1, %%mm2 \n\t"
54 "psubusb %%mm0, %%mm1 \n\t"
55 "movq %%mm2, %%mm0 \n\t"
56 "movq %%mm1, %%mm3 \n\t"
57 "punpcklbw %%mm7, %%mm0 \n\t"
58 "punpcklbw %%mm7, %%mm1 \n\t"
59 "punpckhbw %%mm7, %%mm2 \n\t"
60 "punpckhbw %%mm7, %%mm3 \n\t"
61 "paddw %%mm0, %%mm4 \n\t"
62 "paddw %%mm1, %%mm4 \n\t"
63 "paddw %%mm2, %%mm4 \n\t"
64 "paddw %%mm3, %%mm4 \n\t"
66 "decl %%ecx \n\t"
67 "jnz 1b \n\t"
68 "movq %%mm4, (%%"REG_d") \n\t"
69 "emms \n\t"
71 : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
72 : "memory"
74 return out[0]+out[1]+out[2]+out[3];
76 #endif
78 static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
80 int x, y, d=0;
82 for(y=8; y; y--, new+=ns, old+=os)
83 for(x=8; x; x--)
84 d+=abs(new[x]-old[x]);
86 return d;
89 static int (*diff)(unsigned char *, unsigned char *, int, int);
91 static int diff_plane(unsigned char *old, unsigned char *new,
92 int w, int h, int os, int ns, int arg)
94 int x, y, d, max=0, sum=0, n=0;
96 for(y=0; y<h-7; y+=8)
98 for(x=0; x<w-7; x+=8)
100 d=diff(old+x+y*os, new+x+y*ns, os, ns);
101 if(d>max) max=d;
102 sum+=d;
103 n++;
107 return (sum+n*max)/2;
111 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
112 int w, int h, int s, int zs, int arg)
114 unsigned int shift, sum;
115 unsigned char *e;
117 for(sum=0; h; h--, p+=s-w)
118 for(e=p+w, shift=32; p<e;)
119 sum^=(*p++)<<(shift=(shift-8)&31);
121 return sum;
125 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
126 int w, int h, int s, int zs, int arg)
128 unsigned int shift;
129 uint32_t sum, t;
130 unsigned char *e, *e2;
131 #if MP_WORDSIZE==64
132 typedef uint64_t wsum_t;
133 #else
134 typedef uint32_t wsum_t;
135 #endif
136 wsum_t wsum;
138 for(sum=0; h; h--, p+=s-w)
140 for(shift=0, e=p+w; (int)p&(sizeof(wsum_t)-1) && p<e;)
141 sum^=*p++<<(shift=(shift-8)&31);
143 for(wsum=0, e2=e-sizeof(wsum_t)+1; p<e2; p+=sizeof(wsum_t))
144 wsum^=*(wsum_t *)p;
146 #if MP_WORDSIZE==64
147 t=be2me_32((uint32_t)(wsum>>32^wsum));
148 #else
149 t=be2me_32(wsum);
150 #endif
152 for(sum^=(t<<shift|t>>(32-shift)); p<e;)
153 sum^=*p++<<(shift=(shift-8)&31);
156 return sum;
159 static int deghost_plane(unsigned char *d, unsigned char *s,
160 int w, int h, int ds, int ss, int threshold)
162 int t;
163 unsigned char *e;
165 for(; h; h--, s+=ss-w, d+=ds-w)
166 for(e=d+w; d<e; d++, s++)
167 if(abs(*d-*s)>=threshold)
168 *d=(t=(*d<<1)-*s)<0?0:t>255?255:t;
170 return 0;
173 static int imgop(int(*planeop)(unsigned char *, unsigned char *,
174 int, int, int, int, int),
175 mp_image_t *dst, mp_image_t *src, int arg)
177 if(dst->flags&MP_IMGFLAG_PLANAR)
178 return planeop(dst->planes[0], src?src->planes[0]:0,
179 dst->w, dst->h,
180 dst->stride[0], src?src->stride[0]:0, arg)+
181 planeop(dst->planes[1], src?src->planes[1]:0,
182 dst->chroma_width, dst->chroma_height,
183 dst->stride[1], src?src->stride[1]:0, arg)+
184 planeop(dst->planes[2], src?src->planes[2]:0,
185 dst->chroma_width, dst->chroma_height,
186 dst->stride[2], src?src->stride[2]:0, arg);
188 return planeop(dst->planes[0], src?src->planes[0]:0,
189 dst->w*(dst->bpp/8), dst->h,
190 dst->stride[0], src?src->stride[0]:0, arg);
194 * Find the phase in which the telecine pattern fits best to the
195 * given 5 frame slice of frame difference measurements.
197 * If phase1 and phase2 are not negative, only the two specified
198 * phases are tested.
201 static int cmp(int *a, int *b) { return *b-*a; }
203 static int match(struct vf_priv_s *p, int *diffs,
204 int phase1, int phase2, double *strength)
206 static const int pattern1[]={ -4, 1, 1, 1, 1 },
207 pattern2[]={ -2, -3, 4, 4, -3 }, *pattern;
208 int f, m, n, t[5];
210 pattern=p->deghost>0?pattern2:pattern1;
212 for(f=0; f<5; f++)
214 if(phase1<0 || phase2<0 || f==phase1 || f==phase2)
216 for(n=t[f]=0; n<5; n++)
217 t[f]+=diffs[n]*pattern[(n-f+5)%5];
219 else
220 t[f]=INT_MIN;
223 /* find the best match */
224 for(m=0, n=1; n<5; n++)
225 if(t[n]>t[m]) m=n;
227 if(strength)
229 /* the second best match */
230 for(f=m?0:1, n=f+1; n<5; n++)
231 if(n!=m && t[n]>t[f]) f=n;
233 *strength=(t[m]>0?(double)(t[m]-t[f])/t[m]:0.0);
236 return m;
239 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
241 mp_image_t *dmpi, *tmpi=0;
242 int n, m, f, newphase;
243 struct vf_priv_s *p=vf->priv;
244 unsigned int checksum;
245 double d;
247 dmpi=vf_get_image(vf->next, mpi->imgfmt,
248 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
249 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
250 mpi->width, mpi->height);
251 vf_clone_mpi_attributes(dmpi, mpi);
253 newphase=p->phase;
255 switch(p->pass)
257 case 1:
258 fprintf(p->file, "%08x %d\n",
259 (unsigned int)imgop((void *)checksum_plane, mpi, 0, 0),
260 p->frameno?imgop(diff_plane, dmpi, mpi, 0):0);
261 break;
263 case 2:
264 if(p->frameno/5>p->bcount)
266 mp_msg(MSGT_VFILTER, MSGL_ERR,
267 "\n%s: Log file ends prematurely! "
268 "Switching to one pass mode.\n", vf->info->name);
269 p->pass=0;
270 break;
273 checksum=(unsigned int)imgop((void *)checksum_plane, mpi, 0, 0);
275 if(checksum!=p->csdata[p->frameno])
277 for(f=0; f<100; f++)
278 if(p->frameno+f<p->fcount && p->csdata[p->frameno+f]==checksum)
279 break;
280 else if(p->frameno-f>=0 && p->csdata[p->frameno-f]==checksum)
282 f=-f;
283 break;
286 if(f<100)
288 mp_msg(MSGT_VFILTER, MSGL_INFO,
289 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
290 vf->info->name, f);
292 p->frameno+=f;
293 p->misscount=0;
295 else if(p->misscount++>=30)
297 mp_msg(MSGT_VFILTER, MSGL_ERR,
298 "\n%s: Sync with pass-1 lost! "
299 "Switching to one pass mode.\n", vf->info->name);
300 p->pass=0;
301 break;
305 n=(p->frameno)/5;
306 if(n>=p->bcount) n=p->bcount-1;
308 newphase=p->bdata[n];
309 break;
311 default:
312 if(p->frameno)
314 int *sump=p->sum+p->frameno%5,
315 *histp=p->history+p->frameno%p->window;
317 *sump-=*histp;
318 *sump+=(*histp=imgop(diff_plane, dmpi, mpi, 0));
321 m=match(p, p->sum, -1, -1, &d);
323 if(d>=p->threshold)
324 newphase=m;
327 n=p->ocount++%5;
329 if(newphase!=p->phase && ((p->phase+4)%5<n)==((newphase+4)%5<n))
331 p->phase=newphase;
332 mp_msg(MSGT_VFILTER, MSGL_STATUS,
333 "\n%s: Telecine phase %d.\n", vf->info->name, p->phase);
336 switch((p->frameno++-p->phase+10)%5)
338 case 0:
339 imgop((void *)memcpy_pic, dmpi, mpi, 0);
340 return 0;
342 case 4:
343 if(p->deghost>0)
345 tmpi=vf_get_image(vf->next, mpi->imgfmt,
346 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
347 MP_IMGFLAG_READABLE,
348 mpi->width, mpi->height);
349 vf_clone_mpi_attributes(tmpi, mpi);
351 imgop((void *)memcpy_pic, tmpi, mpi, 0);
352 imgop(deghost_plane, tmpi, dmpi, p->deghost);
353 imgop((void *)memcpy_pic, dmpi, mpi, 0);
354 return vf_next_put_image(vf, tmpi, MP_NOPTS_VALUE);
358 imgop((void *)memcpy_pic, dmpi, mpi, 0);
359 return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
362 static int analyze(struct vf_priv_s *p)
364 int *buf=0, *bp, bufsize=0, n, b, f, i, j, m, s;
365 unsigned int *cbuf=0, *cp;
366 char *pbuf;
367 char lbuf[256];
368 int sum[5];
369 double d;
371 /* read the file */
373 n=15;
374 while(fgets(lbuf, 256, p->file))
376 if(n>=bufsize-19)
378 bufsize=bufsize?bufsize*2:30000;
379 if((bp=realloc(buf, bufsize*sizeof *buf))) buf=bp;
380 if((cp=realloc(cbuf, bufsize*sizeof *cbuf))) cbuf=cp;
382 if(!bp || !cp)
384 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Not enough memory.\n",
385 vf_info_divtc.name);
386 free(buf);
387 free(cbuf);
388 return 0;
391 sscanf(lbuf, "%x %d", cbuf+n, buf+n);
392 n++;
395 if(!n)
397 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Empty 2-pass log file.\n",
398 vf_info_divtc.name);
399 free(buf);
400 free(cbuf);
401 return 0;
404 /* generate some dummy data past the beginning and end of the array */
406 buf+=15, cbuf+=15;
407 n-=15;
409 memcpy(buf-15, buf, 15*sizeof *buf);
410 memset(cbuf-15, 0, 15*sizeof *cbuf);
412 while(n%5)
413 buf[n]=buf[n-5], cbuf[n]=0, n++;
415 memcpy(buf+n, buf+n-15, 15*sizeof *buf);
416 memset(cbuf+n, 0, 15*sizeof *cbuf);
418 p->csdata=cbuf;
419 p->fcount=n;
421 /* array with one slot for each slice of 5 frames */
423 p->bdata=pbuf=malloc(p->bcount=b=(n/5));
424 memset(pbuf, 255, b);
426 /* resolve the automatic mode */
428 if(p->deghost<0)
430 int deghost=-p->deghost;
431 double s0=0.0, s1=0.0;
433 for(f=0; f<n; f+=5)
435 p->deghost=0; match(p, buf+f, -1, -1, &d); s0+=d;
436 p->deghost=1; match(p, buf+f, -1, -1, &d); s1+=d;
439 p->deghost=s1>s0?deghost:0;
441 mp_msg(MSGT_VFILTER, MSGL_INFO,
442 "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
443 vf_info_divtc.name,
444 p->deghost?"ON":"OFF",
445 10.0*log10(s1/s0));
448 /* analyze the data */
450 for(f=0; f<5; f++)
451 for(sum[f]=0, n=-15; n<20; n+=5)
452 sum[f]+=buf[n+f];
454 for(f=0; f<b; f++)
456 m=match(p, sum, -1, -1, &d);
458 if(d>=p->threshold)
459 pbuf[f]=m;
461 if(f<b-1)
462 for(n=0; n<5; n++)
463 sum[n]=sum[n]-buf[5*(f-3)+n]+buf[5*(f+4)+n];
466 /* fill in the gaps */
468 /* the beginning */
469 for(f=0; f<b && pbuf[f]==-1; f++);
471 if(f==b)
473 free(buf-15);
474 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: No telecine pattern found!\n",
475 vf_info_divtc.name);
476 return 0;
479 for(n=0; n<f; pbuf[n++]=pbuf[f]);
481 /* the end */
482 for(f=b-1; pbuf[f]==-1; f--);
483 for(n=f+1; n<b; pbuf[n++]=pbuf[f]);
485 /* the rest */
486 for(f=0;;)
488 while(f<b && pbuf[f]!=-1) f++;
489 if(f==b) break;
490 for(n=f; pbuf[n]==-1; n++);
492 if(pbuf[f-1]==pbuf[n])
494 /* just a gap */
495 while(f<n) pbuf[f++]=pbuf[n];
497 else
499 /* phase change, reanalyze the original data in the gap with zero
500 threshold for only the two phases that appear at the ends */
502 for(i=0; i<5; i++)
503 for(sum[i]=0, j=5*f-15; j<5*f; j+=5)
504 sum[i]+=buf[i+j];
506 for(i=f; i<n; i++)
508 pbuf[i]=match(p, sum, pbuf[f-1], pbuf[n], 0);
510 for(j=0; j<5; j++)
511 sum[j]=sum[j]-buf[5*(i-3)+j]+buf[5*(i+4)+j];
514 /* estimate the transition point by dividing the gap
515 in the same proportion as the number of matches of each kind */
517 for(i=f, m=f; i<n; i++)
518 if(pbuf[i]==pbuf[f-1]) m++;
520 /* find the transition of the right direction nearest to the
521 estimated point */
523 if(m>f && m<n)
525 for(j=m; j>f; j--)
526 if(pbuf[j-1]==pbuf[f-1] && pbuf[j]==pbuf[n]) break;
527 for(s=m; s<n; s++)
528 if(pbuf[s-1]==pbuf[f-1] && pbuf[s]==pbuf[n]) break;
530 m=(s-m<m-j)?s:j;
533 /* and rewrite the data to allow only this one transition */
535 for(i=f; i<m; i++)
536 pbuf[i]=pbuf[f-1];
538 for(; i<n; i++)
539 pbuf[i]=pbuf[n];
541 f=n;
545 free(buf-15);
547 return 1;
550 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
552 switch(fmt)
554 case IMGFMT_444P: case IMGFMT_IYUV: case IMGFMT_RGB24:
555 case IMGFMT_422P: case IMGFMT_UYVY: case IMGFMT_BGR24:
556 case IMGFMT_411P: case IMGFMT_YUY2: case IMGFMT_IF09:
557 case IMGFMT_YV12: case IMGFMT_I420: case IMGFMT_YVU9:
558 case IMGFMT_IUYV: case IMGFMT_Y800: case IMGFMT_Y8:
559 return vf_next_query_format(vf,fmt);
562 return 0;
565 static void uninit(struct vf_instance_s* vf)
567 if(vf->priv)
569 if(vf->priv->file) fclose(vf->priv->file);
570 if(vf->priv->csdata) free(vf->priv->csdata-15);
571 free(vf->priv->bdata);
572 free(vf->priv->history);
573 free(vf->priv);
577 static int open(vf_instance_t *vf, char* args)
579 struct vf_priv_s *p;
580 char *filename="framediff.log", *ap, *q, *a;
582 if(args && !(args=strdup(args)))
584 nomem:
585 mp_msg(MSGT_VFILTER, MSGL_FATAL,
586 "%s: Not enough memory.\n", vf->info->name);
587 fail:
588 uninit(vf);
589 free(args);
590 return 0;
593 vf->put_image=put_image;
594 vf->uninit=uninit;
595 vf->query_format=query_format;
596 vf->default_reqs=VFCAP_ACCEPT_STRIDE;
597 if(!(vf->priv=p=calloc(1, sizeof(struct vf_priv_s))))
598 goto nomem;
600 p->phase=5;
601 p->threshold=0.5;
602 p->window=30;
604 if((ap=args))
605 while(*ap)
607 q=ap;
608 if((ap=strchr(q, ':'))) *ap++=0; else ap=q+strlen(q);
609 if((a=strchr(q, '='))) *a++=0; else a=q+strlen(q);
611 switch(*q)
613 case 0: break;
614 case 'f': filename=a; break;
615 case 't': p->threshold=atof(a); break;
616 case 'w': p->window=5*(atoi(a)+4)/5; break;
617 case 'd': p->deghost=atoi(a); break;
618 case 'p':
619 if(q[1]=='h') p->phase=atoi(a);
620 else p->pass=atoi(a);
621 break;
623 case 'h':
624 mp_msg(MSGT_VFILTER, MSGL_INFO,
625 "\n%s options:\n\n"
626 "pass=1|2 - Use 2-pass mode.\n"
627 "file=filename - Set the 2-pass log file name "
628 "(default %s).\n"
629 "threshold=value - Set the pattern recognition "
630 "sensitivity (default %g).\n"
631 "deghost=value - Select deghosting threshold "
632 "(default %d).\n"
633 "window=numframes - Set the statistics window "
634 "for 1-pass mode (default %d).\n"
635 "phase=0|1|2|3|4 - Set the initial phase "
636 "for 1-pass mode (default %d).\n\n"
637 "The option names can be abbreviated to the shortest "
638 "unique prefix.\n\n",
639 vf->info->name, filename, p->threshold, p->deghost,
640 p->window, p->phase%5);
641 break;
643 default:
644 mp_msg(MSGT_VFILTER, MSGL_FATAL,
645 "%s: Unknown argument %s.\n", vf->info->name, q);
646 goto fail;
650 switch(p->pass)
652 case 1:
653 if(!(p->file=fopen(filename, "w")))
655 mp_msg(MSGT_VFILTER, MSGL_FATAL,
656 "%s: Can't create file %s.\n", vf->info->name, filename);
657 goto fail;
660 break;
662 case 2:
663 if(!(p->file=fopen(filename, "r")))
665 mp_msg(MSGT_VFILTER, MSGL_FATAL,
666 "%s: Can't open file %s.\n", vf->info->name, filename);
667 goto fail;
670 if(!analyze(p))
671 goto fail;
673 fclose(p->file);
674 p->file=0;
675 break;
678 if(p->window<5) p->window=5;
679 if(!(p->history=calloc(sizeof *p->history, p->window)))
680 goto nomem;
682 diff=
683 #ifdef HAVE_MMX
684 gCpuCaps.hasMMX?diff_MMX:
685 #endif
686 diff_C;
688 free(args);
689 return 1;
692 vf_info_t vf_info_divtc =
694 "inverse telecine for deinterlaced video",
695 "divtc",
696 "Ville Saari",
698 open,
699 NULL