]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/vp9dsp_init.c
Merge commit '906ffed9b1b8b06979eb656989aecacb1ae75a3a'
[ffmpeg] / 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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/mem.h"
26 #include "libavutil/x86/cpu.h"
27 #include "libavcodec/vp9dsp.h"
28 #include "libavcodec/x86/vp9dsp_init.h"
29
30 #if HAVE_YASM
31
32 decl_fpel_func(put,  4,   , mmx);
33 decl_fpel_func(put,  8,   , mmx);
34 decl_fpel_func(put, 16,   , sse);
35 decl_fpel_func(put, 32,   , sse);
36 decl_fpel_func(put, 64,   , sse);
37 decl_fpel_func(avg,  4, _8, mmxext);
38 decl_fpel_func(avg,  8, _8, mmxext);
39 decl_fpel_func(avg, 16, _8, sse2);
40 decl_fpel_func(avg, 32, _8, sse2);
41 decl_fpel_func(avg, 64, _8, sse2);
42 decl_fpel_func(put, 32,   , avx);
43 decl_fpel_func(put, 64,   , avx);
44 decl_fpel_func(avg, 32, _8, avx2);
45 decl_fpel_func(avg, 64, _8, avx2);
46
47 decl_mc_funcs(4, mmxext, int16_t, 8, 8);
48 decl_mc_funcs(8, sse2, int16_t,  8, 8);
49 decl_mc_funcs(4, ssse3, int8_t, 32, 8);
50 decl_mc_funcs(8, ssse3, int8_t, 32, 8);
51 #if ARCH_X86_64
52 decl_mc_funcs(16, ssse3, int8_t, 32, 8);
53 decl_mc_funcs(32, avx2, int8_t, 32, 8);
54 #endif
55
56 mc_rep_funcs(16,  8,  8,  sse2, int16_t,  8, 8)
57 #if ARCH_X86_32
58 mc_rep_funcs(16,  8,  8, ssse3, int8_t,  32, 8)
59 #endif
60 mc_rep_funcs(32, 16, 16, sse2,  int16_t,  8, 8)
61 mc_rep_funcs(32, 16, 16, ssse3, int8_t,  32, 8)
62 mc_rep_funcs(64, 32, 32, sse2,  int16_t,  8, 8)
63 mc_rep_funcs(64, 32, 32, ssse3, int8_t,  32, 8)
64 #if ARCH_X86_64 && HAVE_AVX2_EXTERNAL
65 mc_rep_funcs(64, 32, 32, avx2,  int8_t,  32, 8)
66 #endif
67
68 extern const int8_t ff_filters_ssse3[3][15][4][32];
69 extern const int16_t ff_filters_sse2[3][15][8][8];
70
71 filters_8tap_2d_fn2(put, 16, 8, 1, mmxext, sse2, sse2)
72 filters_8tap_2d_fn2(avg, 16, 8, 1, mmxext, sse2, sse2)
73 filters_8tap_2d_fn2(put, 16, 8, 1, ssse3, ssse3, ssse3)
74 filters_8tap_2d_fn2(avg, 16, 8, 1, ssse3, ssse3, ssse3)
75 #if ARCH_X86_64 && HAVE_AVX2_EXTERNAL
76 filters_8tap_2d_fn(put, 64, 32, 8, 1, avx2, ssse3)
77 filters_8tap_2d_fn(put, 32, 32, 8, 1, avx2, ssse3)
78 filters_8tap_2d_fn(avg, 64, 32, 8, 1, avx2, ssse3)
79 filters_8tap_2d_fn(avg, 32, 32, 8, 1, avx2, ssse3)
80 #endif
81
82 filters_8tap_1d_fn3(put, 8, mmxext, sse2, sse2)
83 filters_8tap_1d_fn3(avg, 8, mmxext, sse2, sse2)
84 filters_8tap_1d_fn3(put, 8, ssse3, ssse3, ssse3)
85 filters_8tap_1d_fn3(avg, 8, ssse3, ssse3, ssse3)
86 #if ARCH_X86_64 && HAVE_AVX2_EXTERNAL
87 filters_8tap_1d_fn2(put, 64, 8, avx2, ssse3)
88 filters_8tap_1d_fn2(put, 32, 8, avx2, ssse3)
89 filters_8tap_1d_fn2(avg, 64, 8, avx2, ssse3)
90 filters_8tap_1d_fn2(avg, 32, 8, avx2, ssse3)
91 #endif
92
93 #define itxfm_func(typea, typeb, size, opt) \
94 void ff_vp9_##typea##_##typeb##_##size##x##size##_add_##opt(uint8_t *dst, ptrdiff_t stride, \
95                                                             int16_t *block, int eob)
96 #define itxfm_funcs(size, opt) \
97 itxfm_func(idct,  idct,  size, opt); \
98 itxfm_func(iadst, idct,  size, opt); \
99 itxfm_func(idct,  iadst, size, opt); \
100 itxfm_func(iadst, iadst, size, opt)
101
102 itxfm_func(idct,  idct,  4, mmxext);
103 itxfm_func(idct,  iadst, 4, sse2);
104 itxfm_func(iadst, idct,  4, sse2);
105 itxfm_func(iadst, iadst, 4, sse2);
106 itxfm_funcs(4, ssse3);
107 itxfm_funcs(8, sse2);
108 itxfm_funcs(8, ssse3);
109 itxfm_funcs(8, avx);
110 itxfm_funcs(16, sse2);
111 itxfm_funcs(16, ssse3);
112 itxfm_funcs(16, avx);
113 itxfm_func(idct, idct, 32, sse2);
114 itxfm_func(idct, idct, 32, ssse3);
115 itxfm_func(idct, idct, 32, avx);
116 itxfm_func(iwht, iwht, 4, mmx);
117 itxfm_func(idct, idct, 16, avx2);
118
119 #undef itxfm_func
120 #undef itxfm_funcs
121
122 #define lpf_funcs(size1, size2, opt) \
123 void ff_vp9_loop_filter_v_##size1##_##size2##_##opt(uint8_t *dst, ptrdiff_t stride, \
124                                                     int E, int I, int H); \
125 void ff_vp9_loop_filter_h_##size1##_##size2##_##opt(uint8_t *dst, ptrdiff_t stride, \
126                                                     int E, int I, int H)
127
128 lpf_funcs(16, 16, sse2);
129 lpf_funcs(16, 16, ssse3);
130 lpf_funcs(16, 16, avx);
131 lpf_funcs(44, 16, sse2);
132 lpf_funcs(44, 16, ssse3);
133 lpf_funcs(44, 16, avx);
134 lpf_funcs(84, 16, sse2);
135 lpf_funcs(84, 16, ssse3);
136 lpf_funcs(84, 16, avx);
137 lpf_funcs(48, 16, sse2);
138 lpf_funcs(48, 16, ssse3);
139 lpf_funcs(48, 16, avx);
140 lpf_funcs(88, 16, sse2);
141 lpf_funcs(88, 16, ssse3);
142 lpf_funcs(88, 16, avx);
143
144 #undef lpf_funcs
145
146 #define ipred_func(size, type, opt) \
147 void ff_vp9_ipred_##type##_##size##x##size##_##opt(uint8_t *dst, ptrdiff_t stride, \
148                                                    const uint8_t *l, const uint8_t *a)
149
150 ipred_func(8, v, mmx);
151
152 #define ipred_dc_funcs(size, opt) \
153 ipred_func(size, dc, opt); \
154 ipred_func(size, dc_left, opt); \
155 ipred_func(size, dc_top, opt)
156
157 ipred_dc_funcs(4, mmxext);
158 ipred_dc_funcs(8, mmxext);
159
160 #define ipred_dir_tm_funcs(size, opt) \
161 ipred_func(size, tm, opt); \
162 ipred_func(size, dl, opt); \
163 ipred_func(size, dr, opt); \
164 ipred_func(size, hd, opt); \
165 ipred_func(size, hu, opt); \
166 ipred_func(size, vl, opt); \
167 ipred_func(size, vr, opt)
168
169 ipred_dir_tm_funcs(4, mmxext);
170
171 ipred_func(16, v, sse);
172 ipred_func(32, v, sse);
173
174 ipred_dc_funcs(16, sse2);
175 ipred_dc_funcs(32, sse2);
176
177 #define ipred_dir_tm_h_funcs(size, opt) \
178 ipred_dir_tm_funcs(size, opt); \
179 ipred_func(size, h, opt)
180
181 ipred_dir_tm_h_funcs(8, sse2);
182 ipred_dir_tm_h_funcs(16, sse2);
183 ipred_dir_tm_h_funcs(32, sse2);
184
185 ipred_func(4, h, sse2);
186
187 #define ipred_all_funcs(size, opt) \
188 ipred_dc_funcs(size, opt); \
189 ipred_dir_tm_h_funcs(size, opt)
190
191 // FIXME hd/vl_4x4_ssse3 does not exist
192 ipred_all_funcs(4, ssse3);
193 ipred_all_funcs(8, ssse3);
194 ipred_all_funcs(16, ssse3);
195 ipred_all_funcs(32, ssse3);
196
197 ipred_dir_tm_h_funcs(8, avx);
198 ipred_dir_tm_h_funcs(16, avx);
199 ipred_dir_tm_h_funcs(32, avx);
200
201 ipred_func(32, v, avx);
202
203 ipred_dc_funcs(32, avx2);
204 ipred_func(32, h, avx2);
205 ipred_func(32, tm, avx2);
206
207 #undef ipred_func
208 #undef ipred_dir_tm_h_funcs
209 #undef ipred_dir_tm_funcs
210 #undef ipred_dc_funcs
211
212 #endif /* HAVE_YASM */
213
214 av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int bpp, int bitexact)
215 {
216 #if HAVE_YASM
217     int cpu_flags;
218
219     if (bpp == 10) {
220         ff_vp9dsp_init_10bpp_x86(dsp, bitexact);
221         return;
222     } else if (bpp == 12) {
223         ff_vp9dsp_init_12bpp_x86(dsp, bitexact);
224         return;
225     }
226
227     cpu_flags = av_get_cpu_flags();
228
229 #define init_lpf(opt) do { \
230     dsp->loop_filter_16[0] = ff_vp9_loop_filter_h_16_16_##opt; \
231     dsp->loop_filter_16[1] = ff_vp9_loop_filter_v_16_16_##opt; \
232     dsp->loop_filter_mix2[0][0][0] = ff_vp9_loop_filter_h_44_16_##opt; \
233     dsp->loop_filter_mix2[0][0][1] = ff_vp9_loop_filter_v_44_16_##opt; \
234     dsp->loop_filter_mix2[0][1][0] = ff_vp9_loop_filter_h_48_16_##opt; \
235     dsp->loop_filter_mix2[0][1][1] = ff_vp9_loop_filter_v_48_16_##opt; \
236     dsp->loop_filter_mix2[1][0][0] = ff_vp9_loop_filter_h_84_16_##opt; \
237     dsp->loop_filter_mix2[1][0][1] = ff_vp9_loop_filter_v_84_16_##opt; \
238     dsp->loop_filter_mix2[1][1][0] = ff_vp9_loop_filter_h_88_16_##opt; \
239     dsp->loop_filter_mix2[1][1][1] = ff_vp9_loop_filter_v_88_16_##opt; \
240 } while (0)
241
242 #define init_ipred(sz, opt, t, e) \
243     dsp->intra_pred[TX_##sz##X##sz][e##_PRED] = ff_vp9_ipred_##t##_##sz##x##sz##_##opt
244
245 #define ff_vp9_ipred_hd_4x4_ssse3 ff_vp9_ipred_hd_4x4_mmxext
246 #define ff_vp9_ipred_vl_4x4_ssse3 ff_vp9_ipred_vl_4x4_mmxext
247 #define init_dir_tm_ipred(sz, opt) do { \
248     init_ipred(sz, opt, dl, DIAG_DOWN_LEFT); \
249     init_ipred(sz, opt, dr, DIAG_DOWN_RIGHT); \
250     init_ipred(sz, opt, hd, HOR_DOWN); \
251     init_ipred(sz, opt, vl, VERT_LEFT); \
252     init_ipred(sz, opt, hu, HOR_UP); \
253     init_ipred(sz, opt, tm, TM_VP8); \
254     init_ipred(sz, opt, vr, VERT_RIGHT); \
255 } while (0)
256 #define init_dir_tm_h_ipred(sz, opt) do { \
257     init_dir_tm_ipred(sz, opt); \
258     init_ipred(sz, opt, h,  HOR); \
259 } while (0)
260 #define init_dc_ipred(sz, opt) do { \
261     init_ipred(sz, opt, dc,      DC); \
262     init_ipred(sz, opt, dc_left, LEFT_DC); \
263     init_ipred(sz, opt, dc_top,  TOP_DC); \
264 } while (0)
265 #define init_all_ipred(sz, opt) do { \
266     init_dc_ipred(sz, opt); \
267     init_dir_tm_h_ipred(sz, opt); \
268 } while (0)
269
270     if (EXTERNAL_MMX(cpu_flags)) {
271         init_fpel_func(4, 0,  4, put, , mmx);
272         init_fpel_func(3, 0,  8, put, , mmx);
273         if (!bitexact) {
274             dsp->itxfm_add[4 /* lossless */][DCT_DCT] =
275             dsp->itxfm_add[4 /* lossless */][ADST_DCT] =
276             dsp->itxfm_add[4 /* lossless */][DCT_ADST] =
277             dsp->itxfm_add[4 /* lossless */][ADST_ADST] = ff_vp9_iwht_iwht_4x4_add_mmx;
278         }
279         init_ipred(8, mmx, v, VERT);
280     }
281
282     if (EXTERNAL_MMXEXT(cpu_flags)) {
283         init_subpel2(4, 0, 4, put, 8, mmxext);
284         init_subpel2(4, 1, 4, avg, 8, mmxext);
285         init_fpel_func(4, 1,  4, avg, _8, mmxext);
286         init_fpel_func(3, 1,  8, avg, _8, mmxext);
287         dsp->itxfm_add[TX_4X4][DCT_DCT] = ff_vp9_idct_idct_4x4_add_mmxext;
288         init_dc_ipred(4, mmxext);
289         init_dc_ipred(8, mmxext);
290         init_dir_tm_ipred(4, mmxext);
291     }
292
293     if (EXTERNAL_SSE(cpu_flags)) {
294         init_fpel_func(2, 0, 16, put, , sse);
295         init_fpel_func(1, 0, 32, put, , sse);
296         init_fpel_func(0, 0, 64, put, , sse);
297         init_ipred(16, sse, v, VERT);
298         init_ipred(32, sse, v, VERT);
299     }
300
301     if (EXTERNAL_SSE2(cpu_flags)) {
302         init_subpel3_8to64(0, put, 8, sse2);
303         init_subpel3_8to64(1, avg, 8, sse2);
304         init_fpel_func(2, 1, 16, avg,  _8, sse2);
305         init_fpel_func(1, 1, 32, avg,  _8, sse2);
306         init_fpel_func(0, 1, 64, avg,  _8, sse2);
307         init_lpf(sse2);
308         dsp->itxfm_add[TX_4X4][ADST_DCT]  = ff_vp9_idct_iadst_4x4_add_sse2;
309         dsp->itxfm_add[TX_4X4][DCT_ADST]  = ff_vp9_iadst_idct_4x4_add_sse2;
310         dsp->itxfm_add[TX_4X4][ADST_ADST] = ff_vp9_iadst_iadst_4x4_add_sse2;
311         dsp->itxfm_add[TX_8X8][DCT_DCT] = ff_vp9_idct_idct_8x8_add_sse2;
312         dsp->itxfm_add[TX_8X8][ADST_DCT]  = ff_vp9_idct_iadst_8x8_add_sse2;
313         dsp->itxfm_add[TX_8X8][DCT_ADST]  = ff_vp9_iadst_idct_8x8_add_sse2;
314         dsp->itxfm_add[TX_8X8][ADST_ADST] = ff_vp9_iadst_iadst_8x8_add_sse2;
315         dsp->itxfm_add[TX_16X16][DCT_DCT]   = ff_vp9_idct_idct_16x16_add_sse2;
316         dsp->itxfm_add[TX_16X16][ADST_DCT]  = ff_vp9_idct_iadst_16x16_add_sse2;
317         dsp->itxfm_add[TX_16X16][DCT_ADST]  = ff_vp9_iadst_idct_16x16_add_sse2;
318         dsp->itxfm_add[TX_16X16][ADST_ADST] = ff_vp9_iadst_iadst_16x16_add_sse2;
319         dsp->itxfm_add[TX_32X32][ADST_ADST] =
320         dsp->itxfm_add[TX_32X32][ADST_DCT] =
321         dsp->itxfm_add[TX_32X32][DCT_ADST] =
322         dsp->itxfm_add[TX_32X32][DCT_DCT] = ff_vp9_idct_idct_32x32_add_sse2;
323         init_dc_ipred(16, sse2);
324         init_dc_ipred(32, sse2);
325         init_dir_tm_h_ipred(8, sse2);
326         init_dir_tm_h_ipred(16, sse2);
327         init_dir_tm_h_ipred(32, sse2);
328         init_ipred(4, sse2, h, HOR);
329     }
330
331     if (EXTERNAL_SSSE3(cpu_flags)) {
332         init_subpel3(0, put, 8, ssse3);
333         init_subpel3(1, avg, 8, ssse3);
334         dsp->itxfm_add[TX_4X4][DCT_DCT] = ff_vp9_idct_idct_4x4_add_ssse3;
335         dsp->itxfm_add[TX_4X4][ADST_DCT]  = ff_vp9_idct_iadst_4x4_add_ssse3;
336         dsp->itxfm_add[TX_4X4][DCT_ADST]  = ff_vp9_iadst_idct_4x4_add_ssse3;
337         dsp->itxfm_add[TX_4X4][ADST_ADST] = ff_vp9_iadst_iadst_4x4_add_ssse3;
338         dsp->itxfm_add[TX_8X8][DCT_DCT] = ff_vp9_idct_idct_8x8_add_ssse3;
339         dsp->itxfm_add[TX_8X8][ADST_DCT]  = ff_vp9_idct_iadst_8x8_add_ssse3;
340         dsp->itxfm_add[TX_8X8][DCT_ADST]  = ff_vp9_iadst_idct_8x8_add_ssse3;
341         dsp->itxfm_add[TX_8X8][ADST_ADST] = ff_vp9_iadst_iadst_8x8_add_ssse3;
342         dsp->itxfm_add[TX_16X16][DCT_DCT]   = ff_vp9_idct_idct_16x16_add_ssse3;
343         dsp->itxfm_add[TX_16X16][ADST_DCT]  = ff_vp9_idct_iadst_16x16_add_ssse3;
344         dsp->itxfm_add[TX_16X16][DCT_ADST]  = ff_vp9_iadst_idct_16x16_add_ssse3;
345         dsp->itxfm_add[TX_16X16][ADST_ADST] = ff_vp9_iadst_iadst_16x16_add_ssse3;
346         dsp->itxfm_add[TX_32X32][ADST_ADST] =
347         dsp->itxfm_add[TX_32X32][ADST_DCT] =
348         dsp->itxfm_add[TX_32X32][DCT_ADST] =
349         dsp->itxfm_add[TX_32X32][DCT_DCT] = ff_vp9_idct_idct_32x32_add_ssse3;
350         init_lpf(ssse3);
351         init_all_ipred(4, ssse3);
352         init_all_ipred(8, ssse3);
353         init_all_ipred(16, ssse3);
354         init_all_ipred(32, ssse3);
355     }
356
357     if (EXTERNAL_AVX(cpu_flags)) {
358         dsp->itxfm_add[TX_8X8][DCT_DCT] = ff_vp9_idct_idct_8x8_add_avx;
359         dsp->itxfm_add[TX_8X8][ADST_DCT]  = ff_vp9_idct_iadst_8x8_add_avx;
360         dsp->itxfm_add[TX_8X8][DCT_ADST]  = ff_vp9_iadst_idct_8x8_add_avx;
361         dsp->itxfm_add[TX_8X8][ADST_ADST] = ff_vp9_iadst_iadst_8x8_add_avx;
362         dsp->itxfm_add[TX_16X16][DCT_DCT] = ff_vp9_idct_idct_16x16_add_avx;
363         dsp->itxfm_add[TX_16X16][ADST_DCT]  = ff_vp9_idct_iadst_16x16_add_avx;
364         dsp->itxfm_add[TX_16X16][DCT_ADST]  = ff_vp9_iadst_idct_16x16_add_avx;
365         dsp->itxfm_add[TX_16X16][ADST_ADST] = ff_vp9_iadst_iadst_16x16_add_avx;
366         dsp->itxfm_add[TX_32X32][ADST_ADST] =
367         dsp->itxfm_add[TX_32X32][ADST_DCT] =
368         dsp->itxfm_add[TX_32X32][DCT_ADST] =
369         dsp->itxfm_add[TX_32X32][DCT_DCT] = ff_vp9_idct_idct_32x32_add_avx;
370         init_lpf(avx);
371         init_dir_tm_h_ipred(8, avx);
372         init_dir_tm_h_ipred(16, avx);
373         init_dir_tm_h_ipred(32, avx);
374     }
375     if (EXTERNAL_AVX_FAST(cpu_flags)) {
376         init_fpel_func(1, 0, 32, put, , avx);
377         init_fpel_func(0, 0, 64, put, , avx);
378         init_ipred(32, avx, v, VERT);
379     }
380
381     if (EXTERNAL_AVX2_FAST(cpu_flags)) {
382         init_fpel_func(1, 1, 32, avg, _8, avx2);
383         init_fpel_func(0, 1, 64, avg, _8, avx2);
384         if (ARCH_X86_64) {
385 #if ARCH_X86_64 && HAVE_AVX2_EXTERNAL
386             dsp->itxfm_add[TX_16X16][DCT_DCT] = ff_vp9_idct_idct_16x16_add_avx2;
387             init_subpel3_32_64(0, put, 8, avx2);
388             init_subpel3_32_64(1, avg, 8, avx2);
389 #endif
390         }
391         init_dc_ipred(32, avx2);
392         init_ipred(32, avx2, h,  HOR);
393         init_ipred(32, avx2, tm, TM_VP8);
394     }
395
396 #undef init_fpel
397 #undef init_subpel1
398 #undef init_subpel2
399 #undef init_subpel3
400
401 #endif /* HAVE_YASM */
402 }