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 "libavutil/x86/asm.h"
30 #include "mpbswap.h"
31 
32 #include "img_format.h"
33 #include "mp_image.h"
34 #include "vf.h"
35 
36 #include "libvo/fastmemcpy.h"
37 
39 
40 struct vf_priv_s
41  {
43  ocount, sum[5];
44  double threshold;
45  FILE *file;
46  int8_t *bdata;
47  unsigned int *csdata;
48  int *history;
49  };
50 
51 /*
52  * diff_MMX and diff_C stolen from vf_decimate.c
53  */
54 
55 #if HAVE_MMX && HAVE_EBX_AVAILABLE
56 static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
57  {
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"
63 
64  ASMALIGN(4)
65  "1: \n\t"
66 
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"
84 
85  "decl %%ecx \n\t"
86  "jnz 1b \n\t"
87  "movq %%mm4, (%%"REG_d") \n\t"
88  "emms \n\t"
89  :
90  : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
91  : "%ecx", "memory"
92  );
93  return out[0]+out[1]+out[2]+out[3];
94  }
95 #endif
96 
97 static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
98  {
99  int x, y, d=0;
100 
101  for(y=8; y; y--, new+=ns, old+=os)
102  for(x=8; x; x--)
103  d+=abs(new[x]-old[x]);
104 
105  return d;
106  }
107 
108 static int (*diff)(unsigned char *, unsigned char *, int, int);
109 
110 static int diff_plane(unsigned char *old, unsigned char *new,
111  int w, int h, int os, int ns, int arg)
112  {
113  int x, y, d, max=0, sum=0, n=0;
114 
115  for(y=0; y<h-7; y+=8)
116  {
117  for(x=0; x<w-7; x+=8)
118  {
119  d=diff(old+x+y*os, new+x+y*ns, os, ns);
120  if(d>max) max=d;
121  sum+=d;
122  n++;
123  }
124  }
125 
126  return (sum+n*max)/2;
127  }
128 
129 /*
130 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
131  int w, int h, int s, int zs, int arg)
132  {
133  unsigned int shift, sum;
134  unsigned char *e;
135 
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);
139 
140  return sum;
141  }
142 */
143 
144 static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
145  int w, int h, int s, int zs, int arg)
146  {
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;
156 
157  for(sum=0; h; h--, p+=s-w)
158  {
159  for(shift=0, e=p+w; (int)p&(sizeof(wsum_t)-1) && p<e;)
160  sum^=*p++<<(shift=(shift-8)&31);
161 
162  for(wsum=0, e2=e-sizeof(wsum_t)+1; p<e2; p+=sizeof(wsum_t))
163  wsum^=*(wsum_t *)p;
164 
165 #if HAVE_FAST_64BIT
166  t=be2me_32((uint32_t)(wsum>>32^wsum));
167 #else
168  t=be2me_32(wsum);
169 #endif
170 
171  for(sum^=(t<<shift|t>>(32-shift)); p<e;)
172  sum^=*p++<<(shift=(shift-8)&31);
173  }
174 
175  return sum;
176  }
177 
178 static int deghost_plane(unsigned char *d, unsigned char *s,
179  int w, int h, int ds, int ss, int threshold)
180  {
181  int t;
182  unsigned char *e;
183 
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;
188 
189  return 0;
190  }
191 
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;
195 }
196 
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)
200  {
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);
211 
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);
215  }
216 
217 /*
218  * Find the phase in which the telecine pattern fits best to the
219  * given 5 frame slice of frame difference measurements.
220  *
221  * If phase1 and phase2 are not negative, only the two specified
222  * phases are tested.
223  */
224 
225 static int match(struct vf_priv_s *p, int *diffs,
226  int phase1, int phase2, double *strength)
227  {
228  static const int pattern1[]={ -4, 1, 1, 1, 1 },
229  pattern2[]={ -2, -3, 4, 4, -3 }, *pattern;
230  int f, m, n, t[5];
231 
232  pattern=p->deghost>0?pattern2:pattern1;
233 
234  for(f=0; f<5; f++)
235  {
236  if(phase1<0 || phase2<0 || f==phase1 || f==phase2)
237  {
238  for(n=t[f]=0; n<5; n++)
239  t[f]+=diffs[n]*pattern[(n-f+5)%5];
240  }
241  else
242  t[f]=INT_MIN;
243  }
244 
245  /* find the best match */
246  for(m=0, n=1; n<5; n++)
247  if(t[n]>t[m]) m=n;
248 
249  if(strength)
250  {
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;
254 
255  *strength=(t[m]>0?(double)(t[m]-t[f])/t[m]:0.0);
256  }
257 
258  return m;
259  }
260 
261 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
262  {
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;
268 
269  dmpi=ff_vf_get_image(vf->next, mpi->imgfmt,
272  mpi->width, mpi->height);
273  ff_vf_clone_mpi_attributes(dmpi, mpi);
274 
275  newphase=p->phase;
276 
277  switch(p->pass)
278  {
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;
284 
285  case 2:
286  if(p->frameno/5>p->bcount)
287  {
289  "\n%s: Log file ends prematurely! "
290  "Switching to one pass mode.\n", vf->info->name);
291  p->pass=0;
292  break;
293  }
294 
295  checksum=(unsigned int)imgop((void *)checksum_plane, mpi, 0, 0);
296 
297  if(checksum!=p->csdata[p->frameno])
298  {
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)
303  {
304  f=-f;
305  break;
306  }
307 
308  if(f<100)
309  {
311  "\n%s: Mismatch with pass-1: %+d frame(s).\n",
312  vf->info->name, f);
313 
314  p->frameno+=f;
315  p->misscount=0;
316  }
317  else if(p->misscount++>=30)
318  {
320  "\n%s: Sync with pass-1 lost! "
321  "Switching to one pass mode.\n", vf->info->name);
322  p->pass=0;
323  break;
324  }
325  }
326 
327  n=(p->frameno)/5;
328  if(n>=p->bcount) n=p->bcount-1;
329 
330  newphase=p->bdata[n];
331  break;
332 
333  default:
334  if(p->frameno)
335  {
336  int *sump=p->sum+p->frameno%5,
337  *histp=p->history+p->frameno%p->window;
338 
339  *sump-=*histp;
340  *sump+=(*histp=imgop(diff_plane, dmpi, mpi, 0));
341  }
342 
343  m=match(p, p->sum, -1, -1, &d);
344 
345  if(d>=p->threshold)
346  newphase=m;
347  }
348 
349  n=p->ocount++%5;
350 
351  if(newphase!=p->phase && ((p->phase+4)%5<n)==((newphase+4)%5<n))
352  {
353  p->phase=newphase;
355  "\n%s: Telecine phase %d.\n", vf->info->name, p->phase);
356  }
357 
358  switch((p->frameno++-p->phase+10)%5)
359  {
360  case 0:
361  imgop(copyop, dmpi, mpi, 0);
362  return 0;
363 
364  case 4:
365  if(p->deghost>0)
366  {
367  tmpi=ff_vf_get_image(vf->next, mpi->imgfmt,
370  mpi->width, mpi->height);
371  ff_vf_clone_mpi_attributes(tmpi, mpi);
372 
373  imgop(copyop, tmpi, mpi, 0);
374  imgop(deghost_plane, tmpi, dmpi, p->deghost);
375  imgop(copyop, dmpi, mpi, 0);
376  return ff_vf_next_put_image(vf, tmpi, MP_NOPTS_VALUE);
377  }
378  }
379 
380  imgop(copyop, dmpi, mpi, 0);
381  return ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
382  }
383 
384 static int analyze(struct vf_priv_s *p)
385  {
386  int *buf=0, *bp, bufsize=0, n, b, f, i, j, m, s;
387  unsigned int *cbuf=0, *cp;
388  int8_t *pbuf;
389  int8_t lbuf[256];
390  int sum[5];
391  double d;
392 
393  /* read the file */
394 
395  n=15;
396  while(fgets(lbuf, 256, p->file))
397  {
398  if(n>=bufsize-19)
399  {
400  bufsize=bufsize?bufsize*2:30000;
401  if((bp=realloc(buf, bufsize*sizeof *buf))) buf=bp;
402  if((cp=realloc(cbuf, bufsize*sizeof *cbuf))) cbuf=cp;
403 
404  if(!bp || !cp)
405  {
406  ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Not enough memory.\n",
407  ff_vf_info_divtc.name);
408  free(buf);
409  free(cbuf);
410  return 0;
411  }
412  }
413  sscanf(lbuf, "%x %d", cbuf+n, buf+n);
414  n++;
415  }
416 
417  if(n <= 15)
418  {
419  ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Empty 2-pass log file.\n",
420  ff_vf_info_divtc.name);
421  free(buf);
422  free(cbuf);
423  return 0;
424  }
425 
426  /* generate some dummy data past the beginning and end of the array */
427 
428  buf+=15, cbuf+=15;
429  n-=15;
430 
431  memcpy(buf-15, buf, 15*sizeof *buf);
432  memset(cbuf-15, 0, 15*sizeof *cbuf);
433 
434  while(n%5)
435  buf[n]=buf[n-5], cbuf[n]=0, n++;
436 
437  memcpy(buf+n, buf+n-15, 15*sizeof *buf);
438  memset(cbuf+n, 0, 15*sizeof *cbuf);
439 
440  p->csdata=cbuf;
441  p->fcount=n;
442 
443  /* array with one slot for each slice of 5 frames */
444 
445  p->bdata=pbuf=malloc(p->bcount=b=(n/5));
446  memset(pbuf, 255, b);
447 
448  /* resolve the automatic mode */
449 
450  if(p->deghost<0)
451  {
452  int deghost=-p->deghost;
453  double s0=0.0, s1=0.0;
454 
455  for(f=0; f<n; f+=5)
456  {
457  p->deghost=0; match(p, buf+f, -1, -1, &d); s0+=d;
458  p->deghost=1; match(p, buf+f, -1, -1, &d); s1+=d;
459  }
460 
461  p->deghost=s1>s0?deghost:0;
462 
464  "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
465  ff_vf_info_divtc.name,
466  p->deghost?"ON":"OFF",
467  10.0*log10(s1/s0));
468  }
469 
470  /* analyze the data */
471 
472  for(f=0; f<5; f++)
473  for(sum[f]=0, n=-15; n<20; n+=5)
474  sum[f]+=buf[n+f];
475 
476  for(f=0; f<b; f++)
477  {
478  m=match(p, sum, -1, -1, &d);
479 
480  if(d>=p->threshold)
481  pbuf[f]=m;
482 
483  if(f<b-1)
484  for(n=0; n<5; n++)
485  sum[n]=sum[n]-buf[5*(f-3)+n]+buf[5*(f+4)+n];
486  }
487 
488  /* fill in the gaps */
489 
490  /* the beginning */
491  for(f=0; f<b && pbuf[f]==-1; f++);
492 
493  if(f==b)
494  {
495  free(buf-15);
496  ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: No telecine pattern found!\n",
497  ff_vf_info_divtc.name);
498  return 0;
499  }
500 
501  for(n=0; n<f; pbuf[n++]=pbuf[f]);
502 
503  /* the end */
504  for(f=b-1; pbuf[f]==-1; f--);
505  for(n=f+1; n<b; pbuf[n++]=pbuf[f]);
506 
507  /* the rest */
508  for(f=0;;)
509  {
510  while(f<b && pbuf[f]!=-1) f++;
511  if(f==b) break;
512  for(n=f; pbuf[n]==-1; n++);
513 
514  if(pbuf[f-1]==pbuf[n])
515  {
516  /* just a gap */
517  while(f<n) pbuf[f++]=pbuf[n];
518  }
519  else
520  {
521  /* phase change, reanalyze the original data in the gap with zero
522  threshold for only the two phases that appear at the ends */
523 
524  for(i=0; i<5; i++)
525  for(sum[i]=0, j=5*f-15; j<5*f; j+=5)
526  sum[i]+=buf[i+j];
527 
528  for(i=f; i<n; i++)
529  {
530  pbuf[i]=match(p, sum, pbuf[f-1], pbuf[n], 0);
531 
532  for(j=0; j<5; j++)
533  sum[j]=sum[j]-buf[5*(i-3)+j]+buf[5*(i+4)+j];
534  }
535 
536  /* estimate the transition point by dividing the gap
537  in the same proportion as the number of matches of each kind */
538 
539  for(i=f, m=f; i<n; i++)
540  if(pbuf[i]==pbuf[f-1]) m++;
541 
542  /* find the transition of the right direction nearest to the
543  estimated point */
544 
545  if(m>f && m<n)
546  {
547  for(j=m; j>f; j--)
548  if(pbuf[j-1]==pbuf[f-1] && pbuf[j]==pbuf[n]) break;
549  for(s=m; s<n; s++)
550  if(pbuf[s-1]==pbuf[f-1] && pbuf[s]==pbuf[n]) break;
551 
552  m=(s-m<m-j)?s:j;
553  }
554 
555  /* and rewrite the data to allow only this one transition */
556 
557  for(i=f; i<m; i++)
558  pbuf[i]=pbuf[f-1];
559 
560  for(; i<n; i++)
561  pbuf[i]=pbuf[n];
562 
563  f=n;
564  }
565  }
566 
567  free(buf-15);
568 
569  return 1;
570  }
571 
572 static int query_format(struct vf_instance *vf, unsigned int fmt)
573  {
574  switch(fmt)
575  {
576  case IMGFMT_444P: case IMGFMT_IYUV: case IMGFMT_RGB24:
577  case IMGFMT_422P: case IMGFMT_UYVY: case IMGFMT_BGR24:
578  case IMGFMT_411P: case IMGFMT_YUY2: case IMGFMT_IF09:
579  case IMGFMT_YV12: case IMGFMT_I420: case IMGFMT_YVU9:
580  case IMGFMT_IUYV: case IMGFMT_Y800: case IMGFMT_Y8:
581  return ff_vf_next_query_format(vf,fmt);
582  }
583 
584  return 0;
585  }
586 
587 static void uninit(struct vf_instance *vf)
588  {
589  if(vf->priv)
590  {
591  if(vf->priv->file) fclose(vf->priv->file);
592  if(vf->priv->csdata) free(vf->priv->csdata-15);
593  free(vf->priv->bdata);
594  free(vf->priv->history);
595  free(vf->priv);
596  }
597  }
598 
599 static int vf_open(vf_instance_t *vf, char *args)
600  {
601  struct vf_priv_s *p;
602  const char *filename="framediff.log";
603  char *ap, *q, *a;
604 
605  if(args && !(args=strdup(args)))
606  {
607  nomem:
609  "%s: Not enough memory.\n", vf->info->name);
610  fail:
611  uninit(vf);
612  free(args);
613  return 0;
614  }
615 
616  vf->put_image=put_image;
617  vf->uninit=uninit;
620  if(!(vf->priv=p=calloc(1, sizeof(struct vf_priv_s))))
621  goto nomem;
622 
623  p->phase=5;
624  p->threshold=0.5;
625  p->window=30;
626 
627  if((ap=args))
628  while(*ap)
629  {
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);
633 
634  switch(*q)
635  {
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;
645 
646  case 'h':
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;
665 
666  default:
668  "%s: Unknown argument %s.\n", vf->info->name, q);
669  goto fail;
670  }
671  }
672 
673  switch(p->pass)
674  {
675  case 1:
676  if(!(p->file=fopen(filename, "w")))
677  {
679  "%s: Can't create file %s.\n", vf->info->name, filename);
680  goto fail;
681  }
682 
683  break;
684 
685  case 2:
686  if(!(p->file=fopen(filename, "r")))
687  {
689  "%s: Can't open file %s.\n", vf->info->name, filename);
690  goto fail;
691  }
692 
693  if(!analyze(p))
694  goto fail;
695 
696  fclose(p->file);
697  p->file=0;
698  break;
699  }
700 
701  if(p->window<5) p->window=5;
702  if(!(p->history=calloc(sizeof *p->history, p->window)))
703  goto nomem;
704 
705  diff = diff_C;
706 #if HAVE_MMX && HAVE_EBX_AVAILABLE
707  if(ff_gCpuCaps.hasMMX) diff = diff_MMX;
708 #endif
709 
710  free(args);
711  return 1;
712  }
713 
714 const vf_info_t ff_vf_info_divtc =
715  {
716  "inverse telecine for deinterlaced video",
717  "divtc",
718  "Ville Saari",
719  "",
720  vf_open,
721  NULL
722  };