]> git.sesse.net Git - vlc/blob - modules/video_filter/gradfun.h
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / video_filter / gradfun.h
1 /*
2  * Copyright (C) 2009 Loren Merritt <lorenm@u.washignton.edu>
3  *
4  * This file is part of MPlayer.
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 /*
22  * Debanding algorithm (from gradfun2db by prunedtree):
23  * Boxblur.
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.
28  */
29
30 struct vf_priv_s {
31     int thresh;
32     int radius;
33     uint16_t *buf;
34     void (*filter_line)(uint8_t *dst, uint8_t *src, uint16_t *dc,
35                         int width, int thresh, const uint16_t *dithers);
36     void (*blur_line)(uint16_t *dc, uint16_t *buf, uint16_t *buf1,
37                       uint8_t *src, int sstride, int width);
38 };
39
40 static const uint16_t __attribute__((aligned(16))) pw_7f[8] = {127,127,127,127,127,127,127,127};
41 static const uint16_t __attribute__((aligned(16))) pw_ff[8] = {255,255,255,255,255,255,255,255};
42 static const uint16_t __attribute__((aligned(16))) dither[8][8] = {
43     {  0, 96, 24,120,  6,102, 30,126 },
44     { 64, 32, 88, 56, 70, 38, 94, 62 },
45     { 16,112,  8,104, 22,118, 14,110 },
46     { 80, 48, 72, 40, 86, 54, 78, 46 },
47     {  4,100, 28,124,  2, 98, 26,122 },
48     { 68, 36, 92, 60, 66, 34, 90, 58 },
49     { 20,116, 12,108, 18,114, 10,106 },
50     { 84, 52, 76, 44, 82, 50, 74, 42 },
51 };
52
53 static void filter_line_c(uint8_t *dst, uint8_t *src, uint16_t *dc,
54                           int width, int thresh, const uint16_t *dithers)
55 {
56     int x;
57     for (x=0; x<width; x++, dc+=x&1) {
58         int pix = src[x]<<7;
59         int delta = dc[0] - pix;
60         int m = abs(delta) * thresh >> 16;
61         m = FFMAX(0, 127-m);
62         m = m*m*delta >> 14;
63         pix += m + dithers[x&7];
64         dst[x] = av_clip_uint8(pix>>7);
65     }
66 }
67
68 static void blur_line_c(uint16_t *dc, uint16_t *buf, uint16_t *buf1,
69                         uint8_t *src, int sstride, int width)
70 {
71     int x, v, old;
72     for (x=0; x<width; x++) {
73         v = buf1[x] + src[2*x] + src[2*x+1] + src[2*x+sstride] + src[2*x+1+sstride];
74         old = buf[x];
75         buf[x] = v;
76         dc[x] = v - old;
77     }
78 }
79
80 #if HAVE_MMX2
81 static void filter_line_mmx2(uint8_t *dst, uint8_t *src, uint16_t *dc,
82                              int width, int thresh, const uint16_t *dithers)
83 {
84     intptr_t x;
85     if (width&3) {
86         x = width&~3;
87         filter_line_c(dst+x, src+x, dc+x/2, width-x, thresh, dithers);
88         width = x;
89     }
90     x = -width;
91     __asm__ volatile(
92         "movd          %4, %%mm5 \n"
93         "pxor       %%mm7, %%mm7 \n"
94         "pshufw $0, %%mm5, %%mm5 \n"
95         "movq          %6, %%mm6 \n"
96         "movq          %5, %%mm4 \n"
97         "1: \n"
98         "movd     (%2,%0), %%mm0 \n"
99         "movd     (%3,%0), %%mm1 \n"
100         "punpcklbw  %%mm7, %%mm0 \n"
101         "punpcklwd  %%mm1, %%mm1 \n"
102         "psllw         $7, %%mm0 \n"
103         "pxor       %%mm2, %%mm2 \n"
104         "psubw      %%mm0, %%mm1 \n" // delta = dc - pix
105         "psubw      %%mm1, %%mm2 \n"
106         "pmaxsw     %%mm1, %%mm2 \n"
107         "pmulhuw    %%mm5, %%mm2 \n" // m = abs(delta) * thresh >> 16
108         "psubw      %%mm6, %%mm2 \n"
109         "pminsw     %%mm7, %%mm2 \n" // m = -max(0, 127-m)
110         "pmullw     %%mm2, %%mm2 \n"
111         "paddw      %%mm4, %%mm0 \n" // pix += dither
112         "pmulhw     %%mm2, %%mm1 \n"
113         "psllw         $2, %%mm1 \n" // m = m*m*delta >> 14
114         "paddw      %%mm1, %%mm0 \n" // pix += m
115         "psraw         $7, %%mm0 \n"
116         "packuswb   %%mm0, %%mm0 \n"
117         "movd       %%mm0, (%1,%0) \n" // dst = clip(pix>>7)
118         "add           $4, %0 \n"
119         "jl 1b \n"
120         "emms \n"
121         :"+r"(x)
122         :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
123          "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
124         :"memory"
125     );
126 }
127 #endif
128
129 #if HAVE_SSSE3
130 static void filter_line_ssse3(uint8_t *dst, uint8_t *src, uint16_t *dc,
131                               int width, int thresh, const uint16_t *dithers)
132 {
133     intptr_t x;
134     if (width&7) {
135         // could be 10% faster if I somehow eliminated this
136         x = width&~7;
137         filter_line_c(dst+x, src+x, dc+x/2, width-x, thresh, dithers);
138         width = x;
139     }
140     x = -width;
141     __asm__ volatile(
142         "movd           %4, %%xmm5 \n"
143         "pxor       %%xmm7, %%xmm7 \n"
144         "pshuflw $0,%%xmm5, %%xmm5 \n"
145         "movdqa         %6, %%xmm6 \n"
146         "punpcklqdq %%xmm5, %%xmm5 \n"
147         "movdqa         %5, %%xmm4 \n"
148         "1: \n"
149         "movq      (%2,%0), %%xmm0 \n"
150         "movq      (%3,%0), %%xmm1 \n"
151         "punpcklbw  %%xmm7, %%xmm0 \n"
152         "punpcklwd  %%xmm1, %%xmm1 \n"
153         "psllw          $7, %%xmm0 \n"
154         "psubw      %%xmm0, %%xmm1 \n" // delta = dc - pix
155         "pabsw      %%xmm1, %%xmm2 \n"
156         "pmulhuw    %%xmm5, %%xmm2 \n" // m = abs(delta) * thresh >> 16
157         "psubw      %%xmm6, %%xmm2 \n"
158         "pminsw     %%xmm7, %%xmm2 \n" // m = -max(0, 127-m)
159         "pmullw     %%xmm2, %%xmm2 \n"
160         "psllw          $1, %%xmm2 \n"
161         "paddw      %%xmm4, %%xmm0 \n" // pix += dither
162         "pmulhrsw   %%xmm2, %%xmm1 \n" // m = m*m*delta >> 14
163         "paddw      %%xmm1, %%xmm0 \n" // pix += m
164         "psraw          $7, %%xmm0 \n"
165         "packuswb   %%xmm0, %%xmm0 \n"
166         "movq       %%xmm0, (%1,%0) \n" // dst = clip(pix>>7)
167         "add            $8, %0 \n"
168         "jl 1b \n"
169         :"+&r"(x)
170         :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
171          "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
172         :"memory"
173     );
174 }
175 #endif // HAVE_SSSE3
176
177 #if HAVE_SSE2 && HAVE_6REGS
178 #define BLURV(load)\
179     intptr_t x = -2*width;\
180     __asm__ volatile(\
181         "movdqa %6, %%xmm7 \n"\
182         "1: \n"\
183         load"   (%4,%0), %%xmm0 \n"\
184         load"   (%5,%0), %%xmm1 \n"\
185         "movdqa  %%xmm0, %%xmm2 \n"\
186         "movdqa  %%xmm1, %%xmm3 \n"\
187         "psrlw       $8, %%xmm0 \n"\
188         "psrlw       $8, %%xmm1 \n"\
189         "pand    %%xmm7, %%xmm2 \n"\
190         "pand    %%xmm7, %%xmm3 \n"\
191         "paddw   %%xmm1, %%xmm0 \n"\
192         "paddw   %%xmm3, %%xmm2 \n"\
193         "paddw   %%xmm2, %%xmm0 \n"\
194         "paddw  (%2,%0), %%xmm0 \n"\
195         "movdqa (%1,%0), %%xmm1 \n"\
196         "movdqa  %%xmm0, (%1,%0) \n"\
197         "psubw   %%xmm1, %%xmm0 \n"\
198         "movdqa  %%xmm0, (%3,%0) \n"\
199         "add        $16, %0 \n"\
200         "jl 1b \n"\
201         :"+&r"(x)\
202         :"r"(buf+width),\
203          "r"(buf1+width),\
204          "r"(dc+width),\
205          "r"(src+width*2),\
206          "r"(src+width*2+sstride),\
207          "m"(*pw_ff)\
208         :"memory"\
209     );
210
211 static void blur_line_sse2(uint16_t *dc, uint16_t *buf, uint16_t *buf1,
212                            uint8_t *src, int sstride, int width)
213 {
214     if (((intptr_t)src|sstride)&15) {
215         BLURV("movdqu");
216     } else {
217         BLURV("movdqa");
218     }
219 }
220 #endif // HAVE_6REGS && HAVE_SSE2
221
222 static void filter_plane(struct vf_priv_s *ctx, uint8_t *dst, uint8_t *src,
223                          int width, int height, int dstride, int sstride, int r)
224 {
225     int bstride = ((width+15)&~15)/2;
226     int y;
227     uint32_t dc_factor = (1<<21)/(r*r);
228     uint16_t *dc = ctx->buf+16;
229     uint16_t *buf = ctx->buf+bstride+32;
230     int thresh = ctx->thresh;
231
232     memset(dc, 0, (bstride+16)*sizeof(*buf));
233     for (y=0; y<r; y++)
234         ctx->blur_line(dc, buf+y*bstride, buf+(y-1)*bstride, src+2*y*sstride, sstride, width/2);
235     for (;;) {
236         if (y < height-r) {
237             int mod = ((y+r)/2)%r;
238             uint16_t *buf0 = buf+mod*bstride;
239             uint16_t *buf1 = buf+(mod?mod-1:r-1)*bstride;
240             int x, v;
241             ctx->blur_line(dc, buf0, buf1, src+(y+r)*sstride, sstride, width/2);
242             for (x=v=0; x<r; x++)
243                 v += dc[x];
244             for (; x<width/2; x++) {
245                 v += dc[x] - dc[x-r];
246                 dc[x-r] = v * dc_factor >> 16;
247             }
248             for (; x<(width+r+1)/2; x++)
249                 dc[x-r] = v * dc_factor >> 16;
250             for (x=-r/2; x<0; x++)
251                 dc[x] = dc[0];
252         }
253         if (y == r) {
254             for (y=0; y<r; y++)
255                 ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
256         }
257         ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
258         if (++y >= height) break;
259         ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
260         if (++y >= height) break;
261     }
262 }
263