]> git.sesse.net Git - ffmpeg/blob - libavfilter/x86/gradfun.c
lavf: move ff_codec_get_tag() and ff_codec_get_id() definitions to internal.h
[ffmpeg] / libavfilter / x86 / gradfun.c
1 /*
2  * Copyright (C) 2009 Loren Merritt <lorenm@u.washignton.edu>
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "libavutil/attributes.h"
22 #include "libavutil/cpu.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/x86/asm.h"
25 #include "libavfilter/gradfun.h"
26
27 #if HAVE_INLINE_ASM
28
29 DECLARE_ALIGNED(16, static const uint16_t, pw_7f)[8] = {0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F};
30 DECLARE_ALIGNED(16, static const uint16_t, pw_ff)[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
31
32 #if HAVE_MMXEXT_INLINE
33 static void gradfun_filter_line_mmxext(uint8_t *dst, uint8_t *src, uint16_t *dc,
34                                        int width, int thresh,
35                                        const uint16_t *dithers)
36 {
37     intptr_t x;
38     if (width & 3) {
39         x = width & ~3;
40         ff_gradfun_filter_line_c(dst + x, src + x, dc + x / 2, width - x, thresh, dithers);
41         width = x;
42     }
43     x = -width;
44     __asm__ volatile(
45         "movd          %4, %%mm5 \n"
46         "pxor       %%mm7, %%mm7 \n"
47         "pshufw $0, %%mm5, %%mm5 \n"
48         "movq          %6, %%mm6 \n"
49         "movq          %5, %%mm4 \n"
50         "1: \n"
51         "movd     (%2,%0), %%mm0 \n"
52         "movd     (%3,%0), %%mm1 \n"
53         "punpcklbw  %%mm7, %%mm0 \n"
54         "punpcklwd  %%mm1, %%mm1 \n"
55         "psllw         $7, %%mm0 \n"
56         "pxor       %%mm2, %%mm2 \n"
57         "psubw      %%mm0, %%mm1 \n" // delta = dc - pix
58         "psubw      %%mm1, %%mm2 \n"
59         "pmaxsw     %%mm1, %%mm2 \n"
60         "pmulhuw    %%mm5, %%mm2 \n" // m = abs(delta) * thresh >> 16
61         "psubw      %%mm6, %%mm2 \n"
62         "pminsw     %%mm7, %%mm2 \n" // m = -max(0, 127-m)
63         "pmullw     %%mm2, %%mm2 \n"
64         "paddw      %%mm4, %%mm0 \n" // pix += dither
65         "pmulhw     %%mm2, %%mm1 \n"
66         "psllw         $2, %%mm1 \n" // m = m*m*delta >> 14
67         "paddw      %%mm1, %%mm0 \n" // pix += m
68         "psraw         $7, %%mm0 \n"
69         "packuswb   %%mm0, %%mm0 \n"
70         "movd       %%mm0, (%1,%0) \n" // dst = clip(pix>>7)
71         "add           $4, %0 \n"
72         "jl 1b \n"
73         "emms \n"
74         :"+r"(x)
75         :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
76          "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
77         :"memory"
78     );
79 }
80 #endif
81
82 #if HAVE_SSSE3_INLINE
83 static void gradfun_filter_line_ssse3(uint8_t *dst, uint8_t *src, uint16_t *dc, int width, int thresh, const uint16_t *dithers)
84 {
85     intptr_t x;
86     if (width & 7) {
87         // could be 10% faster if I somehow eliminated this
88         x = width & ~7;
89         ff_gradfun_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, %%xmm5 \n"
95         "pxor       %%xmm7, %%xmm7 \n"
96         "pshuflw $0,%%xmm5, %%xmm5 \n"
97         "movdqa         %6, %%xmm6 \n"
98         "punpcklqdq %%xmm5, %%xmm5 \n"
99         "movdqa         %5, %%xmm4 \n"
100         "1: \n"
101         "movq      (%2,%0), %%xmm0 \n"
102         "movq      (%3,%0), %%xmm1 \n"
103         "punpcklbw  %%xmm7, %%xmm0 \n"
104         "punpcklwd  %%xmm1, %%xmm1 \n"
105         "psllw          $7, %%xmm0 \n"
106         "psubw      %%xmm0, %%xmm1 \n" // delta = dc - pix
107         "pabsw      %%xmm1, %%xmm2 \n"
108         "pmulhuw    %%xmm5, %%xmm2 \n" // m = abs(delta) * thresh >> 16
109         "psubw      %%xmm6, %%xmm2 \n"
110         "pminsw     %%xmm7, %%xmm2 \n" // m = -max(0, 127-m)
111         "pmullw     %%xmm2, %%xmm2 \n"
112         "psllw          $1, %%xmm2 \n"
113         "paddw      %%xmm4, %%xmm0 \n" // pix += dither
114         "pmulhrsw   %%xmm2, %%xmm1 \n" // m = m*m*delta >> 14
115         "paddw      %%xmm1, %%xmm0 \n" // pix += m
116         "psraw          $7, %%xmm0 \n"
117         "packuswb   %%xmm0, %%xmm0 \n"
118         "movq       %%xmm0, (%1,%0) \n" // dst = clip(pix>>7)
119         "add            $8, %0 \n"
120         "jl 1b \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 /* HAVE_SSSE3_INLINE */
128
129 #if HAVE_SSE2_INLINE
130 static void gradfun_blur_line_sse2(uint16_t *dc, uint16_t *buf, uint16_t *buf1, uint8_t *src, int src_linesize, int width)
131 {
132 #define BLURV(load)\
133     intptr_t x = -2*width;\
134     __asm__ volatile(\
135         "movdqa %6, %%xmm7 \n"\
136         "1: \n"\
137         load"   (%4,%0), %%xmm0 \n"\
138         load"   (%5,%0), %%xmm1 \n"\
139         "movdqa  %%xmm0, %%xmm2 \n"\
140         "movdqa  %%xmm1, %%xmm3 \n"\
141         "psrlw       $8, %%xmm0 \n"\
142         "psrlw       $8, %%xmm1 \n"\
143         "pand    %%xmm7, %%xmm2 \n"\
144         "pand    %%xmm7, %%xmm3 \n"\
145         "paddw   %%xmm1, %%xmm0 \n"\
146         "paddw   %%xmm3, %%xmm2 \n"\
147         "paddw   %%xmm2, %%xmm0 \n"\
148         "paddw  (%2,%0), %%xmm0 \n"\
149         "movdqa (%1,%0), %%xmm1 \n"\
150         "movdqa  %%xmm0, (%1,%0) \n"\
151         "psubw   %%xmm1, %%xmm0 \n"\
152         "movdqa  %%xmm0, (%3,%0) \n"\
153         "add        $16, %0 \n"\
154         "jl 1b \n"\
155         :"+&r"(x)\
156         :"r"(buf+width),\
157          "r"(buf1+width),\
158          "r"(dc+width),\
159          "r"(src+width*2),\
160          "r"(src+width*2+src_linesize),\
161          "m"(*pw_ff)\
162         :"memory"\
163     );
164     if (((intptr_t) src | src_linesize) & 15) {
165         BLURV("movdqu");
166     } else {
167         BLURV("movdqa");
168     }
169 }
170 #endif /* HAVE_SSE2_INLINE */
171
172 #endif /* HAVE_INLINE_ASM */
173
174 av_cold void ff_gradfun_init_x86(GradFunContext *gf)
175 {
176     int cpu_flags = av_get_cpu_flags();
177
178 #if HAVE_MMXEXT_INLINE
179     if (cpu_flags & AV_CPU_FLAG_MMXEXT)
180         gf->filter_line = gradfun_filter_line_mmxext;
181 #endif
182 #if HAVE_SSSE3_INLINE
183     if (cpu_flags & AV_CPU_FLAG_SSSE3)
184         gf->filter_line = gradfun_filter_line_ssse3;
185 #endif
186 #if HAVE_SSE2_INLINE
187     if (cpu_flags & AV_CPU_FLAG_SSE2)
188         gf->blur_line = gradfun_blur_line_sse2;
189 #endif
190 }