9 #include "../cpudetect.h"
12 #include "img_format.h"
16 #include "../libvo/fastmemcpy.h"
18 vf_info_t vf_info_divtc
;
22 int deghost
, pass
, phase
, window
, fcount
, bcount
, frameno
, misscount
,
32 * diff_MMX and diff_C stolen from vf_decimate.c
36 static int diff_MMX(unsigned char *old
, unsigned char *new, int os
, int ns
)
38 volatile short out
[4];
41 "pxor %%mm4, %%mm4 \n\t"
42 "pxor %%mm7, %%mm7 \n\t"
47 "movq (%%"REG_S
"), %%mm0 \n\t"
48 "movq (%%"REG_S
"), %%mm2 \n\t"
49 "add %%"REG_a
", %%"REG_S
" \n\t"
50 "movq (%%"REG_D
"), %%mm1 \n\t"
51 "add %%"REG_b
", %%"REG_D
" \n\t"
52 "psubusb %%mm1, %%mm2 \n\t"
53 "psubusb %%mm0, %%mm1 \n\t"
54 "movq %%mm2, %%mm0 \n\t"
55 "movq %%mm1, %%mm3 \n\t"
56 "punpcklbw %%mm7, %%mm0 \n\t"
57 "punpcklbw %%mm7, %%mm1 \n\t"
58 "punpckhbw %%mm7, %%mm2 \n\t"
59 "punpckhbw %%mm7, %%mm3 \n\t"
60 "paddw %%mm0, %%mm4 \n\t"
61 "paddw %%mm1, %%mm4 \n\t"
62 "paddw %%mm2, %%mm4 \n\t"
63 "paddw %%mm3, %%mm4 \n\t"
67 "movq %%mm4, (%%"REG_d
") \n\t"
70 : "S" (old
), "D" (new), "a" ((long)os
), "b" ((long)ns
), "d" (out
)
73 return out
[0]+out
[1]+out
[2]+out
[3];
77 static int diff_C(unsigned char *old
, unsigned char *new, int os
, int ns
)
81 for(y
=8; y
; y
--, new+=ns
, old
+=os
)
83 d
+=abs(new[x
]-old
[x
]);
88 static int (*diff
)(unsigned char *, unsigned char *, int, int);
90 static int diff_plane(unsigned char *old
, unsigned char *new,
91 int w
, int h
, int os
, int ns
, int arg
)
93 int x
, y
, d
, max
=0, sum
=0, n
=0;
99 d
=diff(old
+x
+y
*os
, new+x
+y
*ns
, os
, ns
);
106 return (sum
+n
*max
)/2;
110 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
111 int w, int h, int s, int zs, int arg)
113 unsigned int shift, sum;
116 for(sum=0; h; h--, p+=s-w)
117 for(e=p+w, shift=32; p<e;)
118 sum^=(*p++)<<(shift=(shift-8)&31);
124 static unsigned int checksum_plane(unsigned char *p
, unsigned char *z
,
125 int w
, int h
, int s
, int zs
, int arg
)
129 unsigned char *e
, *e2
;
131 typedef uint64_t wsum_t
;
133 typedef uint32_t wsum_t
;
137 for(sum
=0; h
; h
--, p
+=s
-w
)
139 for(shift
=0, e
=p
+w
; (int)p
&(sizeof(wsum_t
)-1) && p
<e
;)
140 sum
^=*p
++<<(shift
=(shift
-8)&31);
142 for(wsum
=0, e2
=e
-sizeof(wsum_t
)+1; p
<e2
; p
+=sizeof(wsum_t
))
146 t
=be2me_32((uint32_t)(wsum
>>32^wsum
));
151 for(sum
^=(t
<<shift
|t
>>(32-shift
)); p
<e
;)
152 sum
^=*p
++<<(shift
=(shift
-8)&31);
158 static int deghost_plane(unsigned char *d
, unsigned char *s
,
159 int w
, int h
, int ds
, int ss
, int threshold
)
164 for(; h
; h
--, s
+=ss
-w
, d
+=ds
-w
)
165 for(e
=d
+w
; d
<e
; d
++, s
++)
166 if(abs(*d
-*s
)>=threshold
)
167 *d
=(t
=(*d
<<1)-*s
)<0?0:t
>255?255:t
;
172 static int imgop(int(*planeop
)(unsigned char *, unsigned char *,
173 int, int, int, int, int),
174 mp_image_t
*dst
, mp_image_t
*src
, int arg
)
176 if(dst
->flags
&MP_IMGFLAG_PLANAR
)
177 return planeop(dst
->planes
[0], src
?src
->planes
[0]:0,
179 dst
->stride
[0], src
?src
->stride
[0]:0, arg
)+
180 planeop(dst
->planes
[1], src
?src
->planes
[1]:0,
181 dst
->chroma_width
, dst
->chroma_height
,
182 dst
->stride
[1], src
?src
->stride
[1]:0, arg
)+
183 planeop(dst
->planes
[2], src
?src
->planes
[2]:0,
184 dst
->chroma_width
, dst
->chroma_height
,
185 dst
->stride
[2], src
?src
->stride
[2]:0, arg
);
187 return planeop(dst
->planes
[0], src
?src
->planes
[0]:0,
188 dst
->w
*(dst
->bpp
/8), dst
->h
,
189 dst
->stride
[0], src
?src
->stride
[0]:0, arg
);
193 * Find the phase in which the telecine pattern fits best to the
194 * given 5 frame slice of frame difference measurements.
196 * If phase1 and phase2 are not negative, only the two specified
200 static int cmp(int *a
, int *b
) { return *b
-*a
; }
202 static int match(struct vf_priv_s
*p
, int *diffs
,
203 int phase1
, int phase2
, double *strength
)
205 static const int pattern1
[]={ -4, 1, 1, 1, 1 },
206 pattern2
[]={ -2, -3, 4, 4, -3 }, *pattern
;
209 pattern
=p
->deghost
>0?pattern2
:pattern1
;
213 if(phase1
<0 || phase2
<0 || f
==phase1
|| f
==phase2
)
215 for(n
=t
[f
]=0; n
<5; n
++)
216 t
[f
]+=diffs
[n
]*pattern
[(n
-f
+5)%5];
222 /* find the best match */
223 for(m
=0, n
=1; n
<5; n
++)
228 /* the second best match */
229 for(f
=m
?0:1, n
=f
+1; n
<5; n
++)
230 if(n
!=m
&& t
[n
]>t
[f
]) f
=n
;
232 *strength
=(t
[m
]>0?(double)(t
[m
]-t
[f
])/t
[m
]:0.0);
238 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
)
240 mp_image_t
*dmpi
, *tmpi
=0;
241 int n
, m
, f
, newphase
;
242 struct vf_priv_s
*p
=vf
->priv
;
243 unsigned int checksum
;
246 dmpi
=vf_get_image(vf
->next
, mpi
->imgfmt
,
247 MP_IMGTYPE_STATIC
, MP_IMGFLAG_ACCEPT_STRIDE
|
248 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
249 mpi
->width
, mpi
->height
);
250 vf_clone_mpi_attributes(dmpi
, mpi
);
257 fprintf(p
->file
, "%08x %d\n",
258 (unsigned int)imgop((void *)checksum_plane
, mpi
, 0, 0),
259 p
->frameno
?imgop(diff_plane
, dmpi
, mpi
, 0):0);
263 if(p
->frameno
/5>p
->bcount
)
265 mp_msg(MSGT_VFILTER
, MSGL_ERR
,
266 "\n%s: Log file ends prematurely! "
267 "Switching to one pass mode.\n", vf
->info
->name
);
272 checksum
=(unsigned int)imgop((void *)checksum_plane
, mpi
, 0, 0);
274 if(checksum
!=p
->csdata
[p
->frameno
])
277 if(p
->frameno
+f
<p
->fcount
&& p
->csdata
[p
->frameno
+f
]==checksum
)
279 else if(p
->frameno
-f
>=0 && p
->csdata
[p
->frameno
-f
]==checksum
)
287 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
288 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
294 else if(p
->misscount
++>=30)
296 mp_msg(MSGT_VFILTER
, MSGL_ERR
,
297 "\n%s: Sync with pass-1 lost! "
298 "Switching to one pass mode.\n", vf
->info
->name
);
305 if(n
>=p
->bcount
) n
=p
->bcount
-1;
307 newphase
=p
->bdata
[n
];
313 int *sump
=p
->sum
+p
->frameno
%5,
314 *histp
=p
->history
+p
->frameno
%p
->window
;
317 *sump
+=(*histp
=imgop(diff_plane
, dmpi
, mpi
, 0));
320 m
=match(p
, p
->sum
, -1, -1, &d
);
328 if(newphase
!=p
->phase
&& ((p
->phase
+4)%5<n
)==((newphase
+4)%5<n
))
331 mp_msg(MSGT_VFILTER
, MSGL_STATUS
,
332 "\n%s: Telecine phase %d.\n", vf
->info
->name
, p
->phase
);
335 switch((p
->frameno
++-p
->phase
+10)%5)
338 imgop((void *)memcpy_pic
, dmpi
, mpi
, 0);
344 tmpi
=vf_get_image(vf
->next
, mpi
->imgfmt
,
345 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
346 mpi
->width
, mpi
->height
);
347 vf_clone_mpi_attributes(tmpi
, mpi
);
349 imgop((void *)memcpy_pic
, tmpi
, mpi
, 0);
350 imgop(deghost_plane
, tmpi
, dmpi
, p
->deghost
);
351 imgop((void *)memcpy_pic
, dmpi
, mpi
, 0);
352 return vf_next_put_image(vf
, tmpi
);
356 imgop((void *)memcpy_pic
, dmpi
, mpi
, 0);
357 return vf_next_put_image(vf
, dmpi
);
360 static int analyze(struct vf_priv_s
*p
)
362 int *buf
=0, *bp
, bufsize
=0, n
, b
, f
, i
, j
, m
, s
;
363 unsigned int *cbuf
=0, *cp
;
372 while(fgets(lbuf
, 256, p
->file
))
376 bufsize
=bufsize
?bufsize
*2:30000;
377 if((bp
=realloc(buf
, bufsize
*sizeof *buf
))) buf
=bp
;
378 if((cp
=realloc(cbuf
, bufsize
*sizeof *cbuf
))) cbuf
=cp
;
382 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Not enough memory.\n",
389 sscanf(lbuf
, "%x %d", cbuf
+n
, buf
+n
);
395 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Empty 2-pass log file.\n",
402 /* generate some dummy data past the beginning and end of the array */
407 memcpy(buf
-15, buf
, 15*sizeof *buf
);
408 memset(cbuf
-15, 0, 15*sizeof *cbuf
);
411 buf
[n
]=buf
[n
-5], cbuf
[n
]=0, n
++;
413 memcpy(buf
+n
, buf
+n
-15, 15*sizeof *buf
);
414 memset(cbuf
+n
, 0, 15*sizeof *cbuf
);
419 /* array with one slot for each slice of 5 frames */
421 p
->bdata
=pbuf
=malloc(p
->bcount
=b
=(n
/5));
422 memset(pbuf
, 255, b
);
424 /* resolve the automatic mode */
428 int deghost
=-p
->deghost
;
429 double s0
=0.0, s1
=0.0;
433 p
->deghost
=0; match(p
, buf
+f
, -1, -1, &d
); s0
+=d
;
434 p
->deghost
=1; match(p
, buf
+f
, -1, -1, &d
); s1
+=d
;
437 p
->deghost
=s1
>s0
?deghost
:0;
439 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
440 "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
442 p
->deghost
?"ON":"OFF",
446 /* analyze the data */
449 for(sum
[f
]=0, n
=-15; n
<20; n
+=5)
454 m
=match(p
, sum
, -1, -1, &d
);
461 sum
[n
]=sum
[n
]-buf
[5*(f
-3)+n
]+buf
[5*(f
+4)+n
];
464 /* fill in the gaps */
467 for(f
=0; f
<b
&& pbuf
[f
]==-1; f
++);
472 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: No telecine pattern found!\n",
477 for(n
=0; n
<f
; pbuf
[n
++]=pbuf
[f
]);
480 for(f
=b
-1; pbuf
[f
]==-1; f
--);
481 for(n
=f
+1; n
<b
; pbuf
[n
++]=pbuf
[f
]);
486 while(f
<b
&& pbuf
[f
]!=-1) f
++;
488 for(n
=f
; pbuf
[n
]==-1; n
++);
490 if(pbuf
[f
-1]==pbuf
[n
])
493 while(f
<n
) pbuf
[f
++]=pbuf
[n
];
497 /* phase change, reanalyze the original data in the gap with zero
498 threshold for only the two phases that appear at the ends */
501 for(sum
[i
]=0, j
=5*f
-15; j
<5*f
; j
+=5)
506 pbuf
[i
]=match(p
, sum
, pbuf
[f
-1], pbuf
[n
], 0);
509 sum
[j
]=sum
[j
]-buf
[5*(i
-3)+j
]+buf
[5*(i
+4)+j
];
512 /* estimate the transition point by dividing the gap
513 in the same proportion as the number of matches of each kind */
515 for(i
=f
, m
=f
; i
<n
; i
++)
516 if(pbuf
[i
]==pbuf
[f
-1]) m
++;
518 /* find the transition of the right direction nearest to the
524 if(pbuf
[j
-1]==pbuf
[f
-1] && pbuf
[j
]==pbuf
[n
]) break;
526 if(pbuf
[s
-1]==pbuf
[f
-1] && pbuf
[s
]==pbuf
[n
]) break;
531 /* and rewrite the data to allow only this one transition */
548 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
)
552 case IMGFMT_444P
: case IMGFMT_IYUV
: case IMGFMT_RGB24
:
553 case IMGFMT_422P
: case IMGFMT_UYVY
: case IMGFMT_BGR24
:
554 case IMGFMT_411P
: case IMGFMT_YUY2
: case IMGFMT_IF09
:
555 case IMGFMT_YV12
: case IMGFMT_I420
: case IMGFMT_YVU9
:
556 case IMGFMT_IUYV
: case IMGFMT_Y800
: case IMGFMT_Y8
:
557 return vf_next_query_format(vf
,fmt
);
563 static void uninit(struct vf_instance_s
* vf
)
567 if(vf
->priv
->file
) fclose(vf
->priv
->file
);
568 if(vf
->priv
->csdata
) free(vf
->priv
->csdata
-15);
569 free(vf
->priv
->bdata
);
570 free(vf
->priv
->history
);
575 static int open(vf_instance_t
*vf
, char* args
)
578 char *filename
="framediff.log", *ap
, *q
, *a
;
580 if(args
&& !(args
=strdup(args
)))
583 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
584 "%s: Not enough memory.\n", vf
->info
->name
);
591 vf
->put_image
=put_image
;
593 vf
->query_format
=query_format
;
594 vf
->default_reqs
=VFCAP_ACCEPT_STRIDE
;
595 if(!(vf
->priv
=p
=calloc(1, sizeof(struct vf_priv_s
))))
606 if((ap
=strchr(q
, ':'))) *ap
++=0; else ap
=q
+strlen(q
);
607 if((a
=strchr(q
, '='))) *a
++=0; else a
=q
+strlen(q
);
612 case 'f': filename
=a
; break;
613 case 't': p
->threshold
=atof(a
); break;
614 case 'w': p
->window
=5*(atoi(a
)+4)/5; break;
615 case 'd': p
->deghost
=atoi(a
); break;
617 if(q
[1]=='h') p
->phase
=atoi(a
);
618 else p
->pass
=atoi(a
);
622 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
624 "pass=1|2 - Use 2-pass mode.\n"
625 "file=filename - Set the 2-pass log file name "
627 "threshold=value - Set the pattern recognition "
628 "sensitivity (default %g).\n"
629 "deghost=value - Select deghosting threshold "
631 "window=numframes - Set the statistics window "
632 "for 1-pass mode (default %d).\n"
633 "phase=0|1|2|3|4 - Set the initial phase "
634 "for 1-pass mode (default %d).\n\n"
635 "The option names can be abbreviated to the shortest "
636 "unique prefix.\n\n",
637 vf
->info
->name
, filename
, p
->threshold
, p
->deghost
,
638 p
->window
, p
->phase
%5);
642 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
643 "%s: Unknown argument %s.\n", vf
->info
->name
, q
);
651 if(!(p
->file
=fopen(filename
, "w")))
653 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
654 "%s: Can't create file %s.\n", vf
->info
->name
, filename
);
661 if(!(p
->file
=fopen(filename
, "r")))
663 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
664 "%s: Can't open file %s.\n", vf
->info
->name
, filename
);
676 if(p
->window
<5) p
->window
=5;
677 if(!(p
->history
=calloc(sizeof *p
->history
, p
->window
)))
682 gCpuCaps
.hasMMX
?diff_MMX
:
690 vf_info_t vf_info_divtc
=
692 "inverse telecine for deinterlaced video",