FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_divtc.c
Go to the documentation of this file.
1 /*
2  * This file is part of MPlayer.
3  *
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.
8  *
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.
13  *
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.
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <math.h>
24 
25 #include "config.h"
26 #include "mp_msg.h"
27 #include "cpudetect.h"
28 #include "libavutil/common.h"
29 #include "mpbswap.h"
30 
31 #include "img_format.h"
32 #include "mp_image.h"
33 #include "vf.h"
34 
35 #include "libvo/fastmemcpy.h"
36 
38 
39 struct vf_priv_s
40  {
42  ocount, sum[5];
43  double threshold;
44  FILE *file;
45  int8_t *bdata;
46  unsigned int *csdata;
47  int *history;
48  };
49 
50 /*
51  * diff_MMX and diff_C stolen from vf_decimate.c
52  */
53 
54 #if HAVE_MMX && HAVE_EBX_AVAILABLE
55 static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
56  {
57  volatile short out[4];
58  __asm__ (
59  "movl $8, %%ecx \n\t"
60  "pxor %%mm4, %%mm4 \n\t"
61  "pxor %%mm7, %%mm7 \n\t"
62 
63  ASMALIGN(4)
64  "1: \n\t"
65 
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"
83 
84  "decl %%ecx \n\t"
85  "jnz 1b \n\t"
86  "movq %%mm4, (%%"REG_d") \n\t"
87  "emms \n\t"
88  :
89  : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
90  : "%ecx", "memory"
91  );
92  return out[0]+out[1]+out[2]+out[3];
93  }
94 #endif
95 
96 static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
97  {
98  int x, y, d=0;
99 
100  for(y=8; y; y--, new+=ns, old+=os)
101  for(x=8; x; x--)
102  d+=abs(new[x]-old[x]);
103 
104  return d;
105  }
106 
107 static int (*diff)(unsigned char *, unsigned char *, int, int);
108 
109 static int diff_plane(unsigned char *old, unsigned char *new,
110  int w, int h, int os, int ns, int arg)
111  {
112  int x, y, d, max=0, sum=0, n=0;
113 
114  for(y=0; y<h-7; y+=8)
115  {
116  for(x=0; x<w-7; x+=8)
117  {
118  d=diff(old+x+y*os, new+x+y*ns, os, ns);
119  if(d>max) max=d;
120  sum+=d;
121  n++;
122  }
123  }
124 
125  return (sum+n*max)/2;
126  }
127 
128 /*
129 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
130  int w, int h, int s, int zs, int arg)
131  {
132  unsigned int shift, sum;
133  unsigned char *e;
134 
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);
138 
139  return sum;
140  }
141 */
142 
143 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
144  int w, int h, int s, int zs, int arg)
145  {
146  unsigned int shift;
147  uint32_t sum, t;
148  unsigned char *e, *e2;
149 #if HAVE_FAST_64BIT
150  typedef uint64_t wsum_t;
151 #else
152  typedef uint32_t wsum_t;
153 #endif
154  wsum_t wsum;
155 
156  for(sum=0; h; h--, p+=s-w)
157  {
158  for(shift=0, e=p+w; (int)p&(sizeof(wsum_t)-1) && p<e;)
159  sum^=*p++<<(shift=(shift-8)&31);
160 
161  for(wsum=0, e2=e-sizeof(wsum_t)+1; p<e2; p+=sizeof(wsum_t))
162  wsum^=*(wsum_t *)p;
163 
164 #if HAVE_FAST_64BIT
165  t=be2me_32((uint32_t)(wsum>>32^wsum));
166 #else
167  t=be2me_32(wsum);
168 #endif
169 
170  for(sum^=(t<<shift|t>>(32-shift)); p<e;)
171  sum^=*p++<<(shift=(shift-8)&31);
172  }
173 
174  return sum;
175  }
176 
177 static int deghost_plane(unsigned char *d, unsigned char *s,
178  int w, int h, int ds, int ss, int threshold)
179  {
180  int t;
181  unsigned char *e;
182 
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;
187 
188  return 0;
189  }
190 
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);
193  return 0;
194 }
195 
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)
199  {
200  if(dst->flags&MP_IMGFLAG_PLANAR)
201  return planeop(dst->planes[0], src?src->planes[0]:0,
202  dst->w, dst->h,
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);
210 
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);
214  }
215 
216 /*
217  * Find the phase in which the telecine pattern fits best to the
218  * given 5 frame slice of frame difference measurements.
219  *
220  * If phase1 and phase2 are not negative, only the two specified
221  * phases are tested.
222  */
223 
224 static int match(struct vf_priv_s *p, int *diffs,
225  int phase1, int phase2, double *strength)
226  {
227  static const int pattern1[]={ -4, 1, 1, 1, 1 },
228  pattern2[]={ -2, -3, 4, 4, -3 }, *pattern;
229  int f, m, n, t[5];
230 
231  pattern=p->deghost>0?pattern2:pattern1;
232 
233  for(f=0; f<5; f++)
234  {
235  if(phase1<0 || phase2<0 || f==phase1 || f==phase2)
236  {
237  for(n=t[f]=0; n<5; n++)
238  t[f]+=diffs[n]*pattern[(n-f+5)%5];
239  }
240  else
241  t[f]=INT_MIN;
242  }
243 
244  /* find the best match */
245  for(m=0, n=1; n<5; n++)
246  if(t[n]>t[m]) m=n;
247 
248  if(strength)
249  {
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;
253 
254  *strength=(t[m]>0?(double)(t[m]-t[f])/t[m]:0.0);
255  }
256 
257  return m;
258  }
259 
260 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
261  {
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;
266  double d;
267 
268  dmpi=ff_vf_get_image(vf->next, mpi->imgfmt,
271  mpi->width, mpi->height);
272  ff_vf_clone_mpi_attributes(dmpi, mpi);
273 
274  newphase=p->phase;
275 
276  switch(p->pass)
277  {
278  case 1:
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);
282  break;
283 
284  case 2:
285  if(p->frameno/5>p->bcount)
286  {
288  "\n%s: Log file ends prematurely! "
289  "Switching to one pass mode.\n", vf->info->name);
290  p->pass=0;
291  break;
292  }
293 
294  checksum=(unsigned int)imgop((void *)checksum_plane, mpi, 0, 0);
295 
296  if(checksum!=p->csdata[p->frameno])
297  {
298  for(f=0; f<100; f++)
299  if(p->frameno+f<p->fcount && p->csdata[p->frameno+f]==checksum)
300  break;
301  else if(p->frameno-f>=0 && p->csdata[p->frameno-f]==checksum)
302  {
303  f=-f;
304  break;
305  }
306 
307  if(f<100)
308  {
310  "\n%s: Mismatch with pass-1: %+d frame(s).\n",
311  vf->info->name, f);
312 
313  p->frameno+=f;
314  p->misscount=0;
315  }
316  else if(p->misscount++>=30)
317  {
319  "\n%s: Sync with pass-1 lost! "
320  "Switching to one pass mode.\n", vf->info->name);
321  p->pass=0;
322  break;
323  }
324  }
325 
326  n=(p->frameno)/5;
327  if(n>=p->bcount) n=p->bcount-1;
328 
329  newphase=p->bdata[n];
330  break;
331 
332  default:
333  if(p->frameno)
334  {
335  int *sump=p->sum+p->frameno%5,
336  *histp=p->history+p->frameno%p->window;
337 
338  *sump-=*histp;
339  *sump+=(*histp=imgop(diff_plane, dmpi, mpi, 0));
340  }
341 
342  m=match(p, p->sum, -1, -1, &d);
343 
344  if(d>=p->threshold)
345  newphase=m;
346  }
347 
348  n=p->ocount++%5;
349 
350  if(newphase!=p->phase && ((p->phase+4)%5<n)==((newphase+4)%5<n))
351  {
352  p->phase=newphase;
354  "\n%s: Telecine phase %d.\n", vf->info->name, p->phase);
355  }
356 
357  switch((p->frameno++-p->phase+10)%5)
358  {
359  case 0:
360  imgop(copyop, dmpi, mpi, 0);
361  return 0;
362 
363  case 4:
364  if(p->deghost>0)
365  {
366  tmpi=ff_vf_get_image(vf->next, mpi->imgfmt,
369  mpi->width, mpi->height);
370  ff_vf_clone_mpi_attributes(tmpi, mpi);
371 
372  imgop(copyop, tmpi, mpi, 0);
373  imgop(deghost_plane, tmpi, dmpi, p->deghost);
374  imgop(copyop, dmpi, mpi, 0);
375  return ff_vf_next_put_image(vf, tmpi, MP_NOPTS_VALUE);
376  }
377  }
378 
379  imgop(copyop, dmpi, mpi, 0);
380  return ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
381  }
382 
383 static int analyze(struct vf_priv_s *p)
384  {
385  int *buf=0, *bp, bufsize=0, n, b, f, i, j, m, s;
386  unsigned int *cbuf=0, *cp;
387  int8_t *pbuf;
388  int8_t lbuf[256];
389  int sum[5];
390  double d;
391 
392  /* read the file */
393 
394  n=15;
395  while(fgets(lbuf, 256, p->file))
396  {
397  if(n>=bufsize-19)
398  {
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;
402 
403  if(!bp || !cp)
404  {
405  ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Not enough memory.\n",
406  ff_vf_info_divtc.name);
407  free(buf);
408  free(cbuf);
409  return 0;
410  }
411  }
412  sscanf(lbuf, "%x %d", cbuf+n, buf+n);
413  n++;
414  }
415 
416  if(n <= 15)
417  {
418  ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Empty 2-pass log file.\n",
419  ff_vf_info_divtc.name);
420  free(buf);
421  free(cbuf);
422  return 0;
423  }
424 
425  /* generate some dummy data past the beginning and end of the array */
426 
427  buf+=15, cbuf+=15;
428  n-=15;
429 
430  memcpy(buf-15, buf, 15*sizeof *buf);
431  memset(cbuf-15, 0, 15*sizeof *cbuf);
432 
433  while(n%5)
434  buf[n]=buf[n-5], cbuf[n]=0, n++;
435 
436  memcpy(buf+n, buf+n-15, 15*sizeof *buf);
437  memset(cbuf+n, 0, 15*sizeof *cbuf);
438 
439  p->csdata=cbuf;
440  p->fcount=n;
441 
442  /* array with one slot for each slice of 5 frames */
443 
444  p->bdata=pbuf=malloc(p->bcount=b=(n/5));
445  memset(pbuf, 255, b);
446 
447  /* resolve the automatic mode */
448 
449  if(p->deghost<0)
450  {
451  int deghost=-p->deghost;
452  double s0=0.0, s1=0.0;
453 
454  for(f=0; f<n; f+=5)
455  {
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;
458  }
459 
460  p->deghost=s1>s0?deghost:0;
461 
463  "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
464  ff_vf_info_divtc.name,
465  p->deghost?"ON":"OFF",
466  10.0*log10(s1/s0));
467  }
468 
469  /* analyze the data */
470 
471  for(f=0; f<5; f++)
472  for(sum[f]=0, n=-15; n<20; n+=5)
473  sum[f]+=buf[n+f];
474 
475  for(f=0; f<b; f++)
476  {
477  m=match(p, sum, -1, -1, &d);
478 
479  if(d>=p->threshold)
480  pbuf[f]=m;
481 
482  if(f<b-1)
483  for(n=0; n<5; n++)
484  sum[n]=sum[n]-buf[5*(f-3)+n]+buf[5*(f+4)+n];
485  }
486 
487  /* fill in the gaps */
488 
489  /* the beginning */
490  for(f=0; f<b && pbuf[f]==-1; f++);
491 
492  if(f==b)
493  {
494  free(buf-15);
495  ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: No telecine pattern found!\n",
496  ff_vf_info_divtc.name);
497  return 0;
498  }
499 
500  for(n=0; n<f; pbuf[n++]=pbuf[f]);
501 
502  /* the end */
503  for(f=b-1; pbuf[f]==-1; f--);
504  for(n=f+1; n<b; pbuf[n++]=pbuf[f]);
505 
506  /* the rest */
507  for(f=0;;)
508  {
509  while(f<b && pbuf[f]!=-1) f++;
510  if(f==b) break;
511  for(n=f; pbuf[n]==-1; n++);
512 
513  if(pbuf[f-1]==pbuf[n])
514  {
515  /* just a gap */
516  while(f<n) pbuf[f++]=pbuf[n];
517  }
518  else
519  {
520  /* phase change, reanalyze the original data in the gap with zero
521  threshold for only the two phases that appear at the ends */
522 
523  for(i=0; i<5; i++)
524  for(sum[i]=0, j=5*f-15; j<5*f; j+=5)
525  sum[i]+=buf[i+j];
526 
527  for(i=f; i<n; i++)
528  {
529  pbuf[i]=match(p, sum, pbuf[f-1], pbuf[n], 0);
530 
531  for(j=0; j<5; j++)
532  sum[j]=sum[j]-buf[5*(i-3)+j]+buf[5*(i+4)+j];
533  }
534 
535  /* estimate the transition point by dividing the gap
536  in the same proportion as the number of matches of each kind */
537 
538  for(i=f, m=f; i<n; i++)
539  if(pbuf[i]==pbuf[f-1]) m++;
540 
541  /* find the transition of the right direction nearest to the
542  estimated point */
543 
544  if(m>f && m<n)
545  {
546  for(j=m; j>f; j--)
547  if(pbuf[j-1]==pbuf[f-1] && pbuf[j]==pbuf[n]) break;
548  for(s=m; s<n; s++)
549  if(pbuf[s-1]==pbuf[f-1] && pbuf[s]==pbuf[n]) break;
550 
551  m=(s-m<m-j)?s:j;
552  }
553 
554  /* and rewrite the data to allow only this one transition */
555 
556  for(i=f; i<m; i++)
557  pbuf[i]=pbuf[f-1];
558 
559  for(; i<n; i++)
560  pbuf[i]=pbuf[n];
561 
562  f=n;
563  }
564  }
565 
566  free(buf-15);
567 
568  return 1;
569  }
570 
571 static int query_format(struct vf_instance *vf, unsigned int fmt)
572  {
573  switch(fmt)
574  {
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 ff_vf_next_query_format(vf,fmt);
581  }
582 
583  return 0;
584  }
585 
586 static void uninit(struct vf_instance *vf)
587  {
588  if(vf->priv)
589  {
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);
594  free(vf->priv);
595  }
596  }
597 
598 static int vf_open(vf_instance_t *vf, char *args)
599  {
600  struct vf_priv_s *p;
601  const char *filename="framediff.log";
602  char *ap, *q, *a;
603 
604  if(args && !(args=strdup(args)))
605  {
606  nomem:
608  "%s: Not enough memory.\n", vf->info->name);
609  fail:
610  uninit(vf);
611  free(args);
612  return 0;
613  }
614 
615  vf->put_image=put_image;
616  vf->uninit=uninit;
619  if(!(vf->priv=p=calloc(1, sizeof(struct vf_priv_s))))
620  goto nomem;
621 
622  p->phase=5;
623  p->threshold=0.5;
624  p->window=30;
625 
626  if((ap=args))
627  while(*ap)
628  {
629  q=ap;
630  if((ap=strchr(q, ':'))) *ap++=0; else ap=q+strlen(q);
631  if((a=strchr(q, '='))) *a++=0; else a=q+strlen(q);
632 
633  switch(*q)
634  {
635  case 0: break;
636  case 'f': filename=a; break;
637  case 't': p->threshold=atof(a); break;
638  case 'w': p->window=5*(atoi(a)+4)/5; break;
639  case 'd': p->deghost=atoi(a); break;
640  case 'p':
641  if(q[1]=='h') p->phase=atoi(a);
642  else p->pass=atoi(a);
643  break;
644 
645  case 'h':
647  "\n%s options:\n\n"
648  "pass=1|2 - Use 2-pass mode.\n"
649  "file=filename - Set the 2-pass log file name "
650  "(default %s).\n"
651  "threshold=value - Set the pattern recognition "
652  "sensitivity (default %g).\n"
653  "deghost=value - Select deghosting threshold "
654  "(default %d).\n"
655  "window=numframes - Set the statistics window "
656  "for 1-pass mode (default %d).\n"
657  "phase=0|1|2|3|4 - Set the initial phase "
658  "for 1-pass mode (default %d).\n\n"
659  "The option names can be abbreviated to the shortest "
660  "unique prefix.\n\n",
661  vf->info->name, filename, p->threshold, p->deghost,
662  p->window, p->phase%5);
663  break;
664 
665  default:
667  "%s: Unknown argument %s.\n", vf->info->name, q);
668  goto fail;
669  }
670  }
671 
672  switch(p->pass)
673  {
674  case 1:
675  if(!(p->file=fopen(filename, "w")))
676  {
678  "%s: Can't create file %s.\n", vf->info->name, filename);
679  goto fail;
680  }
681 
682  break;
683 
684  case 2:
685  if(!(p->file=fopen(filename, "r")))
686  {
688  "%s: Can't open file %s.\n", vf->info->name, filename);
689  goto fail;
690  }
691 
692  if(!analyze(p))
693  goto fail;
694 
695  fclose(p->file);
696  p->file=0;
697  break;
698  }
699 
700  if(p->window<5) p->window=5;
701  if(!(p->history=calloc(sizeof *p->history, p->window)))
702  goto nomem;
703 
704  diff = diff_C;
705 #if HAVE_MMX && HAVE_EBX_AVAILABLE
706  if(ff_gCpuCaps.hasMMX) diff = diff_MMX;
707 #endif
708 
709  free(args);
710  return 1;
711  }
712 
713 const vf_info_t ff_vf_info_divtc =
714  {
715  "inverse telecine for deinterlaced video",
716  "divtc",
717  "Ville Saari",
718  "",
719  vf_open,
720  NULL
721  };