]> git.sesse.net Git - vlc/blob - modules/video_filter/gradfun.h
access_dvb/dtv: support for libdvbpsi >= 1.0.0
[vlc] / modules / video_filter / gradfun.h
1 /*
2  * Copyright (c) 2010 Nolan Lum <nol888@gmail.com>
3  * Copyright (c) 2009 Loren Merritt <lorenm@u.washignton.edu>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /*
23  * Debanding algorithm (from gradfun2db by prunedtree):
24  * Boxblur.
25  * Foreach pixel, if it's within threshold of the blurred value, make it closer.
26  * So now we have a smoothed and higher bitdepth version of all the shallow
27  * gradients, while leaving detailed areas untouched.
28  * Dither it back to 8bit.
29  */
30
31 struct vf_priv_s {
32     int thresh;
33     int radius;
34     uint16_t *buf;
35     void (*filter_line)(uint8_t *dst, uint8_t *src, uint16_t *dc,
36                         int width, int thresh, const uint16_t *dithers);
37     void (*blur_line)(uint16_t *dc, uint16_t *buf, uint16_t *buf1,
38                       uint8_t *src, int sstride, int width);
39 };
40
41 static const uint16_t __attribute__((aligned(16))) pw_7f[8] = {127,127,127,127,127,127,127,127};
42 static const uint16_t __attribute__((aligned(16))) pw_ff[8] = {255,255,255,255,255,255,255,255};
43 static const uint16_t __attribute__((aligned(16))) dither[8][8] = {
44     {  0, 96, 24,120,  6,102, 30,126 },
45     { 64, 32, 88, 56, 70, 38, 94, 62 },
46     { 16,112,  8,104, 22,118, 14,110 },
47     { 80, 48, 72, 40, 86, 54, 78, 46 },
48     {  4,100, 28,124,  2, 98, 26,122 },
49     { 68, 36, 92, 60, 66, 34, 90, 58 },
50     { 20,116, 12,108, 18,114, 10,106 },
51     { 84, 52, 76, 44, 82, 50, 74, 42 },
52 };
53
54 static void filter_line_c(uint8_t *dst, uint8_t *src, uint16_t *dc,
55                           int width, int thresh, const uint16_t *dithers)
56 {
57     int x;
58     for (x=0; x<width; x++, dc+=x&1) {
59         int pix = src[x]<<7;
60         int delta = dc[0] - pix;
61         int m = abs(delta) * thresh >> 16;
62         m = FFMAX(0, 127-m);
63         m = m*m*delta >> 14;
64         pix += m + dithers[x&7];
65         dst[x] = av_clip_uint8(pix>>7);
66     }
67 }
68
69 static void blur_line_c(uint16_t *dc, uint16_t *buf, uint16_t *buf1,
70                         uint8_t *src, int sstride, int width)
71 {
72     int x, v, old;
73     for (x=0; x<width; x++) {
74         v = buf1[x] + src[2*x] + src[2*x+1] + src[2*x+sstride] + src[2*x+1+sstride];
75         old = buf[x];
76         buf[x] = v;
77         dc[x] = v - old;
78     }
79 }
80
81 #if HAVE_MMX2
82 VLC_MMX
83 static void filter_line_mmx2(uint8_t *dst, uint8_t *src, uint16_t *dc,
84                              int width, int thresh, const uint16_t *dithers)
85 {
86     intptr_t x;
87     if (width&3) {
88         x = width&~3;
89         filter_line_c(dst+x, src+x, dc+x/2, width-x, thresh, dithers);
90         width = x;
91     }
92     x = -width;
93     __asm__ volatile(
94         "movd          %4, %%mm5 \n"
95         "pxor       %%mm7, %%mm7 \n"
96         "pshufw $0, %%mm5, %%mm5 \n"
97         "movq          %6, %%mm6 \n"
98         "movq          %5, %%mm4 \n"
99         "1: \n"
100         "movd     (%2,%0), %%mm0 \n"
101         "movd     (%3,%0), %%mm1 \n"
102         "punpcklbw  %%mm7, %%mm0 \n"
103         "punpcklwd  %%mm1, %%mm1 \n"
104         "psllw         $7, %%mm0 \n"
105         "pxor       %%mm2, %%mm2 \n"
106         "psubw      %%mm0, %%mm1 \n" // delta = dc - pix
107         "psubw      %%mm1, %%mm2 \n"
108         "pmaxsw     %%mm1, %%mm2 \n"
109         "pmulhuw    %%mm5, %%mm2 \n" // m = abs(delta) * thresh >> 16
110         "psubw      %%mm6, %%mm2 \n"
111         "pminsw     %%mm7, %%mm2 \n" // m = -max(0, 127-m)
112         "pmullw     %%mm2, %%mm2 \n"
113         "paddw      %%mm4, %%mm0 \n" // pix += dither
114         "pmulhw     %%mm2, %%mm1 \n"
115         "psllw         $2, %%mm1 \n" // m = m*m*delta >> 14
116         "paddw      %%mm1, %%mm0 \n" // pix += m
117         "psraw         $7, %%mm0 \n"
118         "packuswb   %%mm0, %%mm0 \n"
119         "movd       %%mm0, (%1,%0) \n" // dst = clip(pix>>7)
120         "add           $4, %0 \n"
121         "jl 1b \n"
122         "emms \n"
123         :"+r"(x)
124         :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
125          "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
126         :"mm0", "mm1", "mm2", "mm4", "mm5", "mm6", "memory"
127     );
128 }
129 #endif
130
131 #if HAVE_SSSE3
132 VLC_SSE
133 static void filter_line_ssse3(uint8_t *dst, uint8_t *src, uint16_t *dc,
134                               int width, int thresh, const uint16_t *dithers)
135 {
136     intptr_t x;
137     if (width&7) {
138         // could be 10% faster if I somehow eliminated this
139         x = width&~7;
140         filter_line_c(dst+x, src+x, dc+x/2, width-x, thresh, dithers);
141         width = x;
142     }
143     x = -width;
144     __asm__ volatile(
145         "movd           %4, %%xmm5 \n"
146         "pxor       %%xmm7, %%xmm7 \n"
147         "pshuflw $0,%%xmm5, %%xmm5 \n"
148         "movdqa         %6, %%xmm6 \n"
149         "punpcklqdq %%xmm5, %%xmm5 \n"
150         "movdqa         %5, %%xmm4 \n"
151         "1: \n"
152         "movq      (%2,%0), %%xmm0 \n"
153         "movq      (%3,%0), %%xmm1 \n"
154         "punpcklbw  %%xmm7, %%xmm0 \n"
155         "punpcklwd  %%xmm1, %%xmm1 \n"
156         "psllw          $7, %%xmm0 \n"
157         "psubw      %%xmm0, %%xmm1 \n" // delta = dc - pix
158         "pabsw      %%xmm1, %%xmm2 \n"
159         "pmulhuw    %%xmm5, %%xmm2 \n" // m = abs(delta) * thresh >> 16
160         "psubw      %%xmm6, %%xmm2 \n"
161         "pminsw     %%xmm7, %%xmm2 \n" // m = -max(0, 127-m)
162         "pmullw     %%xmm2, %%xmm2 \n"
163         "psllw          $1, %%xmm2 \n"
164         "paddw      %%xmm4, %%xmm0 \n" // pix += dither
165         "pmulhrsw   %%xmm2, %%xmm1 \n" // m = m*m*delta >> 14
166         "paddw      %%xmm1, %%xmm0 \n" // pix += m
167         "psraw          $7, %%xmm0 \n"
168         "packuswb   %%xmm0, %%xmm0 \n"
169         "movq       %%xmm0, (%1,%0) \n" // dst = clip(pix>>7)
170         "add            $8, %0 \n"
171         "jl 1b \n"
172         :"+&r"(x)
173         :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
174          "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
175         :"xmm0", "xmm1", "xmm2", "xmm4", "xmm5", "xmm6", "xmm7", "memory"
176     );
177 }
178 #endif // HAVE_SSSE3
179
180 #if HAVE_SSE2 && HAVE_6REGS
181 #define BLURV(load)\
182     intptr_t x = -2*width;\
183     __asm__ volatile(\
184         "movdqa %6, %%xmm7 \n"\
185         "1: \n"\
186         load"   (%4,%0), %%xmm0 \n"\
187         load"   (%5,%0), %%xmm1 \n"\
188         "movdqa  %%xmm0, %%xmm2 \n"\
189         "movdqa  %%xmm1, %%xmm3 \n"\
190         "psrlw       $8, %%xmm0 \n"\
191         "psrlw       $8, %%xmm1 \n"\
192         "pand    %%xmm7, %%xmm2 \n"\
193         "pand    %%xmm7, %%xmm3 \n"\
194         "paddw   %%xmm1, %%xmm0 \n"\
195         "paddw   %%xmm3, %%xmm2 \n"\
196         "paddw   %%xmm2, %%xmm0 \n"\
197         "paddw  (%2,%0), %%xmm0 \n"\
198         "movdqa (%1,%0), %%xmm1 \n"\
199         "movdqa  %%xmm0, (%1,%0) \n"\
200         "psubw   %%xmm1, %%xmm0 \n"\
201         "movdqa  %%xmm0, (%3,%0) \n"\
202         "add        $16, %0 \n"\
203         "jl 1b \n"\
204         :"+&r"(x)\
205         :"r"(buf+width),\
206          "r"(buf1+width),\
207          "r"(dc+width),\
208          "r"(src+width*2),\
209          "r"(src+width*2+sstride),\
210          "m"(*pw_ff)\
211         :"xmm0", "xmm1", "xmm2", "xmm3", "xmm7", "memory"\
212     );
213
214 VLC_SSE
215 static void blur_line_sse2(uint16_t *dc, uint16_t *buf, uint16_t *buf1,
216                            uint8_t *src, int sstride, int width)
217 {
218     if (((intptr_t)src|sstride)&15) {
219         BLURV("movdqu");
220     } else {
221         BLURV("movdqa");
222     }
223 }
224 #endif // HAVE_6REGS && HAVE_SSE2
225
226 static void filter_plane(struct vf_priv_s *ctx, uint8_t *dst, uint8_t *src,
227                          int width, int height, int dstride, int sstride, int r)
228 {
229     int bstride = ((width+15)&~15)/2;
230     int y;
231     uint32_t dc_factor = (1<<21)/(r*r);
232     uint16_t *dc = ctx->buf+16;
233     uint16_t *buf = ctx->buf+bstride+32;
234     int thresh = ctx->thresh;
235
236     memset(dc, 0, (bstride+16)*sizeof(*buf));
237     for (y=0; y<r; y++)
238         ctx->blur_line(dc, buf+y*bstride, buf+(y-1)*bstride, src+2*y*sstride, sstride, width/2);
239     for (;;) {
240         if (y < height-r) {
241             int mod = ((y+r)/2)%r;
242             uint16_t *buf0 = buf+mod*bstride;
243             uint16_t *buf1 = buf+(mod?mod-1:r-1)*bstride;
244             int x, v;
245             ctx->blur_line(dc, buf0, buf1, src+(y+r)*sstride, sstride, width/2);
246             for (x=v=0; x<r; x++)
247                 v += dc[x];
248             for (; x<width/2; x++) {
249                 v += dc[x] - dc[x-r];
250                 dc[x-r] = v * dc_factor >> 16;
251             }
252             for (; x<(width+r+1)/2; x++)
253                 dc[x-r] = v * dc_factor >> 16;
254             for (x=-r/2; x<0; x++)
255                 dc[x] = dc[0];
256         }
257         if (y == r) {
258             for (y=0; y<r; y++)
259                 ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
260         }
261         ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
262         if (++y >= height) break;
263         ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
264         if (++y >= height) break;
265     }
266 }
267