vf_kerndeint.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Original AVISynth Filter Copyright (C) 2003 Donald A. Graft
  3. * Adapted to MPlayer by Tobias Diedrich
  4. *
  5. * This file is part of MPlayer.
  6. *
  7. * MPlayer is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * MPlayer is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <inttypes.h>
  25. #include <math.h>
  26. #include "mp_msg.h"
  27. #include "img_format.h"
  28. #include "mp_image.h"
  29. #include "vf.h"
  30. #include "libvo/fastmemcpy.h"
  31. //===========================================================================//
  32. struct vf_priv_s {
  33. int frame;
  34. int map;
  35. int order;
  36. int thresh;
  37. int sharp;
  38. int twoway;
  39. int do_deinterlace;
  40. };
  41. /***************************************************************************/
  42. static int config(struct vf_instance *vf,
  43. int width, int height, int d_width, int d_height,
  44. unsigned int flags, unsigned int outfmt){
  45. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  46. }
  47. static void uninit(struct vf_instance *vf)
  48. {
  49. free(vf->priv);
  50. }
  51. static inline int IsRGB(mp_image_t *mpi)
  52. {
  53. return mpi->imgfmt == IMGFMT_RGB;
  54. }
  55. static inline int IsYUY2(mp_image_t *mpi)
  56. {
  57. return mpi->imgfmt == IMGFMT_YUY2;
  58. }
  59. #define PLANAR_Y 0
  60. #define PLANAR_U 1
  61. #define PLANAR_V 2
  62. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  63. int cw= mpi->w >> mpi->chroma_x_shift;
  64. int ch= mpi->h >> mpi->chroma_y_shift;
  65. int W = mpi->w, H = mpi->h;
  66. const unsigned char *prvp, *prvpp, *prvpn, *prvpnn, *prvppp, *prvp4p, *prvp4n;
  67. const unsigned char *srcp_saved;
  68. const unsigned char *srcp, *srcpp, *srcpn, *srcpnn, *srcppp, *srcp3p, *srcp3n, *srcp4p, *srcp4n;
  69. unsigned char *dstp, *dstp_saved;
  70. int src_pitch;
  71. int psrc_pitch;
  72. int dst_pitch;
  73. int x, y, z;
  74. int n = vf->priv->frame++;
  75. int val, hi, lo, w, h;
  76. double valf;
  77. int plane;
  78. int threshold = vf->priv->thresh;
  79. int order = vf->priv->order;
  80. int map = vf->priv->map;
  81. int sharp = vf->priv->sharp;
  82. int twoway = vf->priv->twoway;
  83. mp_image_t *dmpi, *pmpi;
  84. if(!vf->priv->do_deinterlace)
  85. return vf_next_put_image(vf, mpi, pts);
  86. dmpi=vf_get_image(vf->next,mpi->imgfmt,
  87. MP_IMGTYPE_IP, MP_IMGFLAG_ACCEPT_STRIDE,
  88. mpi->w,mpi->h);
  89. pmpi=vf_get_image(vf->next,mpi->imgfmt,
  90. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  91. mpi->w,mpi->h);
  92. if(!dmpi) return 0;
  93. for (z=0; z<mpi->num_planes; z++) {
  94. if (z == 0) plane = PLANAR_Y;
  95. else if (z == 1) plane = PLANAR_U;
  96. else plane = PLANAR_V;
  97. h = plane == PLANAR_Y ? H : ch;
  98. w = plane == PLANAR_Y ? W : cw;
  99. srcp = srcp_saved = mpi->planes[z];
  100. src_pitch = mpi->stride[z];
  101. psrc_pitch = pmpi->stride[z];
  102. dstp = dstp_saved = dmpi->planes[z];
  103. dst_pitch = dmpi->stride[z];
  104. srcp = srcp_saved + (1-order) * src_pitch;
  105. dstp = dstp_saved + (1-order) * dst_pitch;
  106. for (y=0; y<h; y+=2) {
  107. fast_memcpy(dstp, srcp, w);
  108. srcp += 2*src_pitch;
  109. dstp += 2*dst_pitch;
  110. }
  111. // Copy through the lines that will be missed below.
  112. fast_memcpy(dstp_saved + order*dst_pitch, srcp_saved + (1-order)*src_pitch, w);
  113. fast_memcpy(dstp_saved + (2+order)*dst_pitch, srcp_saved + (3-order)*src_pitch, w);
  114. fast_memcpy(dstp_saved + (h-2+order)*dst_pitch, srcp_saved + (h-1-order)*src_pitch, w);
  115. fast_memcpy(dstp_saved + (h-4+order)*dst_pitch, srcp_saved + (h-3-order)*src_pitch, w);
  116. /* For the other field choose adaptively between using the previous field
  117. or the interpolant from the current field. */
  118. prvp = pmpi->planes[z] + 5*psrc_pitch - (1-order)*psrc_pitch;
  119. prvpp = prvp - psrc_pitch;
  120. prvppp = prvp - 2*psrc_pitch;
  121. prvp4p = prvp - 4*psrc_pitch;
  122. prvpn = prvp + psrc_pitch;
  123. prvpnn = prvp + 2*psrc_pitch;
  124. prvp4n = prvp + 4*psrc_pitch;
  125. srcp = srcp_saved + 5*src_pitch - (1-order)*src_pitch;
  126. srcpp = srcp - src_pitch;
  127. srcppp = srcp - 2*src_pitch;
  128. srcp3p = srcp - 3*src_pitch;
  129. srcp4p = srcp - 4*src_pitch;
  130. srcpn = srcp + src_pitch;
  131. srcpnn = srcp + 2*src_pitch;
  132. srcp3n = srcp + 3*src_pitch;
  133. srcp4n = srcp + 4*src_pitch;
  134. dstp = dstp_saved + 5*dst_pitch - (1-order)*dst_pitch;
  135. for (y = 5 - (1-order); y <= h - 5 - (1-order); y+=2)
  136. {
  137. for (x = 0; x < w; x++)
  138. {
  139. if ((threshold == 0) || (n == 0) ||
  140. (abs((int)prvp[x] - (int)srcp[x]) > threshold) ||
  141. (abs((int)prvpp[x] - (int)srcpp[x]) > threshold) ||
  142. (abs((int)prvpn[x] - (int)srcpn[x]) > threshold))
  143. {
  144. if (map == 1)
  145. {
  146. int g = x & ~3;
  147. if (IsRGB(mpi) == 1)
  148. {
  149. dstp[g++] = 255;
  150. dstp[g++] = 255;
  151. dstp[g++] = 255;
  152. dstp[g] = 255;
  153. x = g;
  154. }
  155. else if (IsYUY2(mpi) == 1)
  156. {
  157. dstp[g++] = 235;
  158. dstp[g++] = 128;
  159. dstp[g++] = 235;
  160. dstp[g] = 128;
  161. x = g;
  162. }
  163. else
  164. {
  165. if (plane == PLANAR_Y) dstp[x] = 235;
  166. else dstp[x] = 128;
  167. }
  168. }
  169. else
  170. {
  171. if (IsRGB(mpi))
  172. {
  173. hi = 255;
  174. lo = 0;
  175. }
  176. else if (IsYUY2(mpi))
  177. {
  178. hi = (x & 1) ? 240 : 235;
  179. lo = 16;
  180. }
  181. else
  182. {
  183. hi = (plane == PLANAR_Y) ? 235 : 240;
  184. lo = 16;
  185. }
  186. if (sharp == 1)
  187. {
  188. if (twoway == 1)
  189. valf = + 0.526*((int)srcpp[x] + (int)srcpn[x])
  190. + 0.170*((int)srcp[x] + (int)prvp[x])
  191. - 0.116*((int)srcppp[x] + (int)srcpnn[x] + (int)prvppp[x] + (int)prvpnn[x])
  192. - 0.026*((int)srcp3p[x] + (int)srcp3n[x])
  193. + 0.031*((int)srcp4p[x] + (int)srcp4n[x] + (int)prvp4p[x] + (int)prvp4n[x]);
  194. else
  195. valf = + 0.526*((int)srcpp[x] + (int)srcpn[x])
  196. + 0.170*((int)prvp[x])
  197. - 0.116*((int)prvppp[x] + (int)prvpnn[x])
  198. - 0.026*((int)srcp3p[x] + (int)srcp3n[x])
  199. + 0.031*((int)prvp4p[x] + (int)prvp4p[x]);
  200. if (valf > hi) valf = hi;
  201. else if (valf < lo) valf = lo;
  202. dstp[x] = (int) valf;
  203. }
  204. else
  205. {
  206. if (twoway == 1)
  207. val = (8*((int)srcpp[x] + (int)srcpn[x]) + 2*((int)srcp[x] + (int)prvp[x]) -
  208. (int)(srcppp[x]) - (int)(srcpnn[x]) -
  209. (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
  210. else
  211. val = (8*((int)srcpp[x] + (int)srcpn[x]) + 2*((int)prvp[x]) -
  212. (int)(prvppp[x]) - (int)(prvpnn[x])) >> 4;
  213. if (val > hi) val = hi;
  214. else if (val < lo) val = lo;
  215. dstp[x] = (int) val;
  216. }
  217. }
  218. }
  219. else
  220. {
  221. dstp[x] = srcp[x];
  222. }
  223. }
  224. prvp += 2*psrc_pitch;
  225. prvpp += 2*psrc_pitch;
  226. prvppp += 2*psrc_pitch;
  227. prvpn += 2*psrc_pitch;
  228. prvpnn += 2*psrc_pitch;
  229. prvp4p += 2*psrc_pitch;
  230. prvp4n += 2*psrc_pitch;
  231. srcp += 2*src_pitch;
  232. srcpp += 2*src_pitch;
  233. srcppp += 2*src_pitch;
  234. srcp3p += 2*src_pitch;
  235. srcp4p += 2*src_pitch;
  236. srcpn += 2*src_pitch;
  237. srcpnn += 2*src_pitch;
  238. srcp3n += 2*src_pitch;
  239. srcp4n += 2*src_pitch;
  240. dstp += 2*dst_pitch;
  241. }
  242. srcp = mpi->planes[z];
  243. dstp = pmpi->planes[z];
  244. for (y=0; y<h; y++) {
  245. fast_memcpy(dstp, srcp, w);
  246. srcp += src_pitch;
  247. dstp += psrc_pitch;
  248. }
  249. }
  250. return vf_next_put_image(vf,dmpi, pts);
  251. }
  252. //===========================================================================//
  253. static int query_format(struct vf_instance *vf, unsigned int fmt){
  254. switch(fmt)
  255. {
  256. case IMGFMT_YV12:
  257. case IMGFMT_RGB:
  258. case IMGFMT_YUY2:
  259. return vf_next_query_format(vf, fmt);
  260. }
  261. return 0;
  262. }
  263. static int control(struct vf_instance *vf, int request, void* data){
  264. switch (request)
  265. {
  266. case VFCTRL_GET_DEINTERLACE:
  267. *(int*)data = vf->priv->do_deinterlace;
  268. return CONTROL_OK;
  269. case VFCTRL_SET_DEINTERLACE:
  270. vf->priv->do_deinterlace = *(int*)data;
  271. return CONTROL_OK;
  272. }
  273. return vf_next_control (vf, request, data);
  274. }
  275. static int vf_open(vf_instance_t *vf, char *args){
  276. vf->control=control;
  277. vf->config=config;
  278. vf->put_image=put_image;
  279. vf->query_format=query_format;
  280. vf->uninit=uninit;
  281. vf->priv=malloc(sizeof(struct vf_priv_s));
  282. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  283. vf->priv->frame = 0;
  284. vf->priv->map = 0;
  285. vf->priv->order = 0;
  286. vf->priv->thresh = 10;
  287. vf->priv->sharp = 0;
  288. vf->priv->twoway = 0;
  289. vf->priv->do_deinterlace=1;
  290. if (args)
  291. {
  292. sscanf(args, "%d:%d:%d:%d:%d",
  293. &vf->priv->thresh, &vf->priv->map,
  294. &vf->priv->order, &vf->priv->sharp,
  295. &vf->priv->twoway);
  296. }
  297. if (vf->priv->order > 1) vf->priv->order = 1;
  298. return 1;
  299. }
  300. const vf_info_t vf_info_kerndeint = {
  301. "Kernel Deinterlacer",
  302. "kerndeint",
  303. "Donald Graft",
  304. "",
  305. vf_open,
  306. NULL
  307. };
  308. //===========================================================================//