00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022
00023 #include "config.h"
00024 #include "mp_msg.h"
00025 #include "cpudetect.h"
00026
00027 #include "img_format.h"
00028 #include "mp_image.h"
00029 #include "vf.h"
00030
00031 #include "libvo/fastmemcpy.h"
00032
00033
00034 struct vf_priv_s {
00035 int hi, lo;
00036 float frac;
00037 int max, last, cnt;
00038 };
00039
00040 #if HAVE_MMX && HAVE_EBX_AVAILABLE
00041 static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
00042 {
00043 volatile short out[4];
00044 __asm__ (
00045 "movl $8, %%ecx \n\t"
00046 "pxor %%mm4, %%mm4 \n\t"
00047 "pxor %%mm7, %%mm7 \n\t"
00048
00049 ASMALIGN(4)
00050 "1: \n\t"
00051
00052 "movq (%%"REG_S"), %%mm0 \n\t"
00053 "movq (%%"REG_S"), %%mm2 \n\t"
00054 "add %%"REG_a", %%"REG_S" \n\t"
00055 "movq (%%"REG_D"), %%mm1 \n\t"
00056 "add %%"REG_b", %%"REG_D" \n\t"
00057 "psubusb %%mm1, %%mm2 \n\t"
00058 "psubusb %%mm0, %%mm1 \n\t"
00059 "movq %%mm2, %%mm0 \n\t"
00060 "movq %%mm1, %%mm3 \n\t"
00061 "punpcklbw %%mm7, %%mm0 \n\t"
00062 "punpcklbw %%mm7, %%mm1 \n\t"
00063 "punpckhbw %%mm7, %%mm2 \n\t"
00064 "punpckhbw %%mm7, %%mm3 \n\t"
00065 "paddw %%mm0, %%mm4 \n\t"
00066 "paddw %%mm1, %%mm4 \n\t"
00067 "paddw %%mm2, %%mm4 \n\t"
00068 "paddw %%mm3, %%mm4 \n\t"
00069
00070 "decl %%ecx \n\t"
00071 "jnz 1b \n\t"
00072 "movq %%mm4, (%%"REG_d") \n\t"
00073 "emms \n\t"
00074 :
00075 : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
00076 : "%ecx", "memory"
00077 );
00078 return out[0]+out[1]+out[2]+out[3];
00079 }
00080 #endif
00081
00082 static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
00083 {
00084 int x, y, d=0;
00085 for (y = 8; y; y--) {
00086 for (x = 8; x; x--) {
00087 d += abs(new[x] - old[x]);
00088 }
00089 new += ns;
00090 old += os;
00091 }
00092 return d;
00093 }
00094
00095 static int (*diff)(unsigned char *, unsigned char *, int, int);
00096
00097 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)
00098 {
00099 int x, y;
00100 int d, c=0;
00101 int t = (w/16)*(h/16)*frac;
00102 for (y = 0; y < h-7; y += 4) {
00103 for (x = 8; x < w-7; x += 4) {
00104 d = diff(old+x+y*os, new+x+y*ns, os, ns);
00105 if (d > hi) return 0;
00106 if (d > lo) {
00107 c++;
00108 if (c > t) return 0;
00109 }
00110 }
00111 }
00112 return 1;
00113 }
00114
00115 static int diff_to_drop(int hi, int lo, float frac, mp_image_t *old, mp_image_t *new)
00116 {
00117 if (new->flags & MP_IMGFLAG_PLANAR) {
00118 return diff_to_drop_plane(hi,lo,frac, old->planes[0], new->planes[0],
00119 new->w, new->h, old->stride[0], new->stride[0])
00120 && diff_to_drop_plane(hi,lo,frac, old->planes[1], new->planes[1],
00121 new->chroma_width, new->chroma_height,
00122 old->stride[1], new->stride[1])
00123 && diff_to_drop_plane(hi,lo,frac, old->planes[2], new->planes[2],
00124 new->chroma_width, new->chroma_height,
00125 old->stride[2], new->stride[2]);
00126 }
00127 return diff_to_drop_plane(hi,lo,frac, old->planes[0], new->planes[0],
00128 new->w*(new->bpp/8), new->h, old->stride[0], new->stride[0]);
00129 }
00130
00131 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
00132 {
00133 mp_image_t *dmpi;
00134
00135 dmpi = vf_get_image(vf->next, mpi->imgfmt,
00136 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
00137 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
00138 mpi->width, mpi->height);
00139 dmpi->qscale = mpi->qscale;
00140 dmpi->qstride = mpi->qstride;
00141 dmpi->qscale_type = mpi->qscale_type;
00142
00143 if (diff_to_drop(vf->priv->hi, vf->priv->lo, vf->priv->frac, dmpi, mpi)) {
00144 if (vf->priv->max == 0)
00145 return 0;
00146 else if ((vf->priv->max > 0) && (vf->priv->cnt++ < vf->priv->max))
00147 return 0;
00148 else if ((vf->priv->max < 0) && (vf->priv->last+1 >= -vf->priv->max))
00149 return vf->priv->last=0;
00150 }
00151 vf->priv->last++;
00152 vf->priv->cnt=0;
00153
00154 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
00155 dmpi->stride[0], mpi->stride[0]);
00156 if (mpi->flags & MP_IMGFLAG_PLANAR) {
00157 memcpy_pic(dmpi->planes[1], mpi->planes[1],
00158 mpi->chroma_width, mpi->chroma_height,
00159 dmpi->stride[1], mpi->stride[1]);
00160 memcpy_pic(dmpi->planes[2], mpi->planes[2],
00161 mpi->chroma_width, mpi->chroma_height,
00162 dmpi->stride[2], mpi->stride[2]);
00163 }
00164 return vf_next_put_image(vf, dmpi, pts);
00165 }
00166
00167 static void uninit(struct vf_instance *vf)
00168 {
00169 free(vf->priv);
00170 }
00171
00172 static int vf_open(vf_instance_t *vf, char *args)
00173 {
00174 struct vf_priv_s *p;
00175 vf->put_image = put_image;
00176 vf->uninit = uninit;
00177 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
00178 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
00179 p->max = 0;
00180 p->hi = 64*12;
00181 p->lo = 64*5;
00182 p->frac = 0.33;
00183 if (args) sscanf(args, "%d:%d:%d:%f", &p->max, &p->hi, &p->lo, &p->frac);
00184 diff = diff_C;
00185 #if HAVE_MMX && HAVE_EBX_AVAILABLE
00186 if(gCpuCaps.hasMMX) diff = diff_MMX;
00187 #endif
00188 return 1;
00189 }
00190
00191 const vf_info_t vf_info_decimate = {
00192 "near-duplicate frame remover",
00193 "decimate",
00194 "Rich Felker",
00195 "",
00196 vf_open,
00197 NULL
00198 };