demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / video_filter / gradfun.h
blob6ba56fbe0eb2c7c22cae34e0d0926ddb97e5a89d
1 /*
2 * Copyright (c) 2010 Nolan Lum <nol888@gmail.com>
3 * Copyright (c) 2009 Loren Merritt <lorenm@u.washignton.edu>
5 * This file is part of Libav.
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.
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.
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
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.
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);
41 static alignas (16) const uint16_t pw_7f[8] = {127,127,127,127,127,127,127,127};
42 static alignas (16) const uint16_t pw_ff[8] = {255,255,255,255,255,255,255,255};
43 static alignas (16) const uint16_t 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 },
54 static void filter_line_c(uint8_t *dst, uint8_t *src, uint16_t *dc,
55 int width, int thresh, const uint16_t *dithers)
57 for( int 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);
68 static void blur_line_c(uint16_t *dc, uint16_t *buf, uint16_t *buf1,
69 uint8_t *src, int sstride, int width)
71 for( int x = 0; x < width; x++ ) {
72 int v = buf1[x] + src[2*x] + src[2*x+1] + src[2*x+sstride] +
73 src[2*x+1+sstride];
74 int old = buf[x];
75 buf[x] = v;
76 dc[x] = v - old;
80 #if HAVE_MMX2
81 VLC_MMX
82 static void filter_line_mmx2(uint8_t *dst, uint8_t *src, uint16_t *dc,
83 int width, int thresh, const uint16_t *dithers)
85 intptr_t x;
86 if (width&3) {
87 x = width&~3;
88 filter_line_c(dst+x, src+x, dc+x/2, width-x, thresh, dithers);
89 width = x;
91 x = -width;
92 __asm__ volatile(
93 "movd %4, %%mm5 \n"
94 "pxor %%mm7, %%mm7 \n"
95 "pshufw $0, %%mm5, %%mm5 \n"
96 "movq %6, %%mm6 \n"
97 "movq %5, %%mm4 \n"
98 "1: \n"
99 "movd (%2,%0), %%mm0 \n"
100 "movd (%3,%0), %%mm1 \n"
101 "punpcklbw %%mm7, %%mm0 \n"
102 "punpcklwd %%mm1, %%mm1 \n"
103 "psllw $7, %%mm0 \n"
104 "pxor %%mm2, %%mm2 \n"
105 "psubw %%mm0, %%mm1 \n" // delta = dc - pix
106 "psubw %%mm1, %%mm2 \n"
107 "pmaxsw %%mm1, %%mm2 \n"
108 "pmulhuw %%mm5, %%mm2 \n" // m = abs(delta) * thresh >> 16
109 "psubw %%mm6, %%mm2 \n"
110 "pminsw %%mm7, %%mm2 \n" // m = -max(0, 127-m)
111 "pmullw %%mm2, %%mm2 \n"
112 "paddw %%mm4, %%mm0 \n" // pix += dither
113 "pmulhw %%mm2, %%mm1 \n"
114 "psllw $2, %%mm1 \n" // m = m*m*delta >> 14
115 "paddw %%mm1, %%mm0 \n" // pix += m
116 "psraw $7, %%mm0 \n"
117 "packuswb %%mm0, %%mm0 \n"
118 "movd %%mm0, (%1,%0) \n" // dst = clip(pix>>7)
119 "add $4, %0 \n"
120 "jl 1b \n"
121 "emms \n"
122 :"+r"(x)
123 :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
124 "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
125 :"mm0", "mm1", "mm2", "mm4", "mm5", "mm6", "memory"
128 #endif
130 #if HAVE_SSSE3
131 VLC_SSE
132 static void filter_line_ssse3(uint8_t *dst, uint8_t *src, uint16_t *dc,
133 int width, int thresh, const uint16_t *dithers)
135 intptr_t x;
136 if (width&7) {
137 // could be 10% faster if I somehow eliminated this
138 x = width&~7;
139 filter_line_c(dst+x, src+x, dc+x/2, width-x, thresh, dithers);
140 width = x;
142 x = -width;
143 __asm__ volatile(
144 "movd %4, %%xmm5 \n"
145 "pxor %%xmm7, %%xmm7 \n"
146 "pshuflw $0,%%xmm5, %%xmm5 \n"
147 "movdqa %6, %%xmm6 \n"
148 "punpcklqdq %%xmm5, %%xmm5 \n"
149 "movdqa %5, %%xmm4 \n"
150 "1: \n"
151 "movq (%2,%0), %%xmm0 \n"
152 "movq (%3,%0), %%xmm1 \n"
153 "punpcklbw %%xmm7, %%xmm0 \n"
154 "punpcklwd %%xmm1, %%xmm1 \n"
155 "psllw $7, %%xmm0 \n"
156 "psubw %%xmm0, %%xmm1 \n" // delta = dc - pix
157 "pabsw %%xmm1, %%xmm2 \n"
158 "pmulhuw %%xmm5, %%xmm2 \n" // m = abs(delta) * thresh >> 16
159 "psubw %%xmm6, %%xmm2 \n"
160 "pminsw %%xmm7, %%xmm2 \n" // m = -max(0, 127-m)
161 "pmullw %%xmm2, %%xmm2 \n"
162 "psllw $1, %%xmm2 \n"
163 "paddw %%xmm4, %%xmm0 \n" // pix += dither
164 "pmulhrsw %%xmm2, %%xmm1 \n" // m = m*m*delta >> 14
165 "paddw %%xmm1, %%xmm0 \n" // pix += m
166 "psraw $7, %%xmm0 \n"
167 "packuswb %%xmm0, %%xmm0 \n"
168 "movq %%xmm0, (%1,%0) \n" // dst = clip(pix>>7)
169 "add $8, %0 \n"
170 "jl 1b \n"
171 :"+&r"(x)
172 :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
173 "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
174 :"xmm0", "xmm1", "xmm2", "xmm4", "xmm5", "xmm6", "xmm7", "memory"
177 #endif // HAVE_SSSE3
179 #if HAVE_SSE2 && HAVE_6REGS
180 #define BLURV(load)\
181 intptr_t x = -2*width;\
182 __asm__ volatile(\
183 "movdqa %6, %%xmm7 \n"\
184 "1: \n"\
185 load" (%4,%0), %%xmm0 \n"\
186 load" (%5,%0), %%xmm1 \n"\
187 "movdqa %%xmm0, %%xmm2 \n"\
188 "movdqa %%xmm1, %%xmm3 \n"\
189 "psrlw $8, %%xmm0 \n"\
190 "psrlw $8, %%xmm1 \n"\
191 "pand %%xmm7, %%xmm2 \n"\
192 "pand %%xmm7, %%xmm3 \n"\
193 "paddw %%xmm1, %%xmm0 \n"\
194 "paddw %%xmm3, %%xmm2 \n"\
195 "paddw %%xmm2, %%xmm0 \n"\
196 "paddw (%2,%0), %%xmm0 \n"\
197 "movdqa (%1,%0), %%xmm1 \n"\
198 "movdqa %%xmm0, (%1,%0) \n"\
199 "psubw %%xmm1, %%xmm0 \n"\
200 "movdqa %%xmm0, (%3,%0) \n"\
201 "add $16, %0 \n"\
202 "jl 1b \n"\
203 :"+&r"(x)\
204 :"r"(buf+width),\
205 "r"(buf1+width),\
206 "r"(dc+width),\
207 "r"(src+width*2),\
208 "r"(src+width*2+sstride),\
209 "m"(*pw_ff)\
210 :"xmm0", "xmm1", "xmm2", "xmm3", "xmm7", "memory"\
213 VLC_SSE
214 static void blur_line_sse2(uint16_t *dc, uint16_t *buf, uint16_t *buf1,
215 uint8_t *src, int sstride, int width)
217 if (((intptr_t)src|sstride)&15) {
218 BLURV("movdqu");
219 } else {
220 BLURV("movdqa");
223 #endif // HAVE_6REGS && HAVE_SSE2
225 static void filter_plane(struct vf_priv_s *ctx, uint8_t *dst, uint8_t *src,
226 int width, int height, int dstride, int sstride, int r)
228 int bstride = ((width+15)&~15)/2;
229 int y;
230 uint32_t dc_factor = (1<<21)/(r*r);
231 uint16_t *dc = ctx->buf+16;
232 uint16_t *buf = ctx->buf+bstride+32;
233 int thresh = ctx->thresh;
235 memset(dc, 0, (bstride+16)*sizeof(*buf));
236 for (y=0; y<r; y++)
237 ctx->blur_line(dc, buf+y*bstride, buf+(y-1)*bstride, src+2*y*sstride, sstride, width/2);
238 for (;;) {
239 if (y < height-r) {
240 int mod = ((y+r)/2)%r;
241 uint16_t *buf0 = buf+mod*bstride;
242 uint16_t *buf1 = buf+(mod?mod-1:r-1)*bstride;
243 int x, v;
244 ctx->blur_line(dc, buf0, buf1, src+(y+r)*sstride, sstride, width/2);
245 for (x=v=0; x<r; x++)
246 v += dc[x];
247 for (; x<width/2; x++) {
248 v += dc[x] - dc[x-r];
249 dc[x-r] = v * dc_factor >> 16;
251 for (; x<(width+r+1)/2; x++)
252 dc[x-r] = v * dc_factor >> 16;
253 for (x=-r/2; x<0; x++)
254 dc[x] = dc[0];
256 if (y == r) {
257 for (y=0; y<r; y++)
258 ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
260 ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
261 if (++y >= height) break;
262 ctx->filter_line(dst+y*dstride, src+y*sstride, dc-r/2, width, thresh, dither[y&7]);
263 if (++y >= height) break;