vf_phase.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <limits.h>
  22. #include "config.h"
  23. #include "mp_msg.h"
  24. #include "img_format.h"
  25. #include "mp_image.h"
  26. #include "vf.h"
  27. #include "libvo/fastmemcpy.h"
  28. enum mode { PROGRESSIVE, TOP_FIRST, BOTTOM_FIRST,
  29. TOP_FIRST_ANALYZE, BOTTOM_FIRST_ANALYZE,
  30. ANALYZE, FULL_ANALYZE, AUTO, AUTO_ANALYZE };
  31. #define fixed_mode(p) ((p)<=BOTTOM_FIRST)
  32. struct vf_priv_s
  33. {
  34. enum mode mode;
  35. int verbose;
  36. unsigned char *buf[3];
  37. };
  38. /*
  39. * Copy fields from either current or buffered previous frame to the
  40. * output and store the current frame unmodified to the buffer.
  41. */
  42. static void do_plane(unsigned char *to, unsigned char *from,
  43. int w, int h, int ts, int fs,
  44. unsigned char **bufp, enum mode mode)
  45. {
  46. unsigned char *buf, *end;
  47. int top;
  48. if(!*bufp)
  49. {
  50. mode=PROGRESSIVE;
  51. if(!(*bufp=malloc(h*w))) return;
  52. }
  53. for(end=to+h*ts, buf=*bufp, top=1; to<end; from+=fs, to+=ts, buf+=w, top^=1)
  54. {
  55. fast_memcpy(to, mode==(top?BOTTOM_FIRST:TOP_FIRST)?buf:from, w);
  56. fast_memcpy(buf, from, w);
  57. }
  58. }
  59. /*
  60. * This macro interpolates the value of both fields at a point halfway
  61. * between lines and takes the squared difference. In field resolution
  62. * the point is a quarter pixel below a line in one field and a quarter
  63. * pixel above a line in other.
  64. *
  65. * (the result is actually multiplied by 25)
  66. */
  67. #define diff(a, as, b, bs) (t=((*a-b[bs])<<2)+a[as<<1]-b[-bs], t*t)
  68. /*
  69. * Find which field combination has the smallest average squared difference
  70. * between the fields.
  71. */
  72. static enum mode analyze_plane(unsigned char *old, unsigned char *new,
  73. int w, int h, int os, int ns, enum mode mode,
  74. int verbose, int fields)
  75. {
  76. double bdiff, pdiff, tdiff, scale;
  77. int bdif, tdif, pdif;
  78. int top, t;
  79. unsigned char *end, *rend;
  80. if(mode==AUTO)
  81. mode=fields&MP_IMGFIELD_ORDERED?fields&MP_IMGFIELD_TOP_FIRST?
  82. TOP_FIRST:BOTTOM_FIRST:PROGRESSIVE;
  83. else if(mode==AUTO_ANALYZE)
  84. mode=fields&MP_IMGFIELD_ORDERED?fields&MP_IMGFIELD_TOP_FIRST?
  85. TOP_FIRST_ANALYZE:BOTTOM_FIRST_ANALYZE:FULL_ANALYZE;
  86. if(fixed_mode(mode))
  87. bdiff=pdiff=tdiff=65536.0;
  88. else
  89. {
  90. bdiff=pdiff=tdiff=0.0;
  91. for(end=new+(h-2)*ns, new+=ns, old+=os, top=0;
  92. new<end; new+=ns-w, old+=os-w, top^=1)
  93. {
  94. pdif=tdif=bdif=0;
  95. switch(mode)
  96. {
  97. case TOP_FIRST_ANALYZE:
  98. if(top)
  99. for(rend=new+w; new<rend; new++, old++)
  100. pdif+=diff(new, ns, new, ns),
  101. tdif+=diff(new, ns, old, os);
  102. else
  103. for(rend=new+w; new<rend; new++, old++)
  104. pdif+=diff(new, ns, new, ns),
  105. tdif+=diff(old, os, new, ns);
  106. break;
  107. case BOTTOM_FIRST_ANALYZE:
  108. if(top)
  109. for(rend=new+w; new<rend; new++, old++)
  110. pdif+=diff(new, ns, new, ns),
  111. bdif+=diff(old, os, new, ns);
  112. else
  113. for(rend=new+w; new<rend; new++, old++)
  114. pdif+=diff(new, ns, new, ns),
  115. bdif+=diff(new, ns, old, os);
  116. break;
  117. case ANALYZE:
  118. if(top)
  119. for(rend=new+w; new<rend; new++, old++)
  120. tdif+=diff(new, ns, old, os),
  121. bdif+=diff(old, os, new, ns);
  122. else
  123. for(rend=new+w; new<rend; new++, old++)
  124. bdif+=diff(new, ns, old, os),
  125. tdif+=diff(old, os, new, ns);
  126. break;
  127. default: /* FULL_ANALYZE */
  128. if(top)
  129. for(rend=new+w; new<rend; new++, old++)
  130. pdif+=diff(new, ns, new, ns),
  131. tdif+=diff(new, ns, old, os),
  132. bdif+=diff(old, os, new, ns);
  133. else
  134. for(rend=new+w; new<rend; new++, old++)
  135. pdif+=diff(new, ns, new, ns),
  136. bdif+=diff(new, ns, old, os),
  137. tdif+=diff(old, os, new, ns);
  138. }
  139. pdiff+=(double)pdif;
  140. tdiff+=(double)tdif;
  141. bdiff+=(double)bdif;
  142. }
  143. scale=1.0/(w*(h-3))/25.0;
  144. pdiff*=scale;
  145. tdiff*=scale;
  146. bdiff*=scale;
  147. if(mode==TOP_FIRST_ANALYZE)
  148. bdiff=65536.0;
  149. else if(mode==BOTTOM_FIRST_ANALYZE)
  150. tdiff=65536.0;
  151. else if(mode==ANALYZE)
  152. pdiff=65536.0;
  153. if(bdiff<pdiff && bdiff<tdiff)
  154. mode=BOTTOM_FIRST;
  155. else if(tdiff<pdiff && tdiff<bdiff)
  156. mode=TOP_FIRST;
  157. else
  158. mode=PROGRESSIVE;
  159. }
  160. if( mp_msg_test(MSGT_VFILTER,MSGL_V) )
  161. {
  162. mp_msg(MSGT_VFILTER, MSGL_INFO, "%c", mode==BOTTOM_FIRST?'b':mode==TOP_FIRST?'t':'p');
  163. if(tdiff==65536.0) mp_msg(MSGT_VFILTER, MSGL_INFO," N/A "); else mp_msg(MSGT_VFILTER, MSGL_INFO," %8.2f", tdiff);
  164. if(bdiff==65536.0) mp_msg(MSGT_VFILTER, MSGL_INFO," N/A "); else mp_msg(MSGT_VFILTER, MSGL_INFO," %8.2f", bdiff);
  165. if(pdiff==65536.0) mp_msg(MSGT_VFILTER, MSGL_INFO," N/A "); else mp_msg(MSGT_VFILTER, MSGL_INFO," %8.2f", pdiff);
  166. mp_msg(MSGT_VFILTER, MSGL_INFO," \n");
  167. }
  168. return mode;
  169. }
  170. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  171. {
  172. mp_image_t *dmpi;
  173. int w;
  174. enum mode mode;
  175. if(!(dmpi=vf_get_image(vf->next, mpi->imgfmt,
  176. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  177. mpi->w, mpi->h)))
  178. return 0;
  179. w=dmpi->w;
  180. if(!(dmpi->flags&MP_IMGFLAG_PLANAR))
  181. w*=dmpi->bpp/8;
  182. mode=vf->priv->mode;
  183. if(!vf->priv->buf[0])
  184. mode=PROGRESSIVE;
  185. else
  186. mode=analyze_plane(vf->priv->buf[0], mpi->planes[0],
  187. w, dmpi->h, w, mpi->stride[0], mode,
  188. vf->priv->verbose, mpi->fields);
  189. do_plane(dmpi->planes[0], mpi->planes[0],
  190. w, dmpi->h,
  191. dmpi->stride[0], mpi->stride[0],
  192. &vf->priv->buf[0], mode);
  193. if(dmpi->flags&MP_IMGFLAG_PLANAR)
  194. {
  195. do_plane(dmpi->planes[1], mpi->planes[1],
  196. dmpi->chroma_width, dmpi->chroma_height,
  197. dmpi->stride[1], mpi->stride[1],
  198. &vf->priv->buf[1], mode);
  199. do_plane(dmpi->planes[2], mpi->planes[2],
  200. dmpi->chroma_width, dmpi->chroma_height,
  201. dmpi->stride[2], mpi->stride[2],
  202. &vf->priv->buf[2], mode);
  203. }
  204. return vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
  205. }
  206. static void uninit(struct vf_instance *vf)
  207. {
  208. free(vf->priv->buf[0]);
  209. free(vf->priv->buf[1]);
  210. free(vf->priv->buf[2]);
  211. free(vf->priv);
  212. }
  213. static int vf_open(vf_instance_t *vf, char *args)
  214. {
  215. vf->put_image = put_image;
  216. vf->uninit = uninit;
  217. vf->default_reqs = VFCAP_ACCEPT_STRIDE;
  218. if(!(vf->priv = calloc(1, sizeof(struct vf_priv_s))))
  219. {
  220. uninit(vf);
  221. return 0;
  222. }
  223. vf->priv->mode=AUTO_ANALYZE;
  224. vf->priv->verbose=0;
  225. while(args && *args)
  226. {
  227. switch(*args)
  228. {
  229. case 't': vf->priv->mode=TOP_FIRST; break;
  230. case 'a': vf->priv->mode=AUTO; break;
  231. case 'b': vf->priv->mode=BOTTOM_FIRST; break;
  232. case 'u': vf->priv->mode=ANALYZE; break;
  233. case 'T': vf->priv->mode=TOP_FIRST_ANALYZE; break;
  234. case 'A': vf->priv->mode=AUTO_ANALYZE; break;
  235. case 'B': vf->priv->mode=BOTTOM_FIRST_ANALYZE; break;
  236. case 'U': vf->priv->mode=FULL_ANALYZE; break;
  237. case 'p': vf->priv->mode=PROGRESSIVE; break;
  238. case 'v': vf->priv->verbose=1; break;
  239. case ':': break;
  240. default:
  241. uninit(vf);
  242. return 0; /* bad args */
  243. }
  244. if( (args=strchr(args, ':')) ) args++;
  245. }
  246. return 1;
  247. }
  248. const vf_info_t vf_info_phase =
  249. {
  250. "phase shift fields",
  251. "phase",
  252. "Ville Saari",
  253. "",
  254. vf_open,
  255. NULL
  256. };