typo fixes
[mplayer/greg.git] / libmpcodecs / vf_decimate.c
blob03c05a4af5257659d847df3f8b4d50ae0f44a65c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "config.h"
6 #include "mp_msg.h"
7 #include "cpudetect.h"
8 #include "asmalign.h"
10 #include "img_format.h"
11 #include "mp_image.h"
12 #include "vf.h"
14 #include "libvo/fastmemcpy.h"
17 struct vf_priv_s {
18 int hi, lo;
19 float frac;
20 int max, last, cnt;
23 #ifdef HAVE_MMX
24 static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
26 volatile short out[4];
27 asm (
28 "movl $8, %%ecx \n\t"
29 "pxor %%mm4, %%mm4 \n\t"
30 "pxor %%mm7, %%mm7 \n\t"
32 ASMALIGN16
33 "1: \n\t"
35 "movq (%%"REG_S"), %%mm0 \n\t"
36 "movq (%%"REG_S"), %%mm2 \n\t"
37 "add %%"REG_a", %%"REG_S" \n\t"
38 "movq (%%"REG_D"), %%mm1 \n\t"
39 "add %%"REG_b", %%"REG_D" \n\t"
40 "psubusb %%mm1, %%mm2 \n\t"
41 "psubusb %%mm0, %%mm1 \n\t"
42 "movq %%mm2, %%mm0 \n\t"
43 "movq %%mm1, %%mm3 \n\t"
44 "punpcklbw %%mm7, %%mm0 \n\t"
45 "punpcklbw %%mm7, %%mm1 \n\t"
46 "punpckhbw %%mm7, %%mm2 \n\t"
47 "punpckhbw %%mm7, %%mm3 \n\t"
48 "paddw %%mm0, %%mm4 \n\t"
49 "paddw %%mm1, %%mm4 \n\t"
50 "paddw %%mm2, %%mm4 \n\t"
51 "paddw %%mm3, %%mm4 \n\t"
53 "decl %%ecx \n\t"
54 "jnz 1b \n\t"
55 "movq %%mm4, (%%"REG_d") \n\t"
56 "emms \n\t"
58 : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
59 : "memory"
61 return out[0]+out[1]+out[2]+out[3];
63 #endif
65 static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
67 int x, y, d=0;
68 for (y = 8; y; y--) {
69 for (x = 8; x; x--) {
70 d += abs(new[x] - old[x]);
72 new += ns;
73 old += os;
75 return d;
78 static int (*diff)(unsigned char *, unsigned char *, int, int);
80 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)
82 int x, y;
83 int d, c=0;
84 int t = (w/16)*(h/16)*frac;
85 for (y = 0; y < h-7; y += 4) {
86 for (x = 8; x < w-7; x += 4) {
87 d = diff(old+x+y*os, new+x+y*ns, os, ns);
88 if (d > hi) return 0;
89 if (d > lo) {
90 c++;
91 if (c > t) return 0;
95 return 1;
98 static int diff_to_drop(int hi, int lo, float frac, mp_image_t *old, mp_image_t *new)
100 if (new->flags & MP_IMGFLAG_PLANAR) {
101 return diff_to_drop_plane(hi,lo,frac, old->planes[0], new->planes[0],
102 new->w, new->h, old->stride[0], new->stride[0])
103 && diff_to_drop_plane(hi,lo,frac, old->planes[1], new->planes[1],
104 new->chroma_width, new->chroma_height,
105 old->stride[1], new->stride[1])
106 && diff_to_drop_plane(hi,lo,frac, old->planes[2], new->planes[2],
107 new->chroma_width, new->chroma_height,
108 old->stride[2], new->stride[2]);
110 return diff_to_drop_plane(hi,lo,frac, old->planes[0], new->planes[0],
111 new->w*(new->bpp/8), new->h, old->stride[0], new->stride[0]);
114 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
116 mp_image_t *dmpi;
118 dmpi = vf_get_image(vf->next, mpi->imgfmt,
119 MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
120 MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
121 mpi->width, mpi->height);
122 dmpi->qscale = mpi->qscale;
123 dmpi->qstride = mpi->qstride;
124 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;
134 vf->priv->last++;
135 vf->priv->cnt=0;
137 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
138 dmpi->stride[0], mpi->stride[0]);
139 if (mpi->flags & MP_IMGFLAG_PLANAR) {
140 memcpy_pic(dmpi->planes[1], mpi->planes[1],
141 mpi->chroma_width, mpi->chroma_height,
142 dmpi->stride[1], mpi->stride[1]);
143 memcpy_pic(dmpi->planes[2], mpi->planes[2],
144 mpi->chroma_width, mpi->chroma_height,
145 dmpi->stride[2], mpi->stride[2]);
147 return vf_next_put_image(vf, dmpi, pts);
150 static void uninit(struct vf_instance_s* vf)
152 free(vf->priv);
155 static int open(vf_instance_t *vf, char* args)
157 struct vf_priv_s *p;
158 vf->put_image = put_image;
159 vf->uninit = uninit;
160 vf->default_reqs = VFCAP_ACCEPT_STRIDE;
161 vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
162 p->max = 0;
163 p->hi = 64*12;
164 p->lo = 64*5;
165 p->frac = 0.33;
166 if (args) sscanf(args, "%d:%d:%d:%f", &p->max, &p->hi, &p->lo, &p->frac);
167 diff = diff_C;
168 #ifdef HAVE_MMX
169 if(gCpuCaps.hasMMX) diff = diff_MMX;
170 #endif
171 return 1;
174 vf_info_t vf_info_decimate = {
175 "near-duplicate frame remover",
176 "decimate",
177 "Rich Felker",
179 open,
180 NULL