]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/hpeldsp_init.c
x86: avcodec: Use convenience macros to check for CPU flags
[ffmpeg] / libavcodec / x86 / hpeldsp_init.c
1 /*
2  * MMX optimized DSP utils
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
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  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
23  */
24
25 #include "libavutil/cpu.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavutil/x86/cpu.h"
28 #include "libavcodec/hpeldsp.h"
29 #include "dsputil_x86.h"
30
31 void ff_put_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
32                               ptrdiff_t line_size, int h);
33 void ff_put_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
34                              ptrdiff_t line_size, int h);
35 void ff_put_pixels16_x2_mmxext(uint8_t *block, const uint8_t *pixels,
36                                ptrdiff_t line_size, int h);
37 void ff_put_pixels16_x2_3dnow(uint8_t *block, const uint8_t *pixels,
38                               ptrdiff_t line_size, int h);
39 void ff_put_no_rnd_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
40                                      ptrdiff_t line_size, int h);
41 void ff_put_no_rnd_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
42                                     ptrdiff_t line_size, int h);
43 void ff_put_no_rnd_pixels8_x2_exact_mmxext(uint8_t *block,
44                                            const uint8_t *pixels,
45                                            ptrdiff_t line_size, int h);
46 void ff_put_no_rnd_pixels8_x2_exact_3dnow(uint8_t *block,
47                                           const uint8_t *pixels,
48                                           ptrdiff_t line_size, int h);
49 void ff_put_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
50                               ptrdiff_t line_size, int h);
51 void ff_put_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
52                              ptrdiff_t line_size, int h);
53 void ff_put_no_rnd_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
54                                      ptrdiff_t line_size, int h);
55 void ff_put_no_rnd_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
56                                     ptrdiff_t line_size, int h);
57 void ff_put_no_rnd_pixels8_y2_exact_mmxext(uint8_t *block,
58                                            const uint8_t *pixels,
59                                            ptrdiff_t line_size, int h);
60 void ff_put_no_rnd_pixels8_y2_exact_3dnow(uint8_t *block,
61                                           const uint8_t *pixels,
62                                           ptrdiff_t line_size, int h);
63 void ff_avg_pixels8_3dnow(uint8_t *block, const uint8_t *pixels,
64                           ptrdiff_t line_size, int h);
65 void ff_avg_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
66                               ptrdiff_t line_size, int h);
67 void ff_avg_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
68                              ptrdiff_t line_size, int h);
69 void ff_avg_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
70                               ptrdiff_t line_size, int h);
71 void ff_avg_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
72                              ptrdiff_t line_size, int h);
73 void ff_avg_pixels8_xy2_mmxext(uint8_t *block, const uint8_t *pixels,
74                                ptrdiff_t line_size, int h);
75 void ff_avg_pixels8_xy2_3dnow(uint8_t *block, const uint8_t *pixels,
76                               ptrdiff_t line_size, int h);
77
78 #define avg_pixels8_mmx         ff_avg_pixels8_mmx
79 #define avg_pixels8_x2_mmx      ff_avg_pixels8_x2_mmx
80 #define avg_pixels16_mmx        ff_avg_pixels16_mmx
81 #define avg_pixels8_xy2_mmx     ff_avg_pixels8_xy2_mmx
82 #define avg_pixels16_xy2_mmx    ff_avg_pixels16_xy2_mmx
83 #define put_pixels8_mmx         ff_put_pixels8_mmx
84 #define put_pixels16_mmx        ff_put_pixels16_mmx
85 #define put_pixels8_xy2_mmx     ff_put_pixels8_xy2_mmx
86 #define put_pixels16_xy2_mmx    ff_put_pixels16_xy2_mmx
87 #define avg_no_rnd_pixels16_mmx ff_avg_pixels16_mmx
88 #define put_no_rnd_pixels8_mmx  ff_put_pixels8_mmx
89 #define put_no_rnd_pixels16_mmx ff_put_pixels16_mmx
90
91 #if HAVE_INLINE_ASM
92
93 /***********************************/
94 /* MMX no rounding */
95 #define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
96 #define SET_RND  MOVQ_WONE
97 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
98 #define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
99 #define STATIC static
100
101 #include "rnd_template.c"
102 #include "hpeldsp_rnd_template.c"
103
104 #undef DEF
105 #undef SET_RND
106 #undef PAVGBP
107 #undef PAVGB
108 #undef STATIC
109
110 PIXELS16(static, avg_no_rnd, , _y2, _mmx)
111 PIXELS16(static, put_no_rnd, , _y2, _mmx)
112
113 PIXELS16(static, avg_no_rnd, , _xy2, _mmx)
114 PIXELS16(static, put_no_rnd, , _xy2, _mmx)
115
116 /***********************************/
117 /* MMX rounding */
118
119 #define DEF(x, y) x ## _ ## y ## _mmx
120 #define SET_RND  MOVQ_WTWO
121 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
122 #define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
123
124 #include "hpeldsp_rnd_template.c"
125
126 #undef DEF
127 #undef SET_RND
128 #undef PAVGBP
129 #undef PAVGB
130
131 PIXELS16(static, avg, , _y2, _mmx)
132 PIXELS16(static, put, , _y2, _mmx)
133
134 #endif /* HAVE_INLINE_ASM */
135
136
137 #if HAVE_YASM
138
139 #define HPELDSP_AVG_PIXELS16(CPUEXT)                \
140     PIXELS16(static, put_no_rnd, ff_,  _x2, CPUEXT) \
141     PIXELS16(static, put,        ff_,  _y2, CPUEXT) \
142     PIXELS16(static, put_no_rnd, ff_,  _y2, CPUEXT) \
143     PIXELS16(static, avg,        ff_,     , CPUEXT) \
144     PIXELS16(static, avg,        ff_,  _x2, CPUEXT) \
145     PIXELS16(static, avg,        ff_,  _y2, CPUEXT) \
146     PIXELS16(static, avg,        ff_, _xy2, CPUEXT)
147
148 HPELDSP_AVG_PIXELS16(_3dnow)
149 HPELDSP_AVG_PIXELS16(_mmxext)
150
151 #endif /* HAVE_YASM */
152
153 #define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU)                                     \
154     do {                                                                        \
155         c->PFX ## _pixels_tab IDX [0] = PFX ## _pixels ## SIZE ## _     ## CPU; \
156         c->PFX ## _pixels_tab IDX [1] = PFX ## _pixels ## SIZE ## _x2_  ## CPU; \
157         c->PFX ## _pixels_tab IDX [2] = PFX ## _pixels ## SIZE ## _y2_  ## CPU; \
158         c->PFX ## _pixels_tab IDX [3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
159     } while (0)
160
161 static void hpeldsp_init_mmx(HpelDSPContext *c, int flags, int cpu_flags)
162 {
163 #if HAVE_MMX_INLINE
164     SET_HPEL_FUNCS(put,        [0], 16, mmx);
165     SET_HPEL_FUNCS(put_no_rnd, [0], 16, mmx);
166     SET_HPEL_FUNCS(avg,        [0], 16, mmx);
167     SET_HPEL_FUNCS(avg_no_rnd,    , 16, mmx);
168     SET_HPEL_FUNCS(put,        [1],  8, mmx);
169     SET_HPEL_FUNCS(put_no_rnd, [1],  8, mmx);
170     SET_HPEL_FUNCS(avg,        [1],  8, mmx);
171 #endif /* HAVE_MMX_INLINE */
172 }
173
174 static void hpeldsp_init_mmxext(HpelDSPContext *c, int flags, int cpu_flags)
175 {
176 #if HAVE_MMXEXT_EXTERNAL
177     c->put_pixels_tab[0][1] = ff_put_pixels16_x2_mmxext;
178     c->put_pixels_tab[0][2] = put_pixels16_y2_mmxext;
179
180     c->avg_pixels_tab[0][0] = avg_pixels16_mmxext;
181     c->avg_pixels_tab[0][1] = avg_pixels16_x2_mmxext;
182     c->avg_pixels_tab[0][2] = avg_pixels16_y2_mmxext;
183
184     c->put_pixels_tab[1][1] = ff_put_pixels8_x2_mmxext;
185     c->put_pixels_tab[1][2] = ff_put_pixels8_y2_mmxext;
186
187     c->avg_pixels_tab[1][0] = ff_avg_pixels8_mmxext;
188     c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_mmxext;
189     c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_mmxext;
190
191     if (!(flags & CODEC_FLAG_BITEXACT)) {
192         c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_mmxext;
193         c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_mmxext;
194         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_mmxext;
195         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_mmxext;
196
197         c->avg_pixels_tab[0][3] = avg_pixels16_xy2_mmxext;
198         c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_mmxext;
199     }
200
201     if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
202         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_mmxext;
203         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_mmxext;
204     }
205 #endif /* HAVE_MMXEXT_EXTERNAL */
206 }
207
208 static void hpeldsp_init_3dnow(HpelDSPContext *c, int flags, int cpu_flags)
209 {
210 #if HAVE_AMD3DNOW_EXTERNAL
211     c->put_pixels_tab[0][1] = ff_put_pixels16_x2_3dnow;
212     c->put_pixels_tab[0][2] = put_pixels16_y2_3dnow;
213
214     c->avg_pixels_tab[0][0] = avg_pixels16_3dnow;
215     c->avg_pixels_tab[0][1] = avg_pixels16_x2_3dnow;
216     c->avg_pixels_tab[0][2] = avg_pixels16_y2_3dnow;
217
218     c->put_pixels_tab[1][1] = ff_put_pixels8_x2_3dnow;
219     c->put_pixels_tab[1][2] = ff_put_pixels8_y2_3dnow;
220
221     c->avg_pixels_tab[1][0] = ff_avg_pixels8_3dnow;
222     c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_3dnow;
223     c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_3dnow;
224
225     if (!(flags & CODEC_FLAG_BITEXACT)){
226         c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_3dnow;
227         c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_3dnow;
228         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_3dnow;
229         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_3dnow;
230
231         c->avg_pixels_tab[0][3] = avg_pixels16_xy2_3dnow;
232         c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_3dnow;
233     }
234
235     if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
236         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_3dnow;
237         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_3dnow;
238     }
239 #endif /* HAVE_AMD3DNOW_EXTERNAL */
240 }
241
242 static void hpeldsp_init_sse2(HpelDSPContext *c, int flags, int cpu_flags)
243 {
244 #if HAVE_SSE2_EXTERNAL
245     if (!(cpu_flags & AV_CPU_FLAG_SSE2SLOW)) {
246         // these functions are slower than mmx on AMD, but faster on Intel
247         c->put_pixels_tab[0][0]        = ff_put_pixels16_sse2;
248         c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_sse2;
249         c->avg_pixels_tab[0][0]        = ff_avg_pixels16_sse2;
250     }
251 #endif /* HAVE_SSE2_EXTERNAL */
252 }
253
254 void ff_hpeldsp_init_x86(HpelDSPContext *c, int flags)
255 {
256     int cpu_flags = av_get_cpu_flags();
257
258     if (INLINE_MMX(cpu_flags))
259         hpeldsp_init_mmx(c, flags, cpu_flags);
260
261     if (EXTERNAL_MMXEXT(cpu_flags))
262         hpeldsp_init_mmxext(c, flags, cpu_flags);
263
264     if (EXTERNAL_AMD3DNOW(cpu_flags))
265         hpeldsp_init_3dnow(c, flags, cpu_flags);
266
267     if (EXTERNAL_SSE2(cpu_flags))
268         hpeldsp_init_sse2(c, flags, cpu_flags);
269 }