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
,
51 * diff_MMX and diff_C stolen from vf_decimate.c
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;
129 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
130 int w, int h, int s, int zs, int arg)
132 unsigned int shift, sum;
135 for(sum=0; h; h--, p+=s-w)
136 for(e=p+w, shift=32; p<e;)
137 sum^=(*p++)<<(shift=(shift-8)&31);
143 static unsigned int checksum_plane(unsigned char *p
, unsigned char *z
,
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);
177 static int deghost_plane(unsigned char *d
, unsigned char *s
,
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
) {
192 memcpy_pic(d
, s
, bpl
, h
, dstride
, sstride
);
196 static int imgop(int(*planeop
)(unsigned char *, unsigned char *,
197 int, int, int, int, int),
198 mp_image_t
*dst
, mp_image_t
*src
, int arg
)
200 if(dst
->flags
&MP_IMGFLAG_PLANAR
)
201 return planeop(dst
->planes
[0], src
?src
->planes
[0]:0,
203 dst
->stride
[0], src
?src
->stride
[0]:0, arg
)+
204 planeop(dst
->planes
[1], src
?src
->planes
[1]:0,
205 dst
->chroma_width
, dst
->chroma_height
,
206 dst
->stride
[1], src
?src
->stride
[1]:0, arg
)+
207 planeop(dst
->planes
[2], src
?src
->planes
[2]:0,
208 dst
->chroma_width
, dst
->chroma_height
,
209 dst
->stride
[2], src
?src
->stride
[2]:0, arg
);
211 return planeop(dst
->planes
[0], src
?src
->planes
[0]:0,
212 dst
->w
*(dst
->bpp
/8), dst
->h
,
213 dst
->stride
[0], src
?src
->stride
[0]:0, arg
);
217 * Find the phase in which the telecine pattern fits best to the
218 * given 5 frame slice of frame difference measurements.
220 * If phase1 and phase2 are not negative, only the two specified
224 static int match(struct vf_priv_s
*p
, int *diffs
,
225 int phase1
, int phase2
, double *strength
)
227 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];
244 /* find the best match */
245 for(m
=0, n
=1; n
<5; n
++)
250 /* the second best match */
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);
260 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
262 mp_image_t
*dmpi
, *tmpi
=0;
263 int n
, m
, f
, newphase
;
264 struct vf_priv_s
*p
=vf
->priv
;
265 unsigned int checksum
;
268 dmpi
=vf_get_image(vf
->next
, mpi
->imgfmt
,
269 MP_IMGTYPE_STATIC
, MP_IMGFLAG_ACCEPT_STRIDE
|
270 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
271 mpi
->width
, mpi
->height
);
272 vf_clone_mpi_attributes(dmpi
, mpi
);
279 fprintf(p
->file
, "%08x %d\n",
280 (unsigned int)imgop((void *)checksum_plane
, mpi
, 0, 0),
281 p
->frameno
?imgop(diff_plane
, dmpi
, mpi
, 0):0);
285 if(p
->frameno
/5>p
->bcount
)
287 mp_msg(MSGT_VFILTER
, MSGL_ERR
,
288 "\n%s: Log file ends prematurely! "
289 "Switching to one pass mode.\n", vf
->info
->name
);
294 checksum
=(unsigned int)imgop((void *)checksum_plane
, mpi
, 0, 0);
296 if(checksum
!=p
->csdata
[p
->frameno
])
299 if(p
->frameno
+f
<p
->fcount
&& p
->csdata
[p
->frameno
+f
]==checksum
)
301 else if(p
->frameno
-f
>=0 && p
->csdata
[p
->frameno
-f
]==checksum
)
309 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
310 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
316 else if(p
->misscount
++>=30)
318 mp_msg(MSGT_VFILTER
, MSGL_ERR
,
319 "\n%s: Sync with pass-1 lost! "
320 "Switching to one pass mode.\n", vf
->info
->name
);
327 if(n
>=p
->bcount
) n
=p
->bcount
-1;
329 newphase
=p
->bdata
[n
];
335 int *sump
=p
->sum
+p
->frameno
%5,
336 *histp
=p
->history
+p
->frameno
%p
->window
;
339 *sump
+=(*histp
=imgop(diff_plane
, dmpi
, mpi
, 0));
342 m
=match(p
, p
->sum
, -1, -1, &d
);
350 if(newphase
!=p
->phase
&& ((p
->phase
+4)%5<n
)==((newphase
+4)%5<n
))
353 mp_msg(MSGT_VFILTER
, MSGL_STATUS
,
354 "\n%s: Telecine phase %d.\n", vf
->info
->name
, p
->phase
);
357 switch((p
->frameno
++-p
->phase
+10)%5)
360 imgop(copyop
, dmpi
, mpi
, 0);
366 tmpi
=vf_get_image(vf
->next
, mpi
->imgfmt
,
367 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
|
369 mpi
->width
, mpi
->height
);
370 vf_clone_mpi_attributes(tmpi
, mpi
);
372 imgop(copyop
, tmpi
, mpi
, 0);
373 imgop(deghost_plane
, tmpi
, dmpi
, p
->deghost
);
374 imgop(copyop
, dmpi
, mpi
, 0);
375 return vf_next_put_image(vf
, tmpi
, MP_NOPTS_VALUE
);
379 imgop(copyop
, dmpi
, mpi
, 0);
380 return vf_next_put_image(vf
, dmpi
, MP_NOPTS_VALUE
);
383 static int analyze(struct vf_priv_s
*p
)
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
;
405 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Not enough memory.\n",
412 sscanf(lbuf
, "%x %d", cbuf
+n
, buf
+n
);
418 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: Empty 2-pass log file.\n",
425 /* generate some dummy data past the beginning and end of the array */
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
);
442 /* array with one slot for each slice of 5 frames */
444 p
->bdata
=pbuf
=malloc(p
->bcount
=b
=(n
/5));
445 memset(pbuf
, 255, b
);
447 /* resolve the automatic mode */
451 int deghost
=-p
->deghost
;
452 double s0
=0.0, s1
=0.0;
456 p
->deghost
=0; match(p
, buf
+f
, -1, -1, &d
); s0
+=d
;
457 p
->deghost
=1; match(p
, buf
+f
, -1, -1, &d
); s1
+=d
;
460 p
->deghost
=s1
>s0
?deghost
:0;
462 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
463 "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
465 p
->deghost
?"ON":"OFF",
469 /* analyze the data */
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
];
487 /* fill in the gaps */
490 for(f
=0; f
<b
&& pbuf
[f
]==-1; f
++);
495 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "%s: No telecine pattern found!\n",
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
];
520 /* phase change, reanalyze the original data in the gap with zero
521 threshold for only the two phases that appear at the ends */
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
];
535 /* estimate the transition point by dividing the gap
536 in the same proportion as the number of matches of each kind */
538 for(i
=f
, m
=f
; i
<n
; i
++)
539 if(pbuf
[i
]==pbuf
[f
-1]) m
++;
541 /* find the transition of the right direction nearest to the
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;
554 /* and rewrite the data to allow only this one transition */
571 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
575 case IMGFMT_444P
: case IMGFMT_IYUV
: case IMGFMT_RGB24
:
576 case IMGFMT_422P
: case IMGFMT_UYVY
: case IMGFMT_BGR24
:
577 case IMGFMT_411P
: case IMGFMT_YUY2
: case IMGFMT_IF09
:
578 case IMGFMT_YV12
: case IMGFMT_I420
: case IMGFMT_YVU9
:
579 case IMGFMT_IUYV
: case IMGFMT_Y800
: case IMGFMT_Y8
:
580 return vf_next_query_format(vf
,fmt
);
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
);
598 static int vf_open(vf_instance_t
*vf
, char *args
)
601 char *filename
="framediff.log", *ap
, *q
, *a
;
603 if(args
&& !(args
=strdup(args
)))
606 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
607 "%s: Not enough memory.\n", vf
->info
->name
);
614 vf
->put_image
=put_image
;
616 vf
->query_format
=query_format
;
617 vf
->default_reqs
=VFCAP_ACCEPT_STRIDE
;
618 if(!(vf
->priv
=p
=calloc(1, sizeof(struct vf_priv_s
))))
629 if((ap
=strchr(q
, ':'))) *ap
++=0; else ap
=q
+strlen(q
);
630 if((a
=strchr(q
, '='))) *a
++=0; else a
=q
+strlen(q
);
635 case 'f': filename
=a
; break;
636 case 't': p
->threshold
=atof(a
); break;
637 case 'w': p
->window
=5*(atoi(a
)+4)/5; break;
638 case 'd': p
->deghost
=atoi(a
); break;
640 if(q
[1]=='h') p
->phase
=atoi(a
);
641 else p
->pass
=atoi(a
);
645 mp_msg(MSGT_VFILTER
, MSGL_INFO
,
647 "pass=1|2 - Use 2-pass mode.\n"
648 "file=filename - Set the 2-pass log file name "
650 "threshold=value - Set the pattern recognition "
651 "sensitivity (default %g).\n"
652 "deghost=value - Select deghosting threshold "
654 "window=numframes - Set the statistics window "
655 "for 1-pass mode (default %d).\n"
656 "phase=0|1|2|3|4 - Set the initial phase "
657 "for 1-pass mode (default %d).\n\n"
658 "The option names can be abbreviated to the shortest "
659 "unique prefix.\n\n",
660 vf
->info
->name
, filename
, p
->threshold
, p
->deghost
,
661 p
->window
, p
->phase
%5);
665 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
666 "%s: Unknown argument %s.\n", vf
->info
->name
, q
);
674 if(!(p
->file
=fopen(filename
, "w")))
676 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
677 "%s: Can't create file %s.\n", vf
->info
->name
, filename
);
684 if(!(p
->file
=fopen(filename
, "r")))
686 mp_msg(MSGT_VFILTER
, MSGL_FATAL
,
687 "%s: Can't open file %s.\n", vf
->info
->name
, filename
);
699 if(p
->window
<5) p
->window
=5;
700 if(!(p
->history
=calloc(sizeof *p
->history
, p
->window
)))
704 #if HAVE_MMX && HAVE_EBX_AVAILABLE
705 if(gCpuCaps
.hasMMX
) diff
= diff_MMX
;
712 const vf_info_t vf_info_divtc
=
714 "inverse telecine for deinterlaced video",