2 * Copyright (C) 2009 Loren Merritt <lorenm@u.washignton.edu>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Debanding algorithm (from gradfun2db by prunedtree):
24 * Foreach pixel, if it's within threshold of the blurred value, make it closer.
25 * So now we have a smoothed and higher bitdepth version of all the shallow
26 * gradients, while leaving detailed areas untouched.
27 * Dither it back to 8bit.
35 #include <libavutil/common.h>
38 #include "cpudetect.h"
39 #include "img_format.h"
42 #include "libvo/fastmemcpy.h"
43 #include "ffmpeg_files/x86_cpu.h"
49 void (*filter_line
)(uint8_t *dst
, uint8_t *src
, uint16_t *dc
,
50 int width
, int thresh
, const uint16_t *dithers
);
51 void (*blur_line
)(uint16_t *dc
, uint16_t *buf
, uint16_t *buf1
,
52 uint8_t *src
, int sstride
, int width
);
55 static const uint16_t __attribute__((aligned(16))) pw_7f
[8] = {127,127,127,127,127,127,127,127};
56 static const uint16_t __attribute__((aligned(16))) pw_ff
[8] = {255,255,255,255,255,255,255,255};
57 static const uint16_t __attribute__((aligned(16))) dither
[8][8] = {
58 { 0, 96, 24,120, 6,102, 30,126 },
59 { 64, 32, 88, 56, 70, 38, 94, 62 },
60 { 16,112, 8,104, 22,118, 14,110 },
61 { 80, 48, 72, 40, 86, 54, 78, 46 },
62 { 4,100, 28,124, 2, 98, 26,122 },
63 { 68, 36, 92, 60, 66, 34, 90, 58 },
64 { 20,116, 12,108, 18,114, 10,106 },
65 { 84, 52, 76, 44, 82, 50, 74, 42 },
68 static void filter_line_c(uint8_t *dst
, uint8_t *src
, uint16_t *dc
,
69 int width
, int thresh
, const uint16_t *dithers
)
72 for (x
=0; x
<width
; x
++, dc
+=x
&1) {
74 int delta
= dc
[0] - pix
;
75 int m
= abs(delta
) * thresh
>> 16;
78 pix
+= m
+ dithers
[x
&7];
79 dst
[x
] = av_clip_uint8(pix
>>7);
83 static void blur_line_c(uint16_t *dc
, uint16_t *buf
, uint16_t *buf1
,
84 uint8_t *src
, int sstride
, int width
)
87 for (x
=0; x
<width
; x
++) {
88 v
= buf1
[x
] + src
[2*x
] + src
[2*x
+1] + src
[2*x
+sstride
] + src
[2*x
+1+sstride
];
96 static void filter_line_mmx2(uint8_t *dst
, uint8_t *src
, uint16_t *dc
,
97 int width
, int thresh
, const uint16_t *dithers
)
102 filter_line_c(dst
+x
, src
+x
, dc
+x
/2, width
-x
, thresh
, dithers
);
108 "pxor %%mm7, %%mm7 \n"
109 "pshufw $0, %%mm5, %%mm5 \n"
113 "movd (%2,%0), %%mm0 \n"
114 "movd (%3,%0), %%mm1 \n"
115 "punpcklbw %%mm7, %%mm0 \n"
116 "punpcklwd %%mm1, %%mm1 \n"
118 "pxor %%mm2, %%mm2 \n"
119 "psubw %%mm0, %%mm1 \n" // delta = dc - pix
120 "psubw %%mm1, %%mm2 \n"
121 "pmaxsw %%mm1, %%mm2 \n"
122 "pmulhuw %%mm5, %%mm2 \n" // m = abs(delta) * thresh >> 16
123 "psubw %%mm6, %%mm2 \n"
124 "pminsw %%mm7, %%mm2 \n" // m = -max(0, 127-m)
125 "pmullw %%mm2, %%mm2 \n"
126 "paddw %%mm4, %%mm0 \n" // pix += dither
127 "pmulhw %%mm2, %%mm1 \n"
128 "psllw $2, %%mm1 \n" // m = m*m*delta >> 14
129 "paddw %%mm1, %%mm0 \n" // pix += m
131 "packuswb %%mm0, %%mm0 \n"
132 "movd %%mm0, (%1,%0) \n" // dst = clip(pix>>7)
137 :"r"(dst
+width
), "r"(src
+width
), "r"(dc
+width
/2),
138 "rm"(thresh
), "m"(*dithers
), "m"(*pw_7f
)
145 static void filter_line_ssse3(uint8_t *dst
, uint8_t *src
, uint16_t *dc
,
146 int width
, int thresh
, const uint16_t *dithers
)
150 // could be 10% faster if I somehow eliminated this
152 filter_line_c(dst
+x
, src
+x
, dc
+x
/2, width
-x
, thresh
, dithers
);
158 "pxor %%xmm7, %%xmm7 \n"
159 "pshuflw $0,%%xmm5, %%xmm5 \n"
160 "movdqa %6, %%xmm6 \n"
161 "punpcklqdq %%xmm5, %%xmm5 \n"
162 "movdqa %5, %%xmm4 \n"
164 "movq (%2,%0), %%xmm0 \n"
165 "movq (%3,%0), %%xmm1 \n"
166 "punpcklbw %%xmm7, %%xmm0 \n"
167 "punpcklwd %%xmm1, %%xmm1 \n"
168 "psllw $7, %%xmm0 \n"
169 "psubw %%xmm0, %%xmm1 \n" // delta = dc - pix
170 "pabsw %%xmm1, %%xmm2 \n"
171 "pmulhuw %%xmm5, %%xmm2 \n" // m = abs(delta) * thresh >> 16
172 "psubw %%xmm6, %%xmm2 \n"
173 "pminsw %%xmm7, %%xmm2 \n" // m = -max(0, 127-m)
174 "pmullw %%xmm2, %%xmm2 \n"
175 "psllw $1, %%xmm2 \n"
176 "paddw %%xmm4, %%xmm0 \n" // pix += dither
177 "pmulhrsw %%xmm2, %%xmm1 \n" // m = m*m*delta >> 14
178 "paddw %%xmm1, %%xmm0 \n" // pix += m
179 "psraw $7, %%xmm0 \n"
180 "packuswb %%xmm0, %%xmm0 \n"
181 "movq %%xmm0, (%1,%0) \n" // dst = clip(pix>>7)
185 :"r"(dst
+width
), "r"(src
+width
), "r"(dc
+width
/2),
186 "rm"(thresh
), "m"(*dithers
), "m"(*pw_7f
)
192 #if HAVE_SSE2 && HAVE_6REGS
194 intptr_t x = -2*width;\
196 "movdqa %6, %%xmm7 \n"\
198 load" (%4,%0), %%xmm0 \n"\
199 load" (%5,%0), %%xmm1 \n"\
200 "movdqa %%xmm0, %%xmm2 \n"\
201 "movdqa %%xmm1, %%xmm3 \n"\
202 "psrlw $8, %%xmm0 \n"\
203 "psrlw $8, %%xmm1 \n"\
204 "pand %%xmm7, %%xmm2 \n"\
205 "pand %%xmm7, %%xmm3 \n"\
206 "paddw %%xmm1, %%xmm0 \n"\
207 "paddw %%xmm3, %%xmm2 \n"\
208 "paddw %%xmm2, %%xmm0 \n"\
209 "paddw (%2,%0), %%xmm0 \n"\
210 "movdqa (%1,%0), %%xmm1 \n"\
211 "movdqa %%xmm0, (%1,%0) \n"\
212 "psubw %%xmm1, %%xmm0 \n"\
213 "movdqa %%xmm0, (%3,%0) \n"\
221 "r"(src+width*2+sstride),\
226 static void blur_line_sse2(uint16_t *dc
, uint16_t *buf
, uint16_t *buf1
,
227 uint8_t *src
, int sstride
, int width
)
229 if (((intptr_t)src
|sstride
)&15) {
235 #endif // HAVE_6REGS && HAVE_SSE2
237 static void filter(struct vf_priv_s
*ctx
, uint8_t *dst
, uint8_t *src
,
238 int width
, int height
, int dstride
, int sstride
, int r
)
240 int bstride
= ((width
+15)&~15)/2;
242 uint32_t dc_factor
= (1<<21)/(r
*r
);
243 uint16_t *dc
= ctx
->buf
+16;
244 uint16_t *buf
= ctx
->buf
+bstride
+32;
245 int thresh
= ctx
->thresh
;
247 memset(dc
, 0, (bstride
+16)*sizeof(*buf
));
249 ctx
->blur_line(dc
, buf
+y
*bstride
, buf
+(y
-1)*bstride
, src
+2*y
*sstride
, sstride
, width
/2);
252 int mod
= ((y
+r
)/2)%r
;
253 uint16_t *buf0
= buf
+mod
*bstride
;
254 uint16_t *buf1
= buf
+(mod
?mod
-1:r
-1)*bstride
;
256 ctx
->blur_line(dc
, buf0
, buf1
, src
+(y
+r
)*sstride
, sstride
, width
/2);
257 for (x
=v
=0; x
<r
; x
++)
259 for (; x
<width
/2; x
++) {
260 v
+= dc
[x
] - dc
[x
-r
];
261 dc
[x
-r
] = v
* dc_factor
>> 16;
263 for (; x
<(width
+r
+1)/2; x
++)
264 dc
[x
-r
] = v
* dc_factor
>> 16;
265 for (x
=-r
/2; x
<0; x
++)
270 ctx
->filter_line(dst
+y
*dstride
, src
+y
*sstride
, dc
-r
/2, width
, thresh
, dither
[y
&7]);
272 ctx
->filter_line(dst
+y
*dstride
, src
+y
*sstride
, dc
-r
/2, width
, thresh
, dither
[y
&7]);
273 if (++y
>= height
) break;
274 ctx
->filter_line(dst
+y
*dstride
, src
+y
*sstride
, dc
-r
/2, width
, thresh
, dither
[y
&7]);
275 if (++y
>= height
) break;
279 static void get_image(struct vf_instance
*vf
, mp_image_t
*mpi
)
281 if (mpi
->flags
&MP_IMGFLAG_PRESERVE
) return; // don't change
282 // ok, we can do pp in-place:
283 vf
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
284 mpi
->type
, mpi
->flags
, mpi
->width
, mpi
->height
);
285 mpi
->planes
[0] = vf
->dmpi
->planes
[0];
286 mpi
->stride
[0] = vf
->dmpi
->stride
[0];
287 mpi
->width
= vf
->dmpi
->width
;
288 if (mpi
->flags
&MP_IMGFLAG_PLANAR
){
289 mpi
->planes
[1] = vf
->dmpi
->planes
[1];
290 mpi
->planes
[2] = vf
->dmpi
->planes
[2];
291 mpi
->stride
[1] = vf
->dmpi
->stride
[1];
292 mpi
->stride
[2] = vf
->dmpi
->stride
[2];
294 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
297 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
299 mp_image_t
*dmpi
= vf
->dmpi
;
302 if (!(mpi
->flags
&MP_IMGFLAG_DIRECT
)) {
303 // no DR, so get a new image. hope we'll get DR buffer:
304 dmpi
= vf_get_image(vf
->next
,mpi
->imgfmt
, MP_IMGTYPE_TEMP
,
305 MP_IMGFLAG_ACCEPT_STRIDE
|MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
308 vf_clone_mpi_attributes(dmpi
, mpi
);
310 for (p
=0; p
<mpi
->num_planes
; p
++) {
313 int r
= vf
->priv
->radius
;
315 w
>>= mpi
->chroma_x_shift
;
316 h
>>= mpi
->chroma_y_shift
;
317 r
= ((r
>>mpi
->chroma_x_shift
) + (r
>>mpi
->chroma_y_shift
)) / 2;
318 r
= av_clip((r
+1)&~1,4,32);
320 if (FFMIN(w
,h
) > 2*r
)
321 filter(vf
->priv
, dmpi
->planes
[p
], mpi
->planes
[p
], w
, h
,
322 dmpi
->stride
[p
], mpi
->stride
[p
], r
);
323 else if (dmpi
->planes
[p
] != mpi
->planes
[p
])
324 memcpy_pic(dmpi
->planes
[p
], mpi
->planes
[p
], w
, h
,
325 dmpi
->stride
[p
], mpi
->stride
[p
]);
328 return vf_next_put_image(vf
, dmpi
, pts
);
331 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
348 return vf_next_query_format(vf
,fmt
);
353 static int config(struct vf_instance
*vf
,
354 int width
, int height
, int d_width
, int d_height
,
355 unsigned int flags
, unsigned int outfmt
)
358 vf
->priv
->buf
= av_mallocz((((width
+15)&~15)*(vf
->priv
->radius
+1)/2+32)*sizeof(uint16_t));
359 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
362 static void uninit(struct vf_instance
*vf
)
364 if (!vf
->priv
) return;
365 av_free(vf
->priv
->buf
);
370 static int vf_open(vf_instance_t
*vf
, char *args
)
375 vf
->get_image
=get_image
;
376 vf
->put_image
=put_image
;
377 vf
->query_format
=query_format
;
380 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
381 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
383 if (args
) sscanf(args
, "%f:%d", &thresh
, &radius
);
384 vf
->priv
->thresh
= (1<<15)/av_clipf(thresh
,0.51,255);
385 vf
->priv
->radius
= av_clip((radius
+1)&~1,4,32);
387 vf
->priv
->blur_line
= blur_line_c
;
388 vf
->priv
->filter_line
= filter_line_c
;
389 #if HAVE_SSE2 && HAVE_6REGS
390 if (gCpuCaps
.hasSSE2
)
391 vf
->priv
->blur_line
= blur_line_sse2
;
394 if (gCpuCaps
.hasMMX2
)
395 vf
->priv
->filter_line
= filter_line_mmx2
;
398 if (gCpuCaps
.hasSSSE3
)
399 vf
->priv
->filter_line
= filter_line_ssse3
;
405 const vf_info_t vf_info_gradfun
= {