2b94af34802c87c6d211e9791dfd761e26d7cd89
[ffmpeg.git] / libavcodec / x86 / vp9dsp_init.c
1 /*
2  * VP9 SIMD optimizations
3  *
4  * Copyright (c) 2013 Ronald S. Bultje <rsbultje@gmail.com>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/attributes.h"
24 #include "libavutil/cpu.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/x86/cpu.h"
28 #include "libavcodec/vp9.h"
29
30 #if HAVE_YASM
31
32 #define fpel_func(avg, sz, opt)                                         \
33 void ff_ ## avg ## sz ## _ ## opt(uint8_t *dst, const uint8_t *src,     \
34                                   ptrdiff_t dst_stride,                 \
35                                   ptrdiff_t src_stride,                 \
36                                   int h, int mx, int my)
37
38 fpel_func(put,  4, mmx);
39 fpel_func(put,  8, mmx);
40 fpel_func(put, 16, sse);
41 fpel_func(put, 32, sse);
42 fpel_func(put, 64, sse);
43 fpel_func(avg,  4, mmxext);
44 fpel_func(avg,  8, mmxext);
45 fpel_func(avg, 16, sse2);
46 fpel_func(avg, 32, sse2);
47 fpel_func(avg, 64, sse2);
48 #undef fpel_func
49
50 #define mc_func(avg, sz, dir, opt)                                          \
51 void                                                                        \
52 ff_ ## avg ## _8tap_1d_ ## dir ## _ ## sz ## _ ## opt(uint8_t *dst,         \
53                                                       const uint8_t *src,   \
54                                                       ptrdiff_t dst_stride, \
55                                                       ptrdiff_t src_stride, \
56                                                       int h,                \
57                                                       const int8_t (*filter)[16])
58
59 #define mc_funcs(sz)            \
60     mc_func(put, sz, h, ssse3); \
61     mc_func(avg, sz, h, ssse3); \
62     mc_func(put, sz, v, ssse3); \
63     mc_func(avg, sz, v, ssse3)
64
65 mc_funcs(4);
66 mc_funcs(8);
67 #if ARCH_X86_64
68 mc_funcs(16);
69 #endif
70
71 #undef mc_funcs
72 #undef mc_func
73
74 #define mc_rep_func(avg, sz, hsz, dir, opt)                                 \
75 static av_always_inline void                                                \
76 ff_ ## avg ## _8tap_1d_ ## dir ## _ ## sz ## _ ## opt(uint8_t *dst,         \
77                                                       const uint8_t *src,   \
78                                                       ptrdiff_t dst_stride, \
79                                                       ptrdiff_t src_stride, \
80                                                       int h,                \
81                                                       const int8_t (*filter)[16]) \
82 {                                                                           \
83     ff_ ## avg ## _8tap_1d_ ## dir ## _ ## hsz ## _ ## opt(dst, src,        \
84                                                            dst_stride,      \
85                                                            src_stride,      \
86                                                            h,               \
87                                                            filter);         \
88     ff_ ## avg ## _8tap_1d_ ## dir ## _ ## hsz ## _ ## opt(dst + hsz,       \
89                                                            src + hsz,       \
90                                                            dst_stride,      \
91                                                            src_stride,      \
92                                                            h, filter);      \
93 }
94
95 #define mc_rep_funcs(sz, hsz)            \
96     mc_rep_func(put, sz, hsz, h, ssse3); \
97     mc_rep_func(avg, sz, hsz, h, ssse3); \
98     mc_rep_func(put, sz, hsz, v, ssse3); \
99     mc_rep_func(avg, sz, hsz, v, ssse3)
100
101 #if ARCH_X86_32
102 mc_rep_funcs(16, 8);
103 #endif
104 mc_rep_funcs(32, 16);
105 mc_rep_funcs(64, 32);
106
107 #undef mc_rep_funcs
108 #undef mc_rep_func
109
110 extern const int8_t ff_filters_ssse3[3][15][4][16];
111
112 #define filter_8tap_2d_fn(op, sz, f, fname)                             \
113 static void                                                             \
114 op ## _8tap_ ## fname ## _ ## sz ## hv_ssse3(uint8_t *dst,              \
115                                              const uint8_t *src,        \
116                                              ptrdiff_t dst_stride,      \
117                                              ptrdiff_t src_stride,      \
118                                              int h, int mx, int my)     \
119 {                                                                       \
120     LOCAL_ALIGNED_16(uint8_t, temp, [71 * 64]);                         \
121     ff_put_8tap_1d_h_ ## sz ## _ssse3(temp, src - 3 * src_stride,       \
122                                       64, src_stride,                   \
123                                       h + 7,                            \
124                                       ff_filters_ssse3[f][mx - 1]);     \
125     ff_ ## op ## _8tap_1d_v_ ## sz ## _ssse3(dst, temp + 3 * 64,        \
126                                              dst_stride, 64,            \
127                                              h,                         \
128                                              ff_filters_ssse3[f][my - 1]); \
129 }
130
131 #define filters_8tap_2d_fn(op, sz)                          \
132     filter_8tap_2d_fn(op, sz, FILTER_8TAP_REGULAR, regular) \
133     filter_8tap_2d_fn(op, sz, FILTER_8TAP_SHARP, sharp)     \
134     filter_8tap_2d_fn(op, sz, FILTER_8TAP_SMOOTH, smooth)
135
136 #define filters_8tap_2d_fn2(op) \
137     filters_8tap_2d_fn(op, 64)  \
138     filters_8tap_2d_fn(op, 32)  \
139     filters_8tap_2d_fn(op, 16)  \
140     filters_8tap_2d_fn(op, 8)   \
141     filters_8tap_2d_fn(op, 4)
142
143 filters_8tap_2d_fn2(put)
144 filters_8tap_2d_fn2(avg)
145
146 #undef filters_8tap_2d_fn2
147 #undef filters_8tap_2d_fn
148 #undef filter_8tap_2d_fn
149
150 #define filter_8tap_1d_fn(op, sz, f, fname, dir, dvar)                  \
151 static void                                                             \
152 op ## _8tap_ ## fname ## _ ## sz ## dir ## _ssse3(uint8_t *dst,         \
153                                                   const uint8_t *src,   \
154                                                   ptrdiff_t dst_stride, \
155                                                   ptrdiff_t src_stride, \
156                                                   int h, int mx,        \
157                                                   int my)               \
158 {                                                                       \
159     ff_ ## op ## _8tap_1d_ ## dir ## _ ## sz ## _ssse3(dst, src,        \
160                                                        dst_stride,      \
161                                                        src_stride, h,   \
162                                                        ff_filters_ssse3[f][dvar - 1]); \
163 }
164
165 #define filters_8tap_1d_fn(op, sz, dir, dvar)                          \
166     filter_8tap_1d_fn(op, sz, FILTER_8TAP_REGULAR, regular, dir, dvar) \
167     filter_8tap_1d_fn(op, sz, FILTER_8TAP_SHARP, sharp, dir, dvar)     \
168     filter_8tap_1d_fn(op, sz, FILTER_8TAP_SMOOTH, smooth, dir, dvar)
169
170 #define filters_8tap_1d_fn2(op, sz)             \
171     filters_8tap_1d_fn(op, sz, h, mx)           \
172     filters_8tap_1d_fn(op, sz, v, my)
173
174 #define filters_8tap_1d_fn3(op) \
175     filters_8tap_1d_fn2(op, 64) \
176     filters_8tap_1d_fn2(op, 32) \
177     filters_8tap_1d_fn2(op, 16) \
178     filters_8tap_1d_fn2(op,  8) \
179     filters_8tap_1d_fn2(op,  4)
180
181 filters_8tap_1d_fn3(put)
182 filters_8tap_1d_fn3(avg)
183
184 #undef filters_8tap_1d_fn
185 #undef filters_8tap_1d_fn2
186 #undef filters_8tap_1d_fn3
187 #undef filter_8tap_1d_fn
188
189 #endif /* HAVE_YASM */
190
191 av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp)
192 {
193 #if HAVE_YASM
194     int cpu_flags = av_get_cpu_flags();
195
196 #define init_fpel(idx1, idx2, sz, type, opt)                            \
197     dsp->mc[idx1][FILTER_8TAP_SMOOTH ][idx2][0][0] =                    \
198     dsp->mc[idx1][FILTER_8TAP_REGULAR][idx2][0][0] =                    \
199     dsp->mc[idx1][FILTER_8TAP_SHARP  ][idx2][0][0] =                    \
200     dsp->mc[idx1][FILTER_BILINEAR    ][idx2][0][0] = ff_ ## type ## sz ## _ ## opt
201
202
203 #define init_subpel1(idx1, idx2, idxh, idxv, sz, dir, type, opt) \
204     dsp->mc[idx1][FILTER_8TAP_SMOOTH][idx2][idxh][idxv]  = type ## _8tap_smooth_  ## sz ## dir ## _ ## opt; \
205     dsp->mc[idx1][FILTER_8TAP_REGULAR][idx2][idxh][idxv] = type ## _8tap_regular_ ## sz ## dir ## _ ## opt; \
206     dsp->mc[idx1][FILTER_8TAP_SHARP][idx2][idxh][idxv]   = type ## _8tap_sharp_   ## sz ## dir ## _ ## opt
207
208 #define init_subpel2(idx, idxh, idxv, dir, type, opt)     \
209     init_subpel1(0, idx, idxh, idxv, 64, dir, type, opt); \
210     init_subpel1(1, idx, idxh, idxv, 32, dir, type, opt); \
211     init_subpel1(2, idx, idxh, idxv, 16, dir, type, opt); \
212     init_subpel1(3, idx, idxh, idxv,  8, dir, type, opt); \
213     init_subpel1(4, idx, idxh, idxv,  4, dir, type, opt)
214
215 #define init_subpel3(idx, type, opt)        \
216     init_subpel2(idx, 1, 1, hv, type, opt); \
217     init_subpel2(idx, 0, 1,  v, type, opt); \
218     init_subpel2(idx, 1, 0,  h, type, opt)
219
220     if (EXTERNAL_MMX(cpu_flags)) {
221         init_fpel(4, 0,  4, put, mmx);
222         init_fpel(3, 0,  8, put, mmx);
223     }
224
225     if (EXTERNAL_MMXEXT(cpu_flags)) {
226         init_fpel(4, 1,  4, avg, mmxext);
227         init_fpel(3, 1,  8, avg, mmxext);
228     }
229
230     if (EXTERNAL_SSE(cpu_flags)) {
231         init_fpel(2, 0, 16, put, sse);
232         init_fpel(1, 0, 32, put, sse);
233         init_fpel(0, 0, 64, put, sse);
234     }
235
236     if (EXTERNAL_SSE2(cpu_flags)) {
237         init_fpel(2, 1, 16, avg, sse2);
238         init_fpel(1, 1, 32, avg, sse2);
239         init_fpel(0, 1, 64, avg, sse2);
240     }
241
242     if (EXTERNAL_SSSE3(cpu_flags)) {
243         init_subpel3(0, put, ssse3);
244         init_subpel3(1, avg, ssse3);
245     }
246
247 #undef init_fpel
248 #undef init_subpel1
249 #undef init_subpel2
250 #undef init_subpel3
251
252 #endif /* HAVE_YASM */
253 }