2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "cpudetect.h"
28 #include "libavutil/common.h"
31 #include "img_format.h"
35 #include "libvo/fastmemcpy.h"
37 const vf_info_t vf_info_divtc
;
41 int deghost
, pass
, phase
, window
, fcount
, bcount
, frameno
, misscount
,
48 struct vf_detc_pts_buf ptsbuf
;
52 * diff_MMX and diff_C stolen from vf_decimate.c
55 #if HAVE_MMX && HAVE_EBX_AVAILABLE
56 static int diff_MMX(unsigned char *old
, unsigned char *new, int os
, int ns
)
58 volatile short out
[4];
61 "pxor %%mm4, %%mm4 \n\t"
62 "pxor %%mm7, %%mm7 \n\t"
67 "movq (%%"REG_S
"), %%mm0 \n\t"
68 "movq (%%"REG_S
"), %%mm2 \n\t"
69 "add %%"REG_a
", %%"REG_S
" \n\t"
70 "movq (%%"REG_D
"), %%mm1 \n\t"
71 "add %%"REG_b
", %%"REG_D
" \n\t"
72 "psubusb %%mm1, %%mm2 \n\t"
73 "psubusb %%mm0, %%mm1 \n\t"
74 "movq %%mm2, %%mm0 \n\t"
75 "movq %%mm1, %%mm3 \n\t"
76 "punpcklbw %%mm7, %%mm0 \n\t"
77 "punpcklbw %%mm7, %%mm1 \n\t"
78 "punpckhbw %%mm7, %%mm2 \n\t"
79 "punpckhbw %%mm7, %%mm3 \n\t"
80 "paddw %%mm0, %%mm4 \n\t"
81 "paddw %%mm1, %%mm4 \n\t"
82 "paddw %%mm2, %%mm4 \n\t"
83 "paddw %%mm3, %%mm4 \n\t"
87 "movq %%mm4, (%%"REG_d
") \n\t"
90 : "S" (old
), "D" (new), "a" ((long)os
), "b" ((long)ns
), "d" (out
)
93 return out
[0]+out
[1]+out
[2]+out
[3];
97 static int diff_C(unsigned char *old
, unsigned char *new, int os
, int ns
)
101 for(y
=8; y
; y
--, new+=ns
, old
+=os
)
103 d
+=abs(new[x
]-old
[x
]);
108 static int (*diff
)(unsigned char *, unsigned char *, int, int);
110 static int diff_plane(unsigned char *old
, unsigned char *new,
111 int w
, int h
, int os
, int ns
, int arg
)
113 int x
, y
, d
, max
=0, sum
=0, n
=0;
115 for(y
=0; y
<h
-7; y
+=8)
117 for(x
=0; x
<w
-7; x
+=8)
119 d
=diff(old
+x
+y
*os
, new+x
+y
*ns
, os
, ns
);
126 return (sum
+n
*max
)/2;
130 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
131 int w, int h, int s, int zs, int arg)
133 unsigned int shift, sum;
136 for(sum=0; h; h--, p+=s-w)
137 for(e=p+w, shift=32; p<e;)
138 sum^=(*p++)<<(shift=(shift-8)&31);
144 static unsigned int checksum_plane(unsigned char *p
, unsigned char *z
,
145 int w
, int h
, int s
, int zs
, int arg
)
149 unsigned char *e
, *e2
;
151 typedef uint64_t wsum_t
;
153 typedef uint32_t wsum_t
;
157 for(sum
=0; h
; h
--, p
+=s
-w
)
159 for(shift
=0, e
=p
+w
; (size_t)p
&(sizeof(wsum_t
)-1) && p
<e
;)
160 sum
^=*p
++<<(shift
=(shift
-8)&31);
162 for(wsum
=0, e2
=e
-sizeof(wsum_t
)+1; p
<e2
; p
+=sizeof(wsum_t
))
166 t
=be2me_32((uint32_t)(wsum
>>32^wsum
));
171 for(sum
^=(t
<<shift
|t
>>(32-shift
)); p
<e
;)
172 sum
^=*p
++<<(shift
=(shift
-8)&31);
178 static int deghost_plane(unsigned char *d
, unsigned char *s
,
179 int w
, int h
, int ds
, int ss
, int threshold
)
184 for(; h
; h
--, s
+=ss
-w
, d
+=ds
-w
)
185 for(e
=d
+w
; d
<e
; d
++, s
++)
186 if(abs(*d
-*s
)>=threshold
)
187 *d
=(t
=(*d
<<1)-*s
)<0?0:t
>255?255:t
;
192 static int copyop(unsigned char *d
, unsigned char *s
, int bpl
, int h
, int dstride
, int sstride
, int dummy
) {
193 memcpy_pic(d
, s
, bpl
, h
, dstride
, sstride
);
197 static int imgop(int(*planeop
)(unsigned char *, unsigned char *,
198 int, int, int, int, int),
199 mp_image_t
*dst
, mp_image_t
*src
, int arg
)
201 if(dst
->flags
&MP_IMGFLAG_PLANAR
)
202 return planeop(dst
->planes
[0], src
?src
->planes
[0]:0,
204 dst
->stride
[0], src
?src
->stride
[0]:0, arg
)+
205 planeop(dst
->planes
[1], src
?src
->planes
[1]:0,
206 dst
->chroma_width
, dst
->chroma_height
,
207 dst
->stride
[1], src
?src
->stride
[1]:0, arg
)+
208 planeop(dst
->planes
[2], src
?src
->planes
[2]:0,
209 dst
->chroma_width
, dst
->chroma_height
,
210 dst
->stride
[2], src
?src
->stride
[2]:0, arg
);
212 return planeop(dst
->planes
[0], src
?src
->planes
[0]:0,
213 dst
->w
*(dst
->bpp
/8), dst
->h
,
214 dst
->stride
[0], src
?src
->stride
[0]:0, arg
);
218 * Find the phase in which the telecine pattern fits best to the
219 * given 5 frame slice of frame difference measurements.
221 * If phase1 and phase2 are not negative, only the two specified
225 static int match(struct vf_priv_s
*p
, int *diffs
,
226 int phase1
, int phase2
, double *strength
)
228 const int pattern1
[]={ -4, 1, 1, 1, 1 },
229 pattern2
[]={ -2, -3, 4, 4, -3 }, *pattern
;
232 pattern
=p
->deghost
>0?pattern2
:pattern1
;
236 if(phase1
<0 || phase2
<0 || f
==phase1
|| f
==phase2
)
238 for(n
=t
[f
]=0; n
<5; n
++)
239 t
[f
]+=diffs
[n
]*pattern
[(n
-f
+5)%5];
245 /* find the best match */
246 for(m
=0, n
=1; n
<5; n
++)
251 /* the second best match */
252 for(f
=m
?0:1, n
=f
+1; n
<5; n
++)
253 if(n
!=m
&& t
[n
]>t
[f
]) f
=n
;
255 *strength
=(t
[m
]>0?(double)(t
[m
]-t
[f
])/t
[m
]:0.0);
261 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
263 mp_image_t
*dmpi
, *tmpi
=0;
264 int n
, m
, f
, newphase
;
265 struct vf_priv_s
*p
=vf
->priv
;
266 unsigned int checksum
;
269 dmpi
=vf_get_image(vf
->next
, mpi
->imgfmt
,
270 MP_IMGTYPE_STATIC
, MP_IMGFLAG_ACCEPT_STRIDE
|
271 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
272 mpi
->width
, mpi
->height
);
273 vf_clone_mpi_attributes(dmpi
, mpi
);
280 fprintf(p
->file
, "%08x %d\n",
281 (unsigned int)imgop((void *)checksum_plane
, mpi
, 0, 0),
282 p
->frameno
?imgop(diff_plane
, dmpi
, mpi
, 0):0);
286 if(p
->frameno
/5>p
->bcount
)
288 mp_msg(MSGT_VFILTER
, MSGL_ERR
,
289 "\n%s: Log file ends prematurely! "
290 "Switching to one pass mode.\n", vf
->info
->name
);
295 checksum
=(unsigned int)imgop((void *)checksum_plane
, mpi
, 0, 0);
297 if(checksum
!=p
->csdata
[p
->frameno
])
300 if(p
->frameno
+f
<p
->fcount
&& p
->csdata
[p
->frameno
+f
]==checksum
)
302 else if(p
->frameno
-f
>=0 && p
->csdata
[p
->frameno
-f
]==checksum
)
310 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
311 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
317 else if(p
->misscount
++>=30)
319 mp_msg(MSGT_VFILTER
, MSGL_ERR
,
320 "\n%s: Sync with pass-1 lost! "
321 "Switching to one pass mode.\n", vf
->info
->name
);
328 if(n
>=p
->bcount
) n
=p
->bcount
-1;
330 newphase
=p
->bdata
[n
];
336 int *sump
=p
->sum
+p
->frameno
%5,
337 *histp
=p
->history
+p
->frameno
%p
->window
;
340 *sump
+=(*histp
=imgop(diff_plane
, dmpi
, mpi
, 0));
343 m
=match(p
, p
->sum
, -1, -1, &d
);
351 if(newphase
!=p
->phase
&& ((p
->phase
+4)%5<n
)==((newphase
+4)%5<n
))
354 mp_msg(MSGT_VFILTER
, MSGL_STATUS
,
355 "\n%s: Telecine phase %d.\n", vf
->info
->name
, p
->phase
);
358 switch((p
->frameno
++-p
->phase
+10)%5)
361 imgop(copyop
, dmpi
, mpi
, 0);
362 vf_detc_adjust_pts(&p
->ptsbuf
, pts
, 0, 1);
368 tmpi
=vf_get_image(vf
->next
, mpi
->imgfmt
,
369 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
|
371 mpi
->width
, mpi
->height
);
372 vf_clone_mpi_attributes(tmpi
, mpi
);
374 imgop(copyop
, tmpi
, mpi
, 0);
375 imgop(deghost_plane
, tmpi
, dmpi
, p
->deghost
);
376 imgop(copyop
, dmpi
, mpi
, 0);
377 return vf_next_put_image(vf
, tmpi
, vf_detc_adjust_pts(&p
->ptsbuf
, pts
, 0, 0));
381 imgop(copyop
, dmpi
, mpi
, 0);
382 return vf_next_put_image(vf
, dmpi
, vf_detc_adjust_pts(&p
->ptsbuf
, pts
, 0, 0));
385 static int analyze(struct vf_priv_s
*p
)
387 int *buf
=0, *bp
, bufsize
=0, n
, b
, f
, i
, j
, m
, s
;
388 unsigned int *cbuf
=0, *cp
;
397 while(fgets(lbuf
, 256, p
->file
))
401 bufsize
=bufsize
?bufsize
*2:30000;
402 if((bp
=realloc(buf
, bufsize
*sizeof *buf
))) buf
=bp
;
403 if((cp
=realloc(cbuf
, bufsize
*sizeof *cbuf
))) cbuf
=cp
;
407 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Not enough memory.\n",
414 sscanf(lbuf
, "%x %d", cbuf
+n
, buf
+n
);
420 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Empty 2-pass log file.\n",
427 /* generate some dummy data past the beginning and end of the array */
432 memcpy(buf
-15, buf
, 15*sizeof *buf
);
433 memset(cbuf
-15, 0, 15*sizeof *cbuf
);
436 buf
[n
]=buf
[n
-5], cbuf
[n
]=0, n
++;
438 memcpy(buf
+n
, buf
+n
-15, 15*sizeof *buf
);
439 memset(cbuf
+n
, 0, 15*sizeof *cbuf
);
444 /* array with one slot for each slice of 5 frames */
446 p
->bdata
=pbuf
=malloc(p
->bcount
=b
=(n
/5));
447 memset(pbuf
, 255, b
);
449 /* resolve the automatic mode */
453 int deghost
=-p
->deghost
;
454 double s0
=0.0, s1
=0.0;
458 p
->deghost
=0; match(p
, buf
+f
, -1, -1, &d
); s0
+=d
;
459 p
->deghost
=1; match(p
, buf
+f
, -1, -1, &d
); s1
+=d
;
462 p
->deghost
=s1
>s0
?deghost
:0;
464 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
465 "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
467 p
->deghost
?"ON":"OFF",
471 /* analyze the data */
474 for(sum
[f
]=0, n
=-15; n
<20; n
+=5)
479 m
=match(p
, sum
, -1, -1, &d
);
486 sum
[n
]=sum
[n
]-buf
[5*(f
-3)+n
]+buf
[5*(f
+4)+n
];
489 /* fill in the gaps */
492 for(f
=0; f
<b
&& pbuf
[f
]==-1; f
++);
497 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: No telecine pattern found!\n",
502 for(n
=0; n
<f
; pbuf
[n
++]=pbuf
[f
]);
505 for(f
=b
-1; pbuf
[f
]==-1; f
--);
506 for(n
=f
+1; n
<b
; pbuf
[n
++]=pbuf
[f
]);
511 while(f
<b
&& pbuf
[f
]!=-1) f
++;
513 for(n
=f
; pbuf
[n
]==-1; n
++);
515 if(pbuf
[f
-1]==pbuf
[n
])
518 while(f
<n
) pbuf
[f
++]=pbuf
[n
];
522 /* phase change, reanalyze the original data in the gap with zero
523 threshold for only the two phases that appear at the ends */
526 for(sum
[i
]=0, j
=5*f
-15; j
<5*f
; j
+=5)
531 pbuf
[i
]=match(p
, sum
, pbuf
[f
-1], pbuf
[n
], 0);
534 sum
[j
]=sum
[j
]-buf
[5*(i
-3)+j
]+buf
[5*(i
+4)+j
];
537 /* estimate the transition point by dividing the gap
538 in the same proportion as the number of matches of each kind */
540 for(i
=f
, m
=f
; i
<n
; i
++)
541 if(pbuf
[i
]==pbuf
[f
-1]) m
++;
543 /* find the transition of the right direction nearest to the
549 if(pbuf
[j
-1]==pbuf
[f
-1] && pbuf
[j
]==pbuf
[n
]) break;
551 if(pbuf
[s
-1]==pbuf
[f
-1] && pbuf
[s
]==pbuf
[n
]) break;
556 /* and rewrite the data to allow only this one transition */
573 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
577 case IMGFMT_444P
: case IMGFMT_IYUV
: case IMGFMT_RGB24
:
578 case IMGFMT_422P
: case IMGFMT_UYVY
: case IMGFMT_BGR24
:
579 case IMGFMT_411P
: case IMGFMT_YUY2
: case IMGFMT_IF09
:
580 case IMGFMT_YV12
: case IMGFMT_I420
: case IMGFMT_YVU9
:
581 case IMGFMT_IUYV
: case IMGFMT_Y800
: case IMGFMT_Y8
:
582 return vf_next_query_format(vf
,fmt
);
588 static void uninit(struct vf_instance
*vf
)
592 if(vf
->priv
->file
) fclose(vf
->priv
->file
);
593 if(vf
->priv
->csdata
) free(vf
->priv
->csdata
-15);
594 free(vf
->priv
->bdata
);
595 free(vf
->priv
->history
);
600 static int vf_open(vf_instance_t
*vf
, char *args
)
603 char *filename
="framediff.log", *ap
, *q
, *a
;
605 if(args
&& !(args
=strdup(args
)))
608 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
609 "%s: Not enough memory.\n", vf
->info
->name
);
616 vf
->put_image
=put_image
;
618 vf
->query_format
=query_format
;
619 vf
->default_reqs
=VFCAP_ACCEPT_STRIDE
;
620 if(!(vf
->priv
=p
=calloc(1, sizeof(struct vf_priv_s
))))
631 if((ap
=strchr(q
, ':'))) *ap
++=0; else ap
=q
+strlen(q
);
632 if((a
=strchr(q
, '='))) *a
++=0; else a
=q
+strlen(q
);
637 case 'f': filename
=a
; break;
638 case 't': p
->threshold
=atof(a
); break;
639 case 'w': p
->window
=5*(atoi(a
)+4)/5; break;
640 case 'd': p
->deghost
=atoi(a
); break;
642 if(q
[1]=='h') p
->phase
=atoi(a
);
643 else p
->pass
=atoi(a
);
647 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
649 "pass=1|2 - Use 2-pass mode.\n"
650 "file=filename - Set the 2-pass log file name "
652 "threshold=value - Set the pattern recognition "
653 "sensitivity (default %g).\n"
654 "deghost=value - Select deghosting threshold "
656 "window=numframes - Set the statistics window "
657 "for 1-pass mode (default %d).\n"
658 "phase=0|1|2|3|4 - Set the initial phase "
659 "for 1-pass mode (default %d).\n\n"
660 "The option names can be abbreviated to the shortest "
661 "unique prefix.\n\n",
662 vf
->info
->name
, filename
, p
->threshold
, p
->deghost
,
663 p
->window
, p
->phase
%5);
667 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
668 "%s: Unknown argument %s.\n", vf
->info
->name
, q
);
676 if(!(p
->file
=fopen(filename
, "w")))
678 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
679 "%s: Can't create file %s.\n", vf
->info
->name
, filename
);
686 if(!(p
->file
=fopen(filename
, "r")))
688 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
689 "%s: Can't open file %s.\n", vf
->info
->name
, filename
);
701 if(p
->window
<5) p
->window
=5;
702 if(!(p
->history
=calloc(sizeof *p
->history
, p
->window
)))
706 #if HAVE_MMX && HAVE_EBX_AVAILABLE
707 if(gCpuCaps
.hasMMX
) diff
= diff_MMX
;
711 vf_detc_init_pts_buf(&p
->ptsbuf
);
715 const vf_info_t vf_info_divtc
=
717 "inverse telecine for deinterlaced video",