13 #include "img_format.h"
17 #include "libvo/fastmemcpy.h"
19 vf_info_t vf_info_divtc
;
23 int deghost
, pass
, phase
, window
, fcount
, bcount
, frameno
, misscount
,
33 * diff_MMX and diff_C stolen from vf_decimate.c
37 static int diff_MMX(unsigned char *old
, unsigned char *new, int os
, int ns
)
39 volatile short out
[4];
42 "pxor %%mm4, %%mm4 \n\t"
43 "pxor %%mm7, %%mm7 \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"
68 "movq %%mm4, (%%"REG_d
") \n\t"
71 : "S" (old
), "D" (new), "a" ((long)os
), "b" ((long)ns
), "d" (out
)
74 return out
[0]+out
[1]+out
[2]+out
[3];
78 static int diff_C(unsigned char *old
, unsigned char *new, int os
, int ns
)
82 for(y
=8; y
; y
--, new+=ns
, old
+=os
)
84 d
+=abs(new[x
]-old
[x
]);
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;
100 d
=diff(old
+x
+y
*os
, new+x
+y
*ns
, os
, ns
);
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;
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);
125 static unsigned int checksum_plane(unsigned char *p
, unsigned char *z
,
126 int w
, int h
, int s
, int zs
, int arg
)
130 unsigned char *e
, *e2
;
132 typedef uint64_t wsum_t
;
134 typedef uint32_t wsum_t
;
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
))
147 t
=be2me_32((uint32_t)(wsum
>>32^wsum
));
152 for(sum
^=(t
<<shift
|t
>>(32-shift
)); p
<e
;)
153 sum
^=*p
++<<(shift
=(shift
-8)&31);
159 static int deghost_plane(unsigned char *d
, unsigned char *s
,
160 int w
, int h
, int ds
, int ss
, int threshold
)
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
;
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,
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
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
;
210 pattern
=p
->deghost
>0?pattern2
:pattern1
;
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];
223 /* find the best match */
224 for(m
=0, n
=1; n
<5; n
++)
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);
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
;
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
);
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);
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
);
273 checksum
=(unsigned int)imgop((void *)checksum_plane
, mpi
, 0, 0);
275 if(checksum
!=p
->csdata
[p
->frameno
])
278 if(p
->frameno
+f
<p
->fcount
&& p
->csdata
[p
->frameno
+f
]==checksum
)
280 else if(p
->frameno
-f
>=0 && p
->csdata
[p
->frameno
-f
]==checksum
)
288 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
289 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
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
);
306 if(n
>=p
->bcount
) n
=p
->bcount
-1;
308 newphase
=p
->bdata
[n
];
314 int *sump
=p
->sum
+p
->frameno
%5,
315 *histp
=p
->history
+p
->frameno
%p
->window
;
318 *sump
+=(*histp
=imgop(diff_plane
, dmpi
, mpi
, 0));
321 m
=match(p
, p
->sum
, -1, -1, &d
);
329 if(newphase
!=p
->phase
&& ((p
->phase
+4)%5<n
)==((newphase
+4)%5<n
))
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)
339 imgop((void *)memcpy_pic
, dmpi
, mpi
, 0);
345 tmpi
=vf_get_image(vf
->next
, mpi
->imgfmt
,
346 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
|
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
;
374 while(fgets(lbuf
, 256, p
->file
))
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
;
384 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Not enough memory.\n",
391 sscanf(lbuf
, "%x %d", cbuf
+n
, buf
+n
);
397 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Empty 2-pass log file.\n",
404 /* generate some dummy data past the beginning and end of the array */
409 memcpy(buf
-15, buf
, 15*sizeof *buf
);
410 memset(cbuf
-15, 0, 15*sizeof *cbuf
);
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
);
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 */
430 int deghost
=-p
->deghost
;
431 double s0
=0.0, s1
=0.0;
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",
444 p
->deghost
?"ON":"OFF",
448 /* analyze the data */
451 for(sum
[f
]=0, n
=-15; n
<20; n
+=5)
456 m
=match(p
, sum
, -1, -1, &d
);
463 sum
[n
]=sum
[n
]-buf
[5*(f
-3)+n
]+buf
[5*(f
+4)+n
];
466 /* fill in the gaps */
469 for(f
=0; f
<b
&& pbuf
[f
]==-1; f
++);
474 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: No telecine pattern found!\n",
479 for(n
=0; n
<f
; pbuf
[n
++]=pbuf
[f
]);
482 for(f
=b
-1; pbuf
[f
]==-1; f
--);
483 for(n
=f
+1; n
<b
; pbuf
[n
++]=pbuf
[f
]);
488 while(f
<b
&& pbuf
[f
]!=-1) f
++;
490 for(n
=f
; pbuf
[n
]==-1; n
++);
492 if(pbuf
[f
-1]==pbuf
[n
])
495 while(f
<n
) pbuf
[f
++]=pbuf
[n
];
499 /* phase change, reanalyze the original data in the gap with zero
500 threshold for only the two phases that appear at the ends */
503 for(sum
[i
]=0, j
=5*f
-15; j
<5*f
; j
+=5)
508 pbuf
[i
]=match(p
, sum
, pbuf
[f
-1], pbuf
[n
], 0);
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
526 if(pbuf
[j
-1]==pbuf
[f
-1] && pbuf
[j
]==pbuf
[n
]) break;
528 if(pbuf
[s
-1]==pbuf
[f
-1] && pbuf
[s
]==pbuf
[n
]) break;
533 /* and rewrite the data to allow only this one transition */
550 static int query_format(struct vf_instance_s
* vf
, unsigned int 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
);
565 static void uninit(struct vf_instance_s
* vf
)
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
);
577 static int open(vf_instance_t
*vf
, char* args
)
580 char *filename
="framediff.log", *ap
, *q
, *a
;
582 if(args
&& !(args
=strdup(args
)))
585 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
586 "%s: Not enough memory.\n", vf
->info
->name
);
593 vf
->put_image
=put_image
;
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
))))
608 if((ap
=strchr(q
, ':'))) *ap
++=0; else ap
=q
+strlen(q
);
609 if((a
=strchr(q
, '='))) *a
++=0; else a
=q
+strlen(q
);
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;
619 if(q
[1]=='h') p
->phase
=atoi(a
);
620 else p
->pass
=atoi(a
);
624 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
626 "pass=1|2 - Use 2-pass mode.\n"
627 "file=filename - Set the 2-pass log file name "
629 "threshold=value - Set the pattern recognition "
630 "sensitivity (default %g).\n"
631 "deghost=value - Select deghosting threshold "
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);
644 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
645 "%s: Unknown argument %s.\n", vf
->info
->name
, q
);
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
);
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
);
678 if(p
->window
<5) p
->window
=5;
679 if(!(p
->history
=calloc(sizeof *p
->history
, p
->window
)))
684 gCpuCaps
.hasMMX
?diff_MMX
:
692 vf_info_t vf_info_divtc
=
694 "inverse telecine for deinterlaced video",