gl_common: minor cleanup/refactor
[mplayer.git] / libmpcodecs / vf_divtc.c
blobe04e7c0b4ed1fab78d7fc53213b41fbc4a21b2b7
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <math.h>
25 #include "config.h"
26 #include "mp_msg.h"
27 #include "cpudetect.h"
28 #include "libavutil/common.h"
29 #include "mpbswap.h"
31 #include "img_format.h"
32 #include "mp_image.h"
33 #include "vf.h"
35 #include "libvo/fastmemcpy.h"
37 const vf_info_t vf_info_divtc;
39 struct vf_priv_s
41 int deghost, pass, phase, window, fcount, bcount, frameno, misscount,
42 ocount, sum[5];
43 double threshold;
44 FILE *file;
45 int8_t *bdata;
46 unsigned int *csdata;
47 int *history;
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];
59 __asm__ (
60 "movl $8, %%ecx \n\t"
61 "pxor %%mm4, %%mm4 \n\t"
62 "pxor %%mm7, %%mm7 \n\t"
64 ASMALIGN(4)
65 "1: \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"
85 "decl %%ecx \n\t"
86 "jnz 1b \n\t"
87 "movq %%mm4, (%%"REG_d") \n\t"
88 "emms \n\t"
90 : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
91 : "%ecx", "memory"
93 return out[0]+out[1]+out[2]+out[3];
95 #endif
97 static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
99 int x, y, d=0;
101 for(y=8; y; y--, new+=ns, old+=os)
102 for(x=8; x; x--)
103 d+=abs(new[x]-old[x]);
105 return d;
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);
120 if(d>max) max=d;
121 sum+=d;
122 n++;
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;
134 unsigned char *e;
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);
140 return sum;
144 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
145 int w, int h, int s, int zs, int arg)
147 unsigned int shift;
148 uint32_t sum, t;
149 unsigned char *e, *e2;
150 #if HAVE_FAST_64BIT
151 typedef uint64_t wsum_t;
152 #else
153 typedef uint32_t wsum_t;
154 #endif
155 wsum_t wsum;
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))
163 wsum^=*(wsum_t *)p;
165 #if HAVE_FAST_64BIT
166 t=be2me_32((uint32_t)(wsum>>32^wsum));
167 #else
168 t=be2me_32(wsum);
169 #endif
171 for(sum^=(t<<shift|t>>(32-shift)); p<e;)
172 sum^=*p++<<(shift=(shift-8)&31);
175 return sum;
178 static int deghost_plane(unsigned char *d, unsigned char *s,
179 int w, int h, int ds, int ss, int threshold)
181 int t;
182 unsigned char *e;
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;
189 return 0;
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);
194 return 0;
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,
203 dst->w, dst->h,
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
222 * phases are tested.
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;
230 int f, m, n, t[5];
232 pattern=p->deghost>0?pattern2:pattern1;
234 for(f=0; f<5; f++)
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];
241 else
242 t[f]=INT_MIN;
245 /* find the best match */
246 for(m=0, n=1; n<5; n++)
247 if(t[n]>t[m]) m=n;
249 if(strength)
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);
258 return m;
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;
267 double d;
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);
275 newphase=p->phase;
277 switch(p->pass)
279 case 1:
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);
283 break;
285 case 2:
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);
291 p->pass=0;
292 break;
295 checksum=(unsigned int)imgop((void *)checksum_plane, mpi, 0, 0);
297 if(checksum!=p->csdata[p->frameno])
299 for(f=0; f<100; f++)
300 if(p->frameno+f<p->fcount && p->csdata[p->frameno+f]==checksum)
301 break;
302 else if(p->frameno-f>=0 && p->csdata[p->frameno-f]==checksum)
304 f=-f;
305 break;
308 if(f<100)
310 mp_msg(MSGT_VFILTER, MSGL_INFO,
311 "\n%s: Mismatch with pass-1: %+d frame(s).\n",
312 vf->info->name, f);
314 p->frameno+=f;
315 p->misscount=0;
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);
322 p->pass=0;
323 break;
327 n=(p->frameno)/5;
328 if(n>=p->bcount) n=p->bcount-1;
330 newphase=p->bdata[n];
331 break;
333 default:
334 if(p->frameno)
336 int *sump=p->sum+p->frameno%5,
337 *histp=p->history+p->frameno%p->window;
339 *sump-=*histp;
340 *sump+=(*histp=imgop(diff_plane, dmpi, mpi, 0));
343 m=match(p, p->sum, -1, -1, &d);
345 if(d>=p->threshold)
346 newphase=m;
349 n=p->ocount++%5;
351 if(newphase!=p->phase && ((p->phase+4)%5<n)==((newphase+4)%5<n))
353 p->phase=newphase;
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)
360 case 0:
361 imgop(copyop, dmpi, mpi, 0);
362 vf_detc_adjust_pts(&p->ptsbuf, pts, 0, 1);
363 return 0;
365 case 4:
366 if(p->deghost>0)
368 tmpi=vf_get_image(vf->next, mpi->imgfmt,
369 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
370 MP_IMGFLAG_READABLE,
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;
389 int8_t *pbuf;
390 int8_t lbuf[256];
391 int sum[5];
392 double d;
394 /* read the file */
396 n=15;
397 while(fgets(lbuf, 256, p->file))
399 if(n>=bufsize-19)
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;
405 if(!bp || !cp)
407 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Not enough memory.\n",
408 vf_info_divtc.name);
409 free(buf);
410 free(cbuf);
411 return 0;
414 sscanf(lbuf, "%x %d", cbuf+n, buf+n);
415 n++;
418 if(!n)
420 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Empty 2-pass log file.\n",
421 vf_info_divtc.name);
422 free(buf);
423 free(cbuf);
424 return 0;
427 /* generate some dummy data past the beginning and end of the array */
429 buf+=15, cbuf+=15;
430 n-=15;
432 memcpy(buf-15, buf, 15*sizeof *buf);
433 memset(cbuf-15, 0, 15*sizeof *cbuf);
435 while(n%5)
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);
441 p->csdata=cbuf;
442 p->fcount=n;
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 */
451 if(p->deghost<0)
453 int deghost=-p->deghost;
454 double s0=0.0, s1=0.0;
456 for(f=0; f<n; f+=5)
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",
466 vf_info_divtc.name,
467 p->deghost?"ON":"OFF",
468 10.0*log10(s1/s0));
471 /* analyze the data */
473 for(f=0; f<5; f++)
474 for(sum[f]=0, n=-15; n<20; n+=5)
475 sum[f]+=buf[n+f];
477 for(f=0; f<b; f++)
479 m=match(p, sum, -1, -1, &d);
481 if(d>=p->threshold)
482 pbuf[f]=m;
484 if(f<b-1)
485 for(n=0; n<5; n++)
486 sum[n]=sum[n]-buf[5*(f-3)+n]+buf[5*(f+4)+n];
489 /* fill in the gaps */
491 /* the beginning */
492 for(f=0; f<b && pbuf[f]==-1; f++);
494 if(f==b)
496 free(buf-15);
497 mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: No telecine pattern found!\n",
498 vf_info_divtc.name);
499 return 0;
502 for(n=0; n<f; pbuf[n++]=pbuf[f]);
504 /* the end */
505 for(f=b-1; pbuf[f]==-1; f--);
506 for(n=f+1; n<b; pbuf[n++]=pbuf[f]);
508 /* the rest */
509 for(f=0;;)
511 while(f<b && pbuf[f]!=-1) f++;
512 if(f==b) break;
513 for(n=f; pbuf[n]==-1; n++);
515 if(pbuf[f-1]==pbuf[n])
517 /* just a gap */
518 while(f<n) pbuf[f++]=pbuf[n];
520 else
522 /* phase change, reanalyze the original data in the gap with zero
523 threshold for only the two phases that appear at the ends */
525 for(i=0; i<5; i++)
526 for(sum[i]=0, j=5*f-15; j<5*f; j+=5)
527 sum[i]+=buf[i+j];
529 for(i=f; i<n; i++)
531 pbuf[i]=match(p, sum, pbuf[f-1], pbuf[n], 0);
533 for(j=0; j<5; j++)
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
544 estimated point */
546 if(m>f && m<n)
548 for(j=m; j>f; j--)
549 if(pbuf[j-1]==pbuf[f-1] && pbuf[j]==pbuf[n]) break;
550 for(s=m; s<n; s++)
551 if(pbuf[s-1]==pbuf[f-1] && pbuf[s]==pbuf[n]) break;
553 m=(s-m<m-j)?s:j;
556 /* and rewrite the data to allow only this one transition */
558 for(i=f; i<m; i++)
559 pbuf[i]=pbuf[f-1];
561 for(; i<n; i++)
562 pbuf[i]=pbuf[n];
564 f=n;
568 free(buf-15);
570 return 1;
573 static int query_format(struct vf_instance *vf, unsigned int fmt)
575 switch(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);
585 return 0;
588 static void uninit(struct vf_instance *vf)
590 if(vf->priv)
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);
596 free(vf->priv);
600 static int vf_open(vf_instance_t *vf, char *args)
602 struct vf_priv_s *p;
603 char *filename="framediff.log", *ap, *q, *a;
605 if(args && !(args=strdup(args)))
607 nomem:
608 mp_msg(MSGT_VFILTER, MSGL_FATAL,
609 "%s: Not enough memory.\n", vf->info->name);
610 fail:
611 uninit(vf);
612 free(args);
613 return 0;
616 vf->put_image=put_image;
617 vf->uninit=uninit;
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))))
621 goto nomem;
623 p->phase=5;
624 p->threshold=0.5;
625 p->window=30;
627 if((ap=args))
628 while(*ap)
630 q=ap;
631 if((ap=strchr(q, ':'))) *ap++=0; else ap=q+strlen(q);
632 if((a=strchr(q, '='))) *a++=0; else a=q+strlen(q);
634 switch(*q)
636 case 0: break;
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;
641 case 'p':
642 if(q[1]=='h') p->phase=atoi(a);
643 else p->pass=atoi(a);
644 break;
646 case 'h':
647 mp_msg(MSGT_VFILTER, MSGL_INFO,
648 "\n%s options:\n\n"
649 "pass=1|2 - Use 2-pass mode.\n"
650 "file=filename - Set the 2-pass log file name "
651 "(default %s).\n"
652 "threshold=value - Set the pattern recognition "
653 "sensitivity (default %g).\n"
654 "deghost=value - Select deghosting threshold "
655 "(default %d).\n"
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);
664 break;
666 default:
667 mp_msg(MSGT_VFILTER, MSGL_FATAL,
668 "%s: Unknown argument %s.\n", vf->info->name, q);
669 goto fail;
673 switch(p->pass)
675 case 1:
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);
680 goto fail;
683 break;
685 case 2:
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);
690 goto fail;
693 if(!analyze(p))
694 goto fail;
696 fclose(p->file);
697 p->file=0;
698 break;
701 if(p->window<5) p->window=5;
702 if(!(p->history=calloc(sizeof *p->history, p->window)))
703 goto nomem;
705 diff = diff_C;
706 #if HAVE_MMX && HAVE_EBX_AVAILABLE
707 if(gCpuCaps.hasMMX) diff = diff_MMX;
708 #endif
710 free(args);
711 vf_detc_init_pts_buf(&p->ptsbuf);
712 return 1;
715 const vf_info_t vf_info_divtc =
717 "inverse telecine for deinterlaced video",
718 "divtc",
719 "Ville Saari",
721 vf_open,
722 NULL