2 * Copyright (C) 2009 Loren Merritt <lorenm@u.washignton.edu>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/cpu.h"
22 #include "libavutil/x86_cpu.h"
23 #include "libavfilter/gradfun.h"
25 DECLARE_ALIGNED(16, static const uint16_t, pw_7f)[8] = {0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F};
26 DECLARE_ALIGNED(16, static const uint16_t, pw_ff)[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
28 void ff_gradfun_filter_line_mmx2(uint8_t *dst, uint8_t *src, uint16_t *dc, int width, int thresh, const uint16_t *dithers)
34 ff_gradfun_filter_line_c(dst + x, src + x, dc + x / 2, width - x, thresh, dithers);
40 "pxor %%mm7, %%mm7 \n"
41 "pshufw $0, %%mm5, %%mm5 \n"
45 "movd (%2,%0), %%mm0 \n"
46 "movd (%3,%0), %%mm1 \n"
47 "punpcklbw %%mm7, %%mm0 \n"
48 "punpcklwd %%mm1, %%mm1 \n"
50 "pxor %%mm2, %%mm2 \n"
51 "psubw %%mm0, %%mm1 \n" // delta = dc - pix
52 "psubw %%mm1, %%mm2 \n"
53 "pmaxsw %%mm1, %%mm2 \n"
54 "pmulhuw %%mm5, %%mm2 \n" // m = abs(delta) * thresh >> 16
55 "psubw %%mm6, %%mm2 \n"
56 "pminsw %%mm7, %%mm2 \n" // m = -max(0, 127-m)
57 "pmullw %%mm2, %%mm2 \n"
58 "paddw %%mm4, %%mm0 \n" // pix += dither
59 "pmulhw %%mm2, %%mm1 \n"
60 "psllw $2, %%mm1 \n" // m = m*m*delta >> 14
61 "paddw %%mm1, %%mm0 \n" // pix += m
63 "packuswb %%mm0, %%mm0 \n"
64 "movd %%mm0, (%1,%0) \n" // dst = clip(pix>>7)
69 :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
70 "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
76 void ff_gradfun_filter_line_ssse3(uint8_t *dst, uint8_t *src, uint16_t *dc, int width, int thresh, const uint16_t *dithers)
81 // could be 10% faster if I somehow eliminated this
83 ff_gradfun_filter_line_c(dst + x, src + x, dc + x / 2, width - x, thresh, dithers);
89 "pxor %%xmm7, %%xmm7 \n"
90 "pshuflw $0,%%xmm5, %%xmm5 \n"
91 "movdqa %6, %%xmm6 \n"
92 "punpcklqdq %%xmm5, %%xmm5 \n"
93 "movdqa %5, %%xmm4 \n"
95 "movq (%2,%0), %%xmm0 \n"
96 "movq (%3,%0), %%xmm1 \n"
97 "punpcklbw %%xmm7, %%xmm0 \n"
98 "punpcklwd %%xmm1, %%xmm1 \n"
100 "psubw %%xmm0, %%xmm1 \n" // delta = dc - pix
101 "pabsw %%xmm1, %%xmm2 \n"
102 "pmulhuw %%xmm5, %%xmm2 \n" // m = abs(delta) * thresh >> 16
103 "psubw %%xmm6, %%xmm2 \n"
104 "pminsw %%xmm7, %%xmm2 \n" // m = -max(0, 127-m)
105 "pmullw %%xmm2, %%xmm2 \n"
106 "psllw $1, %%xmm2 \n"
107 "paddw %%xmm4, %%xmm0 \n" // pix += dither
108 "pmulhrsw %%xmm2, %%xmm1 \n" // m = m*m*delta >> 14
109 "paddw %%xmm1, %%xmm0 \n" // pix += m
110 "psraw $7, %%xmm0 \n"
111 "packuswb %%xmm0, %%xmm0 \n"
112 "movq %%xmm0, (%1,%0) \n" // dst = clip(pix>>7)
116 :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
117 "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
123 void ff_gradfun_blur_line_sse2(uint16_t *dc, uint16_t *buf, uint16_t *buf1, uint8_t *src, int src_linesize, int width)
127 intptr_t x = -2*width;\
129 "movdqa %6, %%xmm7 \n"\
131 load" (%4,%0), %%xmm0 \n"\
132 load" (%5,%0), %%xmm1 \n"\
133 "movdqa %%xmm0, %%xmm2 \n"\
134 "movdqa %%xmm1, %%xmm3 \n"\
135 "psrlw $8, %%xmm0 \n"\
136 "psrlw $8, %%xmm1 \n"\
137 "pand %%xmm7, %%xmm2 \n"\
138 "pand %%xmm7, %%xmm3 \n"\
139 "paddw %%xmm1, %%xmm0 \n"\
140 "paddw %%xmm3, %%xmm2 \n"\
141 "paddw %%xmm2, %%xmm0 \n"\
142 "paddw (%2,%0), %%xmm0 \n"\
143 "movdqa (%1,%0), %%xmm1 \n"\
144 "movdqa %%xmm0, (%1,%0) \n"\
145 "psubw %%xmm1, %%xmm0 \n"\
146 "movdqa %%xmm0, (%3,%0) \n"\
154 "r"(src+width*2+src_linesize),\
158 if (((intptr_t) src | src_linesize) & 15) {