10 #include "libavutil/common.h"
13 #include "img_format.h"
17 #include "libvo/fastmemcpy.h"
19 const 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 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
);
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,
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
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
;
213 pattern
=p
->deghost
>0?pattern2
:pattern1
;
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];
226 /* find the best match */
227 for(m
=0, n
=1; n
<5; n
++)
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);
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
;
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
);
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);
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
);
276 checksum
=(unsigned int)imgop((void *)checksum_plane
, mpi
, 0, 0);
278 if(checksum
!=p
->csdata
[p
->frameno
])
281 if(p
->frameno
+f
<p
->fcount
&& p
->csdata
[p
->frameno
+f
]==checksum
)
283 else if(p
->frameno
-f
>=0 && p
->csdata
[p
->frameno
-f
]==checksum
)
291 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
292 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
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
);
309 if(n
>=p
->bcount
) n
=p
->bcount
-1;
311 newphase
=p
->bdata
[n
];
317 int *sump
=p
->sum
+p
->frameno
%5,
318 *histp
=p
->history
+p
->frameno
%p
->window
;
321 *sump
+=(*histp
=imgop(diff_plane
, dmpi
, mpi
, 0));
324 m
=match(p
, p
->sum
, -1, -1, &d
);
332 if(newphase
!=p
->phase
&& ((p
->phase
+4)%5<n
)==((newphase
+4)%5<n
))
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)
342 imgop(copyop
, dmpi
, mpi
, 0);
348 tmpi
=vf_get_image(vf
->next
, mpi
->imgfmt
,
349 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
|
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
;
377 while(fgets(lbuf
, 256, p
->file
))
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
;
387 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Not enough memory.\n",
394 sscanf(lbuf
, "%x %d", cbuf
+n
, buf
+n
);
400 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Empty 2-pass log file.\n",
407 /* generate some dummy data past the beginning and end of the array */
412 memcpy(buf
-15, buf
, 15*sizeof *buf
);
413 memset(cbuf
-15, 0, 15*sizeof *cbuf
);
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
);
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 */
433 int deghost
=-p
->deghost
;
434 double s0
=0.0, s1
=0.0;
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",
447 p
->deghost
?"ON":"OFF",
451 /* analyze the data */
454 for(sum
[f
]=0, n
=-15; n
<20; n
+=5)
459 m
=match(p
, sum
, -1, -1, &d
);
466 sum
[n
]=sum
[n
]-buf
[5*(f
-3)+n
]+buf
[5*(f
+4)+n
];
469 /* fill in the gaps */
472 for(f
=0; f
<b
&& pbuf
[f
]==-1; f
++);
477 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: No telecine pattern found!\n",
482 for(n
=0; n
<f
; pbuf
[n
++]=pbuf
[f
]);
485 for(f
=b
-1; pbuf
[f
]==-1; f
--);
486 for(n
=f
+1; n
<b
; pbuf
[n
++]=pbuf
[f
]);
491 while(f
<b
&& pbuf
[f
]!=-1) f
++;
493 for(n
=f
; pbuf
[n
]==-1; n
++);
495 if(pbuf
[f
-1]==pbuf
[n
])
498 while(f
<n
) pbuf
[f
++]=pbuf
[n
];
502 /* phase change, reanalyze the original data in the gap with zero
503 threshold for only the two phases that appear at the ends */
506 for(sum
[i
]=0, j
=5*f
-15; j
<5*f
; j
+=5)
511 pbuf
[i
]=match(p
, sum
, pbuf
[f
-1], pbuf
[n
], 0);
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
529 if(pbuf
[j
-1]==pbuf
[f
-1] && pbuf
[j
]==pbuf
[n
]) break;
531 if(pbuf
[s
-1]==pbuf
[f
-1] && pbuf
[s
]==pbuf
[n
]) break;
536 /* and rewrite the data to allow only this one transition */
553 static int query_format(struct vf_instance_s
* vf
, unsigned int 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
);
568 static void uninit(struct vf_instance_s
* vf
)
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
);
580 static int open(vf_instance_t
*vf
, char* args
)
583 char *filename
="framediff.log", *ap
, *q
, *a
;
585 if(args
&& !(args
=strdup(args
)))
588 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
589 "%s: Not enough memory.\n", vf
->info
->name
);
596 vf
->put_image
=put_image
;
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
))))
611 if((ap
=strchr(q
, ':'))) *ap
++=0; else ap
=q
+strlen(q
);
612 if((a
=strchr(q
, '='))) *a
++=0; else a
=q
+strlen(q
);
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;
622 if(q
[1]=='h') p
->phase
=atoi(a
);
623 else p
->pass
=atoi(a
);
627 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
629 "pass=1|2 - Use 2-pass mode.\n"
630 "file=filename - Set the 2-pass log file name "
632 "threshold=value - Set the pattern recognition "
633 "sensitivity (default %g).\n"
634 "deghost=value - Select deghosting threshold "
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);
647 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
648 "%s: Unknown argument %s.\n", vf
->info
->name
, q
);
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
);
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
);
681 if(p
->window
<5) p
->window
=5;
682 if(!(p
->history
=calloc(sizeof *p
->history
, p
->window
)))
687 gCpuCaps
.hasMMX
?diff_MMX
:
695 const vf_info_t vf_info_divtc
=
697 "inverse telecine for deinterlaced video",