vf_decimate.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "config.h"
  22. #include "mp_msg.h"
  23. #include "cpudetect.h"
  24. #include "img_format.h"
  25. #include "mp_image.h"
  26. #include "vf.h"
  27. #include "libvo/fastmemcpy.h"
  28. struct vf_priv_s {
  29. int hi, lo;
  30. float frac;
  31. int max, last, cnt;
  32. };
  33. #if HAVE_MMX && HAVE_EBX_AVAILABLE
  34. static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
  35. {
  36. volatile short out[4];
  37. __asm__ (
  38. "movl $8, %%ecx \n\t"
  39. "pxor %%mm4, %%mm4 \n\t"
  40. "pxor %%mm7, %%mm7 \n\t"
  41. ASMALIGN(4)
  42. "1: \n\t"
  43. "movq (%%"REG_S"), %%mm0 \n\t"
  44. "movq (%%"REG_S"), %%mm2 \n\t"
  45. "add %%"REG_a", %%"REG_S" \n\t"
  46. "movq (%%"REG_D"), %%mm1 \n\t"
  47. "add %%"REG_b", %%"REG_D" \n\t"
  48. "psubusb %%mm1, %%mm2 \n\t"
  49. "psubusb %%mm0, %%mm1 \n\t"
  50. "movq %%mm2, %%mm0 \n\t"
  51. "movq %%mm1, %%mm3 \n\t"
  52. "punpcklbw %%mm7, %%mm0 \n\t"
  53. "punpcklbw %%mm7, %%mm1 \n\t"
  54. "punpckhbw %%mm7, %%mm2 \n\t"
  55. "punpckhbw %%mm7, %%mm3 \n\t"
  56. "paddw %%mm0, %%mm4 \n\t"
  57. "paddw %%mm1, %%mm4 \n\t"
  58. "paddw %%mm2, %%mm4 \n\t"
  59. "paddw %%mm3, %%mm4 \n\t"
  60. "decl %%ecx \n\t"
  61. "jnz 1b \n\t"
  62. "movq %%mm4, (%%"REG_d") \n\t"
  63. "emms \n\t"
  64. :
  65. : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
  66. : "%ecx", "memory"
  67. );
  68. return out[0]+out[1]+out[2]+out[3];
  69. }
  70. #endif
  71. static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
  72. {
  73. int x, y, d=0;
  74. for (y = 8; y; y--) {
  75. for (x = 8; x; x--) {
  76. d += abs(new[x] - old[x]);
  77. }
  78. new += ns;
  79. old += os;
  80. }
  81. return d;
  82. }
  83. static int (*diff)(unsigned char *, unsigned char *, int, int);
  84. static int diff_to_drop_plane(int hi, int lo, float frac, unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
  85. {
  86. int x, y;
  87. int d, c=0;
  88. int t = (w/16)*(h/16)*frac;
  89. for (y = 0; y < h-7; y += 4) {
  90. for (x = 8; x < w-7; x += 4) {
  91. d = diff(old+x+y*os, new+x+y*ns, os, ns);
  92. if (d > hi) return 0;
  93. if (d > lo) {
  94. c++;
  95. if (c > t) return 0;
  96. }
  97. }
  98. }
  99. return 1;
  100. }
  101. static int diff_to_drop(int hi, int lo, float frac, mp_image_t *old, mp_image_t *new)
  102. {
  103. if (new->flags & MP_IMGFLAG_PLANAR) {
  104. return diff_to_drop_plane(hi,lo,frac, old->planes[0], new->planes[0],
  105. new->w, new->h, old->stride[0], new->stride[0])
  106. && diff_to_drop_plane(hi,lo,frac, old->planes[1], new->planes[1],
  107. new->chroma_width, new->chroma_height,
  108. old->stride[1], new->stride[1])
  109. && diff_to_drop_plane(hi,lo,frac, old->planes[2], new->planes[2],
  110. new->chroma_width, new->chroma_height,
  111. old->stride[2], new->stride[2]);
  112. }
  113. return diff_to_drop_plane(hi,lo,frac, old->planes[0], new->planes[0],
  114. new->w*(new->bpp/8), new->h, old->stride[0], new->stride[0]);
  115. }
  116. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  117. {
  118. mp_image_t *dmpi;
  119. dmpi = vf_get_image(vf->next, mpi->imgfmt,
  120. MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
  121. MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
  122. mpi->width, mpi->height);
  123. dmpi->qscale = mpi->qscale;
  124. dmpi->qstride = mpi->qstride;
  125. dmpi->qscale_type = mpi->qscale_type;
  126. if (diff_to_drop(vf->priv->hi, vf->priv->lo, vf->priv->frac, dmpi, mpi)) {
  127. if (vf->priv->max == 0)
  128. return 0;
  129. else if ((vf->priv->max > 0) && (vf->priv->cnt++ < vf->priv->max))
  130. return 0;
  131. else if ((vf->priv->max < 0) && (vf->priv->last+1 >= -vf->priv->max))
  132. return vf->priv->last=0;
  133. }
  134. vf->priv->last++;
  135. vf->priv->cnt=0;
  136. memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
  137. dmpi->stride[0], mpi->stride[0]);
  138. if (mpi->flags & MP_IMGFLAG_PLANAR) {
  139. memcpy_pic(dmpi->planes[1], mpi->planes[1],
  140. mpi->chroma_width, mpi->chroma_height,
  141. dmpi->stride[1], mpi->stride[1]);
  142. memcpy_pic(dmpi->planes[2], mpi->planes[2],
  143. mpi->chroma_width, mpi->chroma_height,
  144. dmpi->stride[2], mpi->stride[2]);
  145. }
  146. return vf_next_put_image(vf, dmpi, pts);
  147. }
  148. static void uninit(struct vf_instance *vf)
  149. {
  150. free(vf->priv);
  151. }
  152. static int vf_open(vf_instance_t *vf, char *args)
  153. {
  154. struct vf_priv_s *p;
  155. vf->put_image = put_image;
  156. vf->uninit = uninit;
  157. vf->default_reqs = VFCAP_ACCEPT_STRIDE;
  158. vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
  159. p->max = 0;
  160. p->hi = 64*12;
  161. p->lo = 64*5;
  162. p->frac = 0.33;
  163. if (args) sscanf(args, "%d:%d:%d:%f", &p->max, &p->hi, &p->lo, &p->frac);
  164. diff = diff_C;
  165. #if HAVE_MMX && HAVE_EBX_AVAILABLE
  166. if(gCpuCaps.hasMMX) diff = diff_MMX;
  167. #endif
  168. return 1;
  169. }
  170. const vf_info_t vf_info_decimate = {
  171. "near-duplicate frame remover",
  172. "decimate",
  173. "Rich Felker",
  174. "",
  175. vf_open,
  176. NULL
  177. };