ce58c08a3bbd731842eca173bce152a236edcc81
[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/asm.h"
28 #include "libavutil/x86/cpu.h"
29 #include "libavcodec/vp9.h"
30
31 #if HAVE_YASM
32
33 #define fpel_func(avg, sz, opt)                                         \
34 void ff_ ## avg ## sz ## _ ## opt(uint8_t *dst, const uint8_t *src,     \
35                                   ptrdiff_t dst_stride,                 \
36                                   ptrdiff_t src_stride,                 \
37                                   int h, int mx, int my)
38
39 fpel_func(put,  4, mmx);
40 fpel_func(put,  8, mmx);
41 fpel_func(put, 16, sse);
42 fpel_func(put, 32, sse);
43 fpel_func(put, 64, sse);
44 fpel_func(avg,  4, sse);
45 fpel_func(avg,  8, sse);
46 fpel_func(avg, 16, sse2);
47 fpel_func(avg, 32, sse2);
48 fpel_func(avg, 64, sse2);
49 #undef fpel_func
50
51 #define mc_func(avg, sz, dir, opt)                                          \
52 void                                                                        \
53 ff_ ## avg ## _8tap_1d_ ## dir ## _ ## sz ## _ ## opt(uint8_t *dst,         \
54                                                       const uint8_t *src,   \
55                                                       ptrdiff_t dst_stride, \
56                                                       ptrdiff_t src_stride, \
57                                                       int h,                \
58                                                       const int8_t (*filter)[16])
59
60 #define mc_funcs(sz)            \
61     mc_func(put, sz, h, ssse3); \
62     mc_func(avg, sz, h, ssse3); \
63     mc_func(put, sz, v, ssse3); \
64     mc_func(avg, sz, v, ssse3)
65
66 mc_funcs(4);
67 mc_funcs(8);
68
69 #undef mc_funcs
70 #undef mc_func
71
72 #define mc_rep_func(avg, sz, hsz, dir, opt)                                 \
73 static av_always_inline void                                                \
74 ff_ ## avg ## _8tap_1d_ ## dir ## _ ## sz ## _ ## opt(uint8_t *dst,         \
75                                                       const uint8_t *src,   \
76                                                       ptrdiff_t dst_stride, \
77                                                       ptrdiff_t src_stride, \
78                                                       int h,                \
79                                                       const int8_t (*filter)[16]) \
80 {                                                                           \
81     ff_ ## avg ## _8tap_1d_ ## dir ## _ ## hsz ## _ ## opt(dst, src,        \
82                                                            dst_stride,      \
83                                                            src_stride,      \
84                                                            h,               \
85                                                            filter);         \
86     ff_ ## avg ## _8tap_1d_ ## dir ## _ ## hsz ## _ ## opt(dst + hsz,       \
87                                                            src + hsz,       \
88                                                            dst_stride,      \
89                                                            src_stride,      \
90                                                            h, filter);      \
91 }
92
93 #define mc_rep_funcs(sz, hsz)            \
94     mc_rep_func(put, sz, hsz, h, ssse3); \
95     mc_rep_func(avg, sz, hsz, h, ssse3); \
96     mc_rep_func(put, sz, hsz, v, ssse3); \
97     mc_rep_func(avg, sz, hsz, v, ssse3)
98
99 mc_rep_funcs(16, 8);
100 mc_rep_funcs(32, 16);
101 mc_rep_funcs(64, 32);
102
103 #undef mc_rep_funcs
104 #undef mc_rep_func
105
106 extern const int8_t ff_filters_ssse3[3][15][4][16];
107
108 #define filter_8tap_2d_fn(op, sz, f, fname)                             \
109 static void                                                             \
110 op ## _8tap_ ## fname ## _ ## sz ## hv_ssse3(uint8_t *dst,              \
111                                              const uint8_t *src,        \
112                                              ptrdiff_t dst_stride,      \
113                                              ptrdiff_t src_stride,      \
114                                              int h, int mx, int my)     \
115 {                                                                       \
116     LOCAL_ALIGNED_16(uint8_t, temp, [71 * 64]);                         \
117     ff_put_8tap_1d_h_ ## sz ## _ssse3(temp, src - 3 * src_stride,       \
118                                       64, src_stride,                   \
119                                       h + 7,                            \
120                                       ff_filters_ssse3[f][mx - 1]);     \
121     ff_ ## op ## _8tap_1d_v_ ## sz ## _ssse3(dst, temp + 3 * 64,        \
122                                              dst_stride, 64,            \
123                                              h,                         \
124                                              ff_filters_ssse3[f][my - 1]); \
125 }
126
127 #define filters_8tap_2d_fn(op, sz)                          \
128     filter_8tap_2d_fn(op, sz, FILTER_8TAP_REGULAR, regular) \
129     filter_8tap_2d_fn(op, sz, FILTER_8TAP_SHARP, sharp)     \
130     filter_8tap_2d_fn(op, sz, FILTER_8TAP_SMOOTH, smooth)
131
132 #define filters_8tap_2d_fn2(op) \
133     filters_8tap_2d_fn(op, 64)  \
134     filters_8tap_2d_fn(op, 32)  \
135     filters_8tap_2d_fn(op, 16)  \
136     filters_8tap_2d_fn(op, 8)   \
137     filters_8tap_2d_fn(op, 4)
138
139 filters_8tap_2d_fn2(put)
140 filters_8tap_2d_fn2(avg)
141
142 #undef filters_8tap_2d_fn2
143 #undef filters_8tap_2d_fn
144 #undef filter_8tap_2d_fn
145
146 #define filter_8tap_1d_fn(op, sz, f, fname, dir, dvar)                  \
147 static void                                                             \
148 op ## _8tap_ ## fname ## _ ## sz ## dir ## _ssse3(uint8_t *dst,         \
149                                                   const uint8_t *src,   \
150                                                   ptrdiff_t dst_stride, \
151                                                   ptrdiff_t src_stride, \
152                                                   int h, int mx,        \
153                                                   int my)               \
154 {                                                                       \
155     ff_ ## op ## _8tap_1d_ ## dir ## _ ## sz ## _ssse3(dst, src,        \
156                                                        dst_stride,      \
157                                                        src_stride, h,   \
158                                                        ff_filters_ssse3[f][dvar - 1]); \
159 }
160
161 #define filters_8tap_1d_fn(op, sz, dir, dvar)                          \
162     filter_8tap_1d_fn(op, sz, FILTER_8TAP_REGULAR, regular, dir, dvar) \
163     filter_8tap_1d_fn(op, sz, FILTER_8TAP_SHARP, sharp, dir, dvar)     \
164     filter_8tap_1d_fn(op, sz, FILTER_8TAP_SMOOTH, smooth, dir, dvar)
165
166 #define filters_8tap_1d_fn2(op, sz)             \
167     filters_8tap_1d_fn(op, sz, h, mx)           \
168     filters_8tap_1d_fn(op, sz, v, my)
169
170 #define filters_8tap_1d_fn3(op) \
171     filters_8tap_1d_fn2(op, 64) \
172     filters_8tap_1d_fn2(op, 32) \
173     filters_8tap_1d_fn2(op, 16) \
174     filters_8tap_1d_fn2(op,  8) \
175     filters_8tap_1d_fn2(op,  4)
176
177 filters_8tap_1d_fn3(put)
178 filters_8tap_1d_fn3(avg)
179
180 #undef filters_8tap_1d_fn
181 #undef filters_8tap_1d_fn2
182 #undef filters_8tap_1d_fn3
183 #undef filter_8tap_1d_fn
184
185 #endif /* HAVE_YASM */
186
187 av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp)
188 {
189 #if HAVE_YASM
190     int cpu_flags = av_get_cpu_flags();
191
192 #define init_fpel(idx1, idx2, sz, type, opt)                            \
193     dsp->mc[idx1][FILTER_8TAP_SMOOTH ][idx2][0][0] =                    \
194     dsp->mc[idx1][FILTER_8TAP_REGULAR][idx2][0][0] =                    \
195     dsp->mc[idx1][FILTER_8TAP_SHARP  ][idx2][0][0] =                    \
196     dsp->mc[idx1][FILTER_BILINEAR    ][idx2][0][0] = ff_ ## type ## sz ## _ ## opt
197
198
199 #define init_subpel1(idx1, idx2, idxh, idxv, sz, dir, type, opt) \
200     dsp->mc[idx1][FILTER_8TAP_SMOOTH][idx2][idxh][idxv]  = type ## _8tap_smooth_  ## sz ## dir ## _ ## opt; \
201     dsp->mc[idx1][FILTER_8TAP_REGULAR][idx2][idxh][idxv] = type ## _8tap_regular_ ## sz ## dir ## _ ## opt; \
202     dsp->mc[idx1][FILTER_8TAP_SHARP][idx2][idxh][idxv]   = type ## _8tap_sharp_   ## sz ## dir ## _ ## opt
203
204 #define init_subpel2(idx, idxh, idxv, dir, type, opt)     \
205     init_subpel1(0, idx, idxh, idxv, 64, dir, type, opt); \
206     init_subpel1(1, idx, idxh, idxv, 32, dir, type, opt); \
207     init_subpel1(2, idx, idxh, idxv, 16, dir, type, opt); \
208     init_subpel1(3, idx, idxh, idxv,  8, dir, type, opt); \
209     init_subpel1(4, idx, idxh, idxv,  4, dir, type, opt)
210
211 #define init_subpel3(idx, type, opt)        \
212     init_subpel2(idx, 1, 1, hv, type, opt); \
213     init_subpel2(idx, 0, 1,  v, type, opt); \
214     init_subpel2(idx, 1, 0,  h, type, opt)
215
216     if (EXTERNAL_MMX(cpu_flags)) {
217         init_fpel(4, 0,  4, put, mmx);
218         init_fpel(3, 0,  8, put, mmx);
219     }
220
221     if (EXTERNAL_SSE(cpu_flags)) {
222         init_fpel(2, 0, 16, put, sse);
223         init_fpel(1, 0, 32, put, sse);
224         init_fpel(0, 0, 64, put, sse);
225         init_fpel(4, 1,  4, avg, sse);
226         init_fpel(3, 1,  8, avg, sse);
227     }
228
229     if (EXTERNAL_SSE2(cpu_flags)) {
230         init_fpel(2, 1, 16, avg, sse2);
231         init_fpel(1, 1, 32, avg, sse2);
232         init_fpel(0, 1, 64, avg, sse2);
233     }
234
235     if (EXTERNAL_SSSE3(cpu_flags)) {
236         init_subpel3(0, put, ssse3);
237         init_subpel3(1, avg, ssse3);
238     }
239
240 #undef init_fpel
241 #undef init_subpel1
242 #undef init_subpel2
243 #undef init_subpel3
244
245 #endif /* HAVE_YASM */
246 }