Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_divtc.c
blobd2fcb48dbe849c18676d93660ce0c94a84528dac
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 "libavutil/common.h"
11 #include "mpbswap.h"
13 #include "img_format.h"
14 #include "mp_image.h"
15 #include "vf.h"
17 #include "libvo/fastmemcpy.h"
19 const 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 ASMALIGN(4)
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 copyop(unsigned char *d, unsigned char *s, int bpl, int h, int dstride, int sstride, int dummy) {
174 memcpy_pic(d, s, bpl, h, dstride, sstride);
175 return 0;
178 static int imgop(int(*planeop)(unsigned char *, unsigned char *,
179 int, int, int, int, int),
180 mp_image_t *dst, mp_image_t *src, int arg)
182 if(dst->flags&MP_IMGFLAG_PLANAR)
183 return planeop(dst->planes[0], src?src->planes[0]:0,
184 dst->w, dst->h,
185 dst->stride[0], src?src->stride[0]:0, arg)+
186 planeop(dst->planes[1], src?src->planes[1]:0,
187 dst->chroma_width, dst->chroma_height,
188 dst->stride[1], src?src->stride[1]:0, arg)+
189 planeop(dst->planes[2], src?src->planes[2]:0,
190 dst->chroma_width, dst->chroma_height,
191 dst->stride[2], src?src->stride[2]:0, arg);
193 return planeop(dst->planes[0], src?src->planes[0]:0,
194 dst->w*(dst->bpp/8), dst->h,
195 dst->stride[0], src?src->stride[0]:0, arg);
199 * Find the phase in which the telecine pattern fits best to the
200 * given 5 frame slice of frame difference measurements.
202 * If phase1 and phase2 are not negative, only the two specified
203 * phases are tested.
206 static int match(struct vf_priv_s *p, int *diffs,
207 int phase1, int phase2, double *strength)
209 static const int pattern1[]={ -4, 1, 1, 1, 1 },
210 pattern2[]={ -2, -3, 4, 4, -3 }, *pattern;
211 int f, m, n, t[5];
213 pattern=p->deghost>0?pattern2:pattern1;
215 for(f=0; f<5; f++)
217 if(phase1<0 || phase2<0 || f==phase1 || f==phase2)
219 for(n=t[f]=0; n<5; n++)
220 t[f]+=diffs[n]*pattern[(n-f+5)%5];
222 else
223 t[f]=INT_MIN;
226 /* find the best match */
227 for(m=0, n=1; n<5; n++)
228 if(t[n]>t[m]) m=n;
230 if(strength)
232 /* the second best match */
233 for(f=m?0:1, n=f+1; n<5; n++)
234 if(n!=m && t[n]>t[f]) f=n;
236 *strength=(t[m]>0?(double)(t[m]-t[f])/t[m]:0.0);
239 return m;
242 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
244 mp_image_t *dmpi, *tmpi=0;
245 int n, m, f, newphase;
246 struct vf_priv_s *p=vf->priv;
247 unsigned int checksum;
248 double d;
250 dmpi=vf_get_image(vf->next, mpi->imgfmt,
251 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
252 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
253 mpi->width, mpi->height);
254 vf_clone_mpi_attributes(dmpi, mpi);
256 newphase=p->phase;
258 switch(p->pass)
260 case 1:
261 fprintf(p->file, "%08x %d\n",
262 (unsigned int)imgop((void *)checksum_plane, mpi, 0, 0),
263 p->frameno?imgop(diff_plane, dmpi, mpi, 0):0);
264 break;
266 case 2:
267 if(p->frameno/5>p->bcount)
269 mp_msg(MSGT_VFILTER, MSGL_ERR,
270 "\n%s: Log file ends prematurely! "
271 "Switching to one pass mode.\n", vf->info->name);
272 p->pass=0;
273 break;
276 checksum=(unsigned int)imgop((void *)checksum_plane, mpi, 0, 0);
278 if(checksum!=p->csdata[p->frameno])
280 for(f=0; f<100; f++)
281 if(p->frameno+f<p->fcount && p->csdata[p->frameno+f]==checksum)
282 break;
283 else if(p->frameno-f>=0 && p->csdata[p->frameno-f]==checksum)
285 f=-f;
286 break;
289 if(f<100)
291 mp_msg(MSGT_VFILTER, MSGL_INFO,
292 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
293 vf->info->name, f);
295 p->frameno+=f;
296 p->misscount=0;
298 else if(p->misscount++>=30)
300 mp_msg(MSGT_VFILTER, MSGL_ERR,
301 "\n%s: Sync with pass-1 lost! "
302 "Switching to one pass mode.\n", vf->info->name);
303 p->pass=0;
304 break;
308 n=(p->frameno)/5;
309 if(n>=p->bcount) n=p->bcount-1;
311 newphase=p->bdata[n];
312 break;
314 default:
315 if(p->frameno)
317 int *sump=p->sum+p->frameno%5,
318 *histp=p->history+p->frameno%p->window;
320 *sump-=*histp;
321 *sump+=(*histp=imgop(diff_plane, dmpi, mpi, 0));
324 m=match(p, p->sum, -1, -1, &d);
326 if(d>=p->threshold)
327 newphase=m;
330 n=p->ocount++%5;
332 if(newphase!=p->phase && ((p->phase+4)%5<n)==((newphase+4)%5<n))
334 p->phase=newphase;
335 mp_msg(MSGT_VFILTER, MSGL_STATUS,
336 "\n%s: Telecine phase %d.\n", vf->info->name, p->phase);
339 switch((p->frameno++-p->phase+10)%5)
341 case 0:
342 imgop(copyop, dmpi, mpi, 0);
343 return 0;
345 case 4:
346 if(p->deghost>0)
348 tmpi=vf_get_image(vf->next, mpi->imgfmt,
349 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
350 MP_IMGFLAG_READABLE,
351 mpi->width, mpi->height);
352 vf_clone_mpi_attributes(tmpi, mpi);
354 imgop(copyop, tmpi, mpi, 0);
355 imgop(deghost_plane, tmpi, dmpi, p->deghost);
356 imgop(copyop, dmpi, mpi, 0);
357 return vf_next_put_image(vf, tmpi, MP_NOPTS_VALUE);
361 imgop(copyop, dmpi, mpi, 0);
362 return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
365 static int analyze(struct vf_priv_s *p)
367 int *buf=0, *bp, bufsize=0, n, b, f, i, j, m, s;
368 unsigned int *cbuf=0, *cp;
369 char *pbuf;
370 char lbuf[256];
371 int sum[5];
372 double d;
374 /* read the file */
376 n=15;
377 while(fgets(lbuf, 256, p->file))
379 if(n>=bufsize-19)
381 bufsize=bufsize?bufsize*2:30000;
382 if((bp=realloc(buf, bufsize*sizeof *buf))) buf=bp;
383 if((cp=realloc(cbuf, bufsize*sizeof *cbuf))) cbuf=cp;
385 if(!bp || !cp)
387 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Not enough memory.\n",
388 vf_info_divtc.name);
389 free(buf);
390 free(cbuf);
391 return 0;
394 sscanf(lbuf, "%x %d", cbuf+n, buf+n);
395 n++;
398 if(!n)
400 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Empty 2-pass log file.\n",
401 vf_info_divtc.name);
402 free(buf);
403 free(cbuf);
404 return 0;
407 /* generate some dummy data past the beginning and end of the array */
409 buf+=15, cbuf+=15;
410 n-=15;
412 memcpy(buf-15, buf, 15*sizeof *buf);
413 memset(cbuf-15, 0, 15*sizeof *cbuf);
415 while(n%5)
416 buf[n]=buf[n-5], cbuf[n]=0, n++;
418 memcpy(buf+n, buf+n-15, 15*sizeof *buf);
419 memset(cbuf+n, 0, 15*sizeof *cbuf);
421 p->csdata=cbuf;
422 p->fcount=n;
424 /* array with one slot for each slice of 5 frames */
426 p->bdata=pbuf=malloc(p->bcount=b=(n/5));
427 memset(pbuf, 255, b);
429 /* resolve the automatic mode */
431 if(p->deghost<0)
433 int deghost=-p->deghost;
434 double s0=0.0, s1=0.0;
436 for(f=0; f<n; f+=5)
438 p->deghost=0; match(p, buf+f, -1, -1, &d); s0+=d;
439 p->deghost=1; match(p, buf+f, -1, -1, &d); s1+=d;
442 p->deghost=s1>s0?deghost:0;
444 mp_msg(MSGT_VFILTER, MSGL_INFO,
445 "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
446 vf_info_divtc.name,
447 p->deghost?"ON":"OFF",
448 10.0*log10(s1/s0));
451 /* analyze the data */
453 for(f=0; f<5; f++)
454 for(sum[f]=0, n=-15; n<20; n+=5)
455 sum[f]+=buf[n+f];
457 for(f=0; f<b; f++)
459 m=match(p, sum, -1, -1, &d);
461 if(d>=p->threshold)
462 pbuf[f]=m;
464 if(f<b-1)
465 for(n=0; n<5; n++)
466 sum[n]=sum[n]-buf[5*(f-3)+n]+buf[5*(f+4)+n];
469 /* fill in the gaps */
471 /* the beginning */
472 for(f=0; f<b && pbuf[f]==-1; f++);
474 if(f==b)
476 free(buf-15);
477 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: No telecine pattern found!\n",
478 vf_info_divtc.name);
479 return 0;
482 for(n=0; n<f; pbuf[n++]=pbuf[f]);
484 /* the end */
485 for(f=b-1; pbuf[f]==-1; f--);
486 for(n=f+1; n<b; pbuf[n++]=pbuf[f]);
488 /* the rest */
489 for(f=0;;)
491 while(f<b && pbuf[f]!=-1) f++;
492 if(f==b) break;
493 for(n=f; pbuf[n]==-1; n++);
495 if(pbuf[f-1]==pbuf[n])
497 /* just a gap */
498 while(f<n) pbuf[f++]=pbuf[n];
500 else
502 /* phase change, reanalyze the original data in the gap with zero
503 threshold for only the two phases that appear at the ends */
505 for(i=0; i<5; i++)
506 for(sum[i]=0, j=5*f-15; j<5*f; j+=5)
507 sum[i]+=buf[i+j];
509 for(i=f; i<n; i++)
511 pbuf[i]=match(p, sum, pbuf[f-1], pbuf[n], 0);
513 for(j=0; j<5; j++)
514 sum[j]=sum[j]-buf[5*(i-3)+j]+buf[5*(i+4)+j];
517 /* estimate the transition point by dividing the gap
518 in the same proportion as the number of matches of each kind */
520 for(i=f, m=f; i<n; i++)
521 if(pbuf[i]==pbuf[f-1]) m++;
523 /* find the transition of the right direction nearest to the
524 estimated point */
526 if(m>f && m<n)
528 for(j=m; j>f; j--)
529 if(pbuf[j-1]==pbuf[f-1] && pbuf[j]==pbuf[n]) break;
530 for(s=m; s<n; s++)
531 if(pbuf[s-1]==pbuf[f-1] && pbuf[s]==pbuf[n]) break;
533 m=(s-m<m-j)?s:j;
536 /* and rewrite the data to allow only this one transition */
538 for(i=f; i<m; i++)
539 pbuf[i]=pbuf[f-1];
541 for(; i<n; i++)
542 pbuf[i]=pbuf[n];
544 f=n;
548 free(buf-15);
550 return 1;
553 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
555 switch(fmt)
557 case IMGFMT_444P: case IMGFMT_IYUV: case IMGFMT_RGB24:
558 case IMGFMT_422P: case IMGFMT_UYVY: case IMGFMT_BGR24:
559 case IMGFMT_411P: case IMGFMT_YUY2: case IMGFMT_IF09:
560 case IMGFMT_YV12: case IMGFMT_I420: case IMGFMT_YVU9:
561 case IMGFMT_IUYV: case IMGFMT_Y800: case IMGFMT_Y8:
562 return vf_next_query_format(vf,fmt);
565 return 0;
568 static void uninit(struct vf_instance_s* vf)
570 if(vf->priv)
572 if(vf->priv->file) fclose(vf->priv->file);
573 if(vf->priv->csdata) free(vf->priv->csdata-15);
574 free(vf->priv->bdata);
575 free(vf->priv->history);
576 free(vf->priv);
580 static int open(vf_instance_t *vf, char* args)
582 struct vf_priv_s *p;
583 char *filename="framediff.log", *ap, *q, *a;
585 if(args && !(args=strdup(args)))
587 nomem:
588 mp_msg(MSGT_VFILTER, MSGL_FATAL,
589 "%s: Not enough memory.\n", vf->info->name);
590 fail:
591 uninit(vf);
592 free(args);
593 return 0;
596 vf->put_image=put_image;
597 vf->uninit=uninit;
598 vf->query_format=query_format;
599 vf->default_reqs=VFCAP_ACCEPT_STRIDE;
600 if(!(vf->priv=p=calloc(1, sizeof(struct vf_priv_s))))
601 goto nomem;
603 p->phase=5;
604 p->threshold=0.5;
605 p->window=30;
607 if((ap=args))
608 while(*ap)
610 q=ap;
611 if((ap=strchr(q, ':'))) *ap++=0; else ap=q+strlen(q);
612 if((a=strchr(q, '='))) *a++=0; else a=q+strlen(q);
614 switch(*q)
616 case 0: break;
617 case 'f': filename=a; break;
618 case 't': p->threshold=atof(a); break;
619 case 'w': p->window=5*(atoi(a)+4)/5; break;
620 case 'd': p->deghost=atoi(a); break;
621 case 'p':
622 if(q[1]=='h') p->phase=atoi(a);
623 else p->pass=atoi(a);
624 break;
626 case 'h':
627 mp_msg(MSGT_VFILTER, MSGL_INFO,
628 "\n%s options:\n\n"
629 "pass=1|2 - Use 2-pass mode.\n"
630 "file=filename - Set the 2-pass log file name "
631 "(default %s).\n"
632 "threshold=value - Set the pattern recognition "
633 "sensitivity (default %g).\n"
634 "deghost=value - Select deghosting threshold "
635 "(default %d).\n"
636 "window=numframes - Set the statistics window "
637 "for 1-pass mode (default %d).\n"
638 "phase=0|1|2|3|4 - Set the initial phase "
639 "for 1-pass mode (default %d).\n\n"
640 "The option names can be abbreviated to the shortest "
641 "unique prefix.\n\n",
642 vf->info->name, filename, p->threshold, p->deghost,
643 p->window, p->phase%5);
644 break;
646 default:
647 mp_msg(MSGT_VFILTER, MSGL_FATAL,
648 "%s: Unknown argument %s.\n", vf->info->name, q);
649 goto fail;
653 switch(p->pass)
655 case 1:
656 if(!(p->file=fopen(filename, "w")))
658 mp_msg(MSGT_VFILTER, MSGL_FATAL,
659 "%s: Can't create file %s.\n", vf->info->name, filename);
660 goto fail;
663 break;
665 case 2:
666 if(!(p->file=fopen(filename, "r")))
668 mp_msg(MSGT_VFILTER, MSGL_FATAL,
669 "%s: Can't open file %s.\n", vf->info->name, filename);
670 goto fail;
673 if(!analyze(p))
674 goto fail;
676 fclose(p->file);
677 p->file=0;
678 break;
681 if(p->window<5) p->window=5;
682 if(!(p->history=calloc(sizeof *p->history, p->window)))
683 goto nomem;
685 diff=
686 #ifdef HAVE_MMX
687 gCpuCaps.hasMMX?diff_MMX:
688 #endif
689 diff_C;
691 free(args);
692 return 1;
695 const vf_info_t vf_info_divtc =
697 "inverse telecine for deinterlaced video",
698 "divtc",
699 "Ville Saari",
701 open,
702 NULL