vf_dint.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <inttypes.h>
  22. #include "config.h"
  23. #include "mp_msg.h"
  24. #include "mp_image.h"
  25. #include "img_format.h"
  26. #include "vf.h"
  27. struct vf_priv_s {
  28. float sense; // first parameter
  29. float level; // second parameter
  30. unsigned int imgfmt;
  31. int diff;
  32. uint32_t max;
  33. // int dfr;
  34. // int rdfr;
  35. int was_dint;
  36. mp_image_t *pmpi; // previous mpi
  37. };
  38. #define MAXROWSIZE 1200
  39. static int config (struct vf_instance *vf,
  40. int width, int height, int d_width, int d_height,
  41. unsigned int flags, unsigned int outfmt)
  42. {
  43. int rowsize;
  44. vf->priv->pmpi = vf_get_image (vf->next, outfmt, MP_IMGTYPE_TEMP,
  45. 0, width, height);
  46. if (!(vf->priv->pmpi->flags & MP_IMGFLAG_PLANAR) &&
  47. outfmt != IMGFMT_RGB32 && outfmt != IMGFMT_BGR32 &&
  48. outfmt != IMGFMT_RGB24 && outfmt != IMGFMT_BGR24 &&
  49. outfmt != IMGFMT_RGB16 && outfmt != IMGFMT_BGR16)
  50. {
  51. mp_msg (MSGT_VFILTER, MSGL_WARN, "Drop-interlaced filter doesn't support this outfmt :(\n");
  52. return 0;
  53. }
  54. vf->priv->imgfmt = outfmt;
  55. // recalculate internal values
  56. rowsize = vf->priv->pmpi->width;
  57. if (rowsize > MAXROWSIZE) rowsize = MAXROWSIZE;
  58. vf->priv->max = vf->priv->level * vf->priv->pmpi->height * rowsize / 2;
  59. if (vf->priv->pmpi->flags & MP_IMGFLAG_PLANAR) // planar YUV
  60. vf->priv->diff = vf->priv->sense * 256;
  61. else
  62. vf->priv->diff = vf->priv->sense * (1 << (vf->priv->pmpi->bpp/3));
  63. if (vf->priv->diff < 0) vf->priv->diff = 0;
  64. if (!(vf->priv->pmpi->flags & MP_IMGFLAG_PLANAR) &&
  65. vf->priv->pmpi->bpp < 24 && vf->priv->diff > 31)
  66. vf->priv->diff = 31;
  67. mp_msg (MSGT_VFILTER, MSGL_INFO, "Drop-interlaced: %dx%d diff %d / level %u\n",
  68. vf->priv->pmpi->width, vf->priv->pmpi->height,
  69. vf->priv->diff, (unsigned int)vf->priv->max);
  70. // vf->priv->rdfr = vf->priv->dfr = 0;
  71. vf->priv->was_dint = 0;
  72. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  73. }
  74. static int put_image (struct vf_instance *vf, mp_image_t *mpi, double pts)
  75. {
  76. int8_t rrow0[MAXROWSIZE];
  77. int8_t rrow1[MAXROWSIZE];
  78. int8_t rrow2[MAXROWSIZE];
  79. int8_t *row0 = rrow0, *row1 = rrow1, *row2 = rrow2/*, *row3 = rrow3*/;
  80. int rowsize = mpi->width;
  81. uint32_t nok = 0, max = vf->priv->max;
  82. int diff = vf->priv->diff;
  83. int i, j;
  84. register int n1, n2;
  85. unsigned char *cur0, *prv0;
  86. register unsigned char *cur, *prv;
  87. if (rowsize > MAXROWSIZE) rowsize = MAXROWSIZE;
  88. // check if nothing to do
  89. if (mpi->imgfmt == vf->priv->imgfmt)
  90. {
  91. cur0 = mpi->planes[0] + mpi->stride[0];
  92. prv0 = mpi->planes[0];
  93. for (j = 1; j < mpi->height && nok <= max; j++)
  94. {
  95. cur = cur0;
  96. prv = prv0;
  97. // analyse row (row0)
  98. if (mpi->flags & MP_IMGFLAG_PLANAR) // planar YUV - check luminance
  99. for (i = 0; i < rowsize; i++)
  100. {
  101. if (cur[0] - prv[0] > diff)
  102. row0[i] = 1;
  103. else if (cur[0] - prv[0] < -diff)
  104. row0[i] = -1;
  105. else
  106. row0[i] = 0;
  107. cur++;
  108. prv++;
  109. // check if row0 is 1 but row1 is 0, and row2 is 1 or row2 is 0
  110. // but row3 is 1 so it's interlaced ptr (nok++)
  111. if (j > 2 && row0[i] > 0 && (row1[i] < 0 || (!row1[i] && row2[i] < 0)) &&
  112. (++nok) > max)
  113. break;
  114. }
  115. else if (mpi->bpp < 24) // RGB/BGR 16 - check all colors
  116. for (i = 0; i < rowsize; i++)
  117. {
  118. n1 = cur[0] + (cur[1]<<8);
  119. n2 = prv[0] + (prv[1]<<8);
  120. if ((n1&0x1f) - (n2&0x1f) > diff ||
  121. ((n1>>5)&0x3f) - ((n2>>5)&0x3f) > diff ||
  122. ((n1>>11)&0x1f) - ((n2>>11)&0x1f) > diff)
  123. row0[i] = 1;
  124. else if ((n1&0x1f) - (n2&0x1f) < -diff ||
  125. ((n1>>5)&0x3f) - ((n2>>5)&0x3f) < -diff ||
  126. ((n1>>11)&0x1f) - ((n2>>11)&0x1f) < -diff)
  127. row0[i] = -1;
  128. else
  129. row0[i] = 0;
  130. cur += 2;
  131. prv += 2;
  132. // check if row0 is 1 but row1 is 0, and row2 is 1 or row2 is 0
  133. // but row3 is 1 so it's interlaced ptr (nok++)
  134. if (j > 2 && row0[i] > 0 && (row1[i] < 0 || (!row1[i] && row2[i] < 0)) &&
  135. (++nok) > max)
  136. break;
  137. }
  138. else // RGB/BGR 24/32
  139. for (i = 0; i < rowsize; i++)
  140. {
  141. if (cur[0] - prv[0] > diff ||
  142. cur[1] - prv[1] > diff ||
  143. cur[2] - prv[2] > diff)
  144. row0[i] = 1;
  145. else if (prv[0] - cur[0] > diff ||
  146. prv[1] - cur[1] > diff ||
  147. prv[2] - cur[2] > diff)
  148. row0[i] = -1;
  149. else
  150. row0[i] = 0;
  151. cur += mpi->bpp/8;
  152. prv += mpi->bpp/8;
  153. // check if row0 is 1 but row1 is 0, and row2 is 1 or row2 is 0
  154. // but row3 is 1 so it's interlaced ptr (nok++)
  155. if (j > 2 && row0[i] > 0 && (row1[i] < 0 || (!row1[i] && row2[i] < 0)) &&
  156. (++nok) > max)
  157. break;
  158. }
  159. cur0 += mpi->stride[0];
  160. prv0 += mpi->stride[0];
  161. // rotate rows
  162. cur = row2;
  163. row2 = row1;
  164. row1 = row0;
  165. row0 = cur;
  166. }
  167. }
  168. // check if number of interlaced is above of max
  169. if (nok > max)
  170. {
  171. // vf->priv->dfr++;
  172. if (vf->priv->was_dint < 1) // can skip at most one frame!
  173. {
  174. vf->priv->was_dint++;
  175. // vf->priv->rdfr++;
  176. // mp_msg (MSGT_VFILTER, MSGL_INFO, "DI:%d/%d ", vf->priv->rdfr, vf->priv->dfr);
  177. return 0;
  178. }
  179. }
  180. vf->priv->was_dint = 0;
  181. // mp_msg (MSGT_VFILTER, MSGL_INFO, "DI:%d/%d ", vf->priv->rdfr, vf->priv->dfr);
  182. return vf_next_put_image (vf, mpi, pts);
  183. }
  184. static int vf_open(vf_instance_t *vf, char *args){
  185. vf->config = config;
  186. vf->put_image = put_image;
  187. // vf->default_reqs=VFCAP_ACCEPT_STRIDE;
  188. vf->priv = malloc (sizeof(struct vf_priv_s));
  189. vf->priv->sense = 0.1;
  190. vf->priv->level = 0.15;
  191. vf->priv->pmpi = NULL;
  192. if (args)
  193. sscanf (args, "%f:%f", &vf->priv->sense, &vf->priv->level);
  194. return 1;
  195. }
  196. const vf_info_t vf_info_dint = {
  197. "drop interlaced frames",
  198. "dint",
  199. "A.G.",
  200. "",
  201. vf_open,
  202. NULL
  203. };