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];
60 "pxor %%mm4, %%mm4 \n\t"
61 "pxor %%mm7, %%mm7 \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"
86 "movq %%mm4, (%%"REG_d
") \n\t"
89 :
"S" (old),
"D" (
new),
"a" ((
long)os),
"b" ((
long)ns),
"d" (out)
92 return out[0]+out[1]+out[2]+out[3];
96 static int diff_C(
unsigned char *old,
unsigned char *
new,
int os,
int ns)
100 for(y=8; y; y--,
new+=ns, old+=os)
102 d+=abs(
new[x]-old[x]);
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);
125 return (sum+n*max)/2;
144 int w,
int h,
int s,
int zs,
int arg)
148 unsigned char *e, *e2;
150 typedef uint64_t wsum_t;
152 typedef uint32_t wsum_t;
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))
165 t=
be2me_32((uint32_t)(wsum>>32^wsum));
170 for(sum^=(t<<shift|t>>(32-shift)); p<e;)
171 sum^=*p++<<(shift=(shift-8)&31);
178 int w,
int h,
int ds,
int ss,
int threshold)
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;
191 static int copyop(
unsigned char *d,
unsigned char *s,
int bpl,
int h,
int dstride,
int sstride,
int dummy) {
196 static int imgop(
int(*planeop)(
unsigned char *,
unsigned char *,
197 int,
int,
int,
int,
int),
212 dst->
w*(dst->
bpp/8), dst->
h,
225 int phase1,
int phase2,
double *strength)
227 static const int pattern1[]={ -4, 1, 1, 1, 1 },
228 pattern2[]={ -2, -3, 4, 4, -3 }, *pattern;
231 pattern=p->
deghost>0?pattern2:pattern1;
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];
245 for(m=0, n=1; n<5; n++)
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);
263 int n,
m, f, newphase;
265 unsigned int checksum;
279 fprintf(p->
file,
"%08x %d\n",
288 "\n%s: Log file ends prematurely! "
289 "Switching to one pass mode.\n", vf->info->name);
310 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
319 "\n%s: Sync with pass-1 lost! "
320 "Switching to one pass mode.\n", vf->info->name);
329 newphase=p->
bdata[n];
350 if(newphase!=p->
phase && ((p->
phase+4)%5<n)==((newphase+4)%5<n))
354 "\n%s: Telecine phase %d.\n", vf->info->name, p->
phase);
385 int *
buf=0, *bp, bufsize=0, n,
b, f, i, j,
m, s;
386 unsigned int *cbuf=0, *cp;
395 while(fgets(lbuf, 256, p->
file))
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;
406 ff_vf_info_divtc.
name);
412 sscanf(lbuf,
"%x %d", cbuf+n, buf+n);
419 ff_vf_info_divtc.
name);
430 memcpy(buf-15, buf, 15*
sizeof *buf);
431 memset(cbuf-15, 0, 15*
sizeof *cbuf);
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);
445 memset(pbuf, 255, b);
452 double s0=0.0,
s1=0.0;
463 "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
464 ff_vf_info_divtc.
name,
472 for(sum[f]=0, n=-15; n<20; n+=5)
477 m=
match(p, sum, -1, -1, &d);
484 sum[n]=sum[n]-buf[5*(f-3)+n]+buf[5*(f+4)+n];
490 for(f=0; f<b && pbuf[f]==-1; f++);
496 ff_vf_info_divtc.
name);
500 for(n=0; n<f; pbuf[n++]=pbuf[f]);
503 for(f=b-1; pbuf[f]==-1; f--);
504 for(n=f+1; n<
b; pbuf[n++]=pbuf[f]);
509 while(f<b && pbuf[f]!=-1) f++;
511 for(n=f; pbuf[n]==-1; n++);
513 if(pbuf[f-1]==pbuf[n])
516 while(f<n) pbuf[f++]=pbuf[n];
524 for(sum[i]=0, j=5*f-15; j<5*f; j+=5)
529 pbuf[i]=
match(p, sum, pbuf[f-1], pbuf[n], 0);
532 sum[j]=sum[j]-buf[5*(i-3)+j]+buf[5*(i+4)+j];
538 for(i=f, m=f; i<n; i++)
539 if(pbuf[i]==pbuf[f-1]) m++;
547 if(pbuf[j-1]==pbuf[f-1] && pbuf[j]==pbuf[n])
break;
549 if(pbuf[s-1]==pbuf[f-1] && pbuf[s]==pbuf[n])
break;
586 static void uninit(
struct vf_instance *vf)
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);
601 const char *filename=
"framediff.log";
604 if(args && !(args=strdup(args)))
608 "%s: Not enough memory.\n", vf->
info->
name);
630 if((ap=strchr(q,
':'))) *ap++=0;
else ap=q+strlen(q);
631 if((a=strchr(q,
'='))) *a++=0;
else a=q+strlen(q);
636 case 'f': filename=
a;
break;
638 case 'w': p->
window=5*(atoi(a)+4)/5;
break;
639 case 'd': p->
deghost=atoi(a);
break;
641 if(q[1]==
'h') p->
phase=atoi(a);
642 else p->
pass=atoi(a);
648 "pass=1|2 - Use 2-pass mode.\n"
649 "file=filename - Set the 2-pass log file name "
651 "threshold=value - Set the pattern recognition "
652 "sensitivity (default %g).\n"
653 "deghost=value - Select deghosting threshold "
655 "window=numframes - Set the statistics window "
656 "for 1-pass mode (default %d).\n"
657 "phase=0|1|2|3|4 - Set the initial phase "
658 "for 1-pass mode (default %d).\n\n"
659 "The option names can be abbreviated to the shortest "
660 "unique prefix.\n\n",
667 "%s: Unknown argument %s.\n", vf->
info->
name, q);
675 if(!(p->
file=fopen(filename,
"w")))
678 "%s: Can't create file %s.\n", vf->
info->
name, filename);
685 if(!(p->
file=fopen(filename,
"r")))
688 "%s: Can't open file %s.\n", vf->
info->
name, filename);
705 #if HAVE_MMX && HAVE_EBX_AVAILABLE
715 "inverse telecine for deinterlaced video",