]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/hpeldsp_init.c
e313265f2a9b07f065a8cd26f402f80290b8343b
[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 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  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
23  */
24
25 #include "libavutil/cpu.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavcodec/hpeldsp.h"
28 #include "dsputil_mmx.h"
29
30 void ff_put_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
31                               ptrdiff_t line_size, int h);
32 void ff_put_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
33                              ptrdiff_t line_size, int h);
34 void ff_put_pixels16_x2_mmxext(uint8_t *block, const uint8_t *pixels,
35                                ptrdiff_t line_size, int h);
36 void ff_put_pixels16_x2_3dnow(uint8_t *block, const uint8_t *pixels,
37                               ptrdiff_t line_size, int h);
38 void ff_put_no_rnd_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
39                                      ptrdiff_t line_size, int h);
40 void ff_put_no_rnd_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
41                                     ptrdiff_t line_size, int h);
42 void ff_put_no_rnd_pixels8_x2_exact_mmxext(uint8_t *block,
43                                            const uint8_t *pixels,
44                                            ptrdiff_t line_size, int h);
45 void ff_put_no_rnd_pixels8_x2_exact_3dnow(uint8_t *block,
46                                           const uint8_t *pixels,
47                                           ptrdiff_t line_size, int h);
48 void ff_put_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
49                               ptrdiff_t line_size, int h);
50 void ff_put_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
51                              ptrdiff_t line_size, int h);
52 void ff_put_no_rnd_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
53                                      ptrdiff_t line_size, int h);
54 void ff_put_no_rnd_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
55                                     ptrdiff_t line_size, int h);
56 void ff_put_no_rnd_pixels8_y2_exact_mmxext(uint8_t *block,
57                                            const uint8_t *pixels,
58                                            ptrdiff_t line_size, int h);
59 void ff_put_no_rnd_pixels8_y2_exact_3dnow(uint8_t *block,
60                                           const uint8_t *pixels,
61                                           ptrdiff_t line_size, int h);
62 void ff_avg_pixels8_3dnow(uint8_t *block, const uint8_t *pixels,
63                           ptrdiff_t line_size, int h);
64 void ff_avg_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
65                               ptrdiff_t line_size, int h);
66 void ff_avg_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
67                              ptrdiff_t line_size, int h);
68 void ff_avg_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
69                               ptrdiff_t line_size, int h);
70 void ff_avg_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
71                              ptrdiff_t line_size, int h);
72 void ff_avg_pixels8_xy2_mmxext(uint8_t *block, const uint8_t *pixels,
73                                ptrdiff_t line_size, int h);
74 void ff_avg_pixels8_xy2_3dnow(uint8_t *block, const uint8_t *pixels,
75                               ptrdiff_t line_size, int h);
76
77
78 #if HAVE_INLINE_ASM
79
80 /***********************************/
81 /* MMX no rounding */
82 #define NO_RND 1
83 #define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
84 #define SET_RND  MOVQ_WONE
85 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
86 #define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
87 #define OP_AVG(a, b, c, e)              PAVGB_MMX(a, b, c, e)
88
89 #include "hpeldsp_rnd_template.c"
90
91 #undef DEF
92 #undef SET_RND
93 #undef PAVGBP
94 #undef PAVGB
95 #undef NO_RND
96 /***********************************/
97 /* MMX rounding */
98
99 #define DEF(x, y) x ## _ ## y ## _mmx
100 #define SET_RND  MOVQ_WTWO
101 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
102 #define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
103
104 #include "hpeldsp_rnd_template.c"
105
106 #undef DEF
107 #undef SET_RND
108 #undef PAVGBP
109 #undef PAVGB
110 #undef OP_AVG
111
112 #endif /* HAVE_INLINE_ASM */
113
114
115 #if HAVE_YASM
116 #define ff_put_pixels8_mmx ff_put_pixels8_mmxext
117
118 /***********************************/
119 /* 3Dnow specific */
120
121 #define DEF(x) x ## _3dnow
122
123 #include "hpeldsp_avg_template.c"
124
125 #undef DEF
126
127 /***********************************/
128 /* MMXEXT specific */
129
130 #define DEF(x) x ## _mmxext
131
132 #include "hpeldsp_avg_template.c"
133
134 #undef DEF
135
136 #endif /* HAVE_YASM */
137
138
139 #if HAVE_INLINE_ASM
140 #define put_no_rnd_pixels16_mmx put_pixels16_mmx
141 #define put_no_rnd_pixels8_mmx put_pixels8_mmx
142 #define put_pixels16_mmxext put_pixels16_mmx
143 #define put_pixels8_mmxext put_pixels8_mmx
144 #define put_pixels4_mmxext put_pixels4_mmx
145 #define put_no_rnd_pixels16_mmxext put_no_rnd_pixels16_mmx
146 #define put_no_rnd_pixels8_mmxext put_no_rnd_pixels8_mmx
147
148 static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels,
149                             ptrdiff_t line_size, int h)
150 {
151     __asm__ volatile (
152         "lea   (%3, %3), %%"REG_a"      \n\t"
153         ".p2align     3                 \n\t"
154         "1:                             \n\t"
155         "movq  (%1    ), %%mm0          \n\t"
156         "movq  (%1, %3), %%mm1          \n\t"
157         "movq     %%mm0, (%2)           \n\t"
158         "movq     %%mm1, (%2, %3)       \n\t"
159         "add  %%"REG_a", %1             \n\t"
160         "add  %%"REG_a", %2             \n\t"
161         "movq  (%1    ), %%mm0          \n\t"
162         "movq  (%1, %3), %%mm1          \n\t"
163         "movq     %%mm0, (%2)           \n\t"
164         "movq     %%mm1, (%2, %3)       \n\t"
165         "add  %%"REG_a", %1             \n\t"
166         "add  %%"REG_a", %2             \n\t"
167         "subl        $4, %0             \n\t"
168         "jnz         1b                 \n\t"
169         : "+g"(h), "+r"(pixels),  "+r"(block)
170         : "r"((x86_reg)line_size)
171         : "%"REG_a, "memory"
172         );
173 }
174
175 static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels,
176                              ptrdiff_t line_size, int h)
177 {
178     __asm__ volatile (
179         "lea   (%3, %3), %%"REG_a"      \n\t"
180         ".p2align     3                 \n\t"
181         "1:                             \n\t"
182         "movq  (%1    ), %%mm0          \n\t"
183         "movq 8(%1    ), %%mm4          \n\t"
184         "movq  (%1, %3), %%mm1          \n\t"
185         "movq 8(%1, %3), %%mm5          \n\t"
186         "movq     %%mm0,  (%2)          \n\t"
187         "movq     %%mm4, 8(%2)          \n\t"
188         "movq     %%mm1,  (%2, %3)      \n\t"
189         "movq     %%mm5, 8(%2, %3)      \n\t"
190         "add  %%"REG_a", %1             \n\t"
191         "add  %%"REG_a", %2             \n\t"
192         "movq  (%1    ), %%mm0          \n\t"
193         "movq 8(%1    ), %%mm4          \n\t"
194         "movq  (%1, %3), %%mm1          \n\t"
195         "movq 8(%1, %3), %%mm5          \n\t"
196         "movq     %%mm0,  (%2)          \n\t"
197         "movq     %%mm4, 8(%2)          \n\t"
198         "movq     %%mm1,  (%2, %3)      \n\t"
199         "movq     %%mm5, 8(%2, %3)      \n\t"
200         "add  %%"REG_a", %1             \n\t"
201         "add  %%"REG_a", %2             \n\t"
202         "subl        $4, %0             \n\t"
203         "jnz         1b                 \n\t"
204         : "+g"(h), "+r"(pixels),  "+r"(block)
205         : "r"((x86_reg)line_size)
206         : "%"REG_a, "memory"
207         );
208 }
209 #endif /* HAVE_INLINE_ASM */
210
211 #define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU)                                     \
212     do {                                                                        \
213         c->PFX ## _pixels_tab IDX [0] = PFX ## _pixels ## SIZE ## _     ## CPU; \
214         c->PFX ## _pixels_tab IDX [1] = PFX ## _pixels ## SIZE ## _x2_  ## CPU; \
215         c->PFX ## _pixels_tab IDX [2] = PFX ## _pixels ## SIZE ## _y2_  ## CPU; \
216         c->PFX ## _pixels_tab IDX [3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
217     } while (0)
218
219 static void hpeldsp_init_mmx(HpelDSPContext *c, int flags, int mm_flags)
220 {
221 #if HAVE_INLINE_ASM
222     SET_HPEL_FUNCS(put,        [0], 16, mmx);
223     SET_HPEL_FUNCS(put_no_rnd, [0], 16, mmx);
224     SET_HPEL_FUNCS(avg,        [0], 16, mmx);
225     SET_HPEL_FUNCS(avg_no_rnd,    , 16, mmx);
226     SET_HPEL_FUNCS(put,        [1],  8, mmx);
227     SET_HPEL_FUNCS(put_no_rnd, [1],  8, mmx);
228     SET_HPEL_FUNCS(avg,        [1],  8, mmx);
229 #endif /* HAVE_INLINE_ASM */
230 }
231
232 static void hpeldsp_init_mmxext(HpelDSPContext *c, int flags, int mm_flags)
233 {
234 #if HAVE_YASM
235     c->put_pixels_tab[0][1] = ff_put_pixels16_x2_mmxext;
236     c->put_pixels_tab[0][2] = ff_put_pixels16_y2_mmxext;
237
238     c->avg_pixels_tab[0][0] = ff_avg_pixels16_mmxext;
239     c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_mmxext;
240     c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_mmxext;
241
242     c->put_pixels_tab[1][1] = ff_put_pixels8_x2_mmxext;
243     c->put_pixels_tab[1][2] = ff_put_pixels8_y2_mmxext;
244
245     c->avg_pixels_tab[1][0] = ff_avg_pixels8_mmxext;
246     c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_mmxext;
247     c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_mmxext;
248
249     if (!(flags & CODEC_FLAG_BITEXACT)) {
250         c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_mmxext;
251         c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_mmxext;
252         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_mmxext;
253         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_mmxext;
254
255         c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_mmxext;
256         c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_mmxext;
257     }
258
259     if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
260         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_mmxext;
261         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_mmxext;
262     }
263 #endif /* HAVE_YASM */
264 }
265
266 static void hpeldsp_init_3dnow(HpelDSPContext *c, int flags, int mm_flags)
267 {
268 #if HAVE_YASM
269     c->put_pixels_tab[0][1] = ff_put_pixels16_x2_3dnow;
270     c->put_pixels_tab[0][2] = ff_put_pixels16_y2_3dnow;
271
272     c->avg_pixels_tab[0][0] = ff_avg_pixels16_3dnow;
273     c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_3dnow;
274     c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_3dnow;
275
276     c->put_pixels_tab[1][1] = ff_put_pixels8_x2_3dnow;
277     c->put_pixels_tab[1][2] = ff_put_pixels8_y2_3dnow;
278
279     c->avg_pixels_tab[1][0] = ff_avg_pixels8_3dnow;
280     c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_3dnow;
281     c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_3dnow;
282
283     if (!(flags & CODEC_FLAG_BITEXACT)){
284         c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_3dnow;
285         c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_3dnow;
286         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_3dnow;
287         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_3dnow;
288
289         c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_3dnow;
290         c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_3dnow;
291     }
292
293     if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
294         c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_3dnow;
295         c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_3dnow;
296     }
297 #endif /* HAVE_YASM */
298 }
299
300 static void hpeldsp_init_sse2(HpelDSPContext *c, int flags, int mm_flags)
301 {
302 #if HAVE_YASM
303     if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
304         // these functions are slower than mmx on AMD, but faster on Intel
305         c->put_pixels_tab[0][0]        = ff_put_pixels16_sse2;
306         c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_sse2;
307         c->avg_pixels_tab[0][0]        = ff_avg_pixels16_sse2;
308     }
309 #endif /* HAVE_YASM */
310 }
311
312 void ff_hpeldsp_init_x86(HpelDSPContext *c, int flags)
313 {
314     int mm_flags = av_get_cpu_flags();
315
316     if (HAVE_MMX && mm_flags & AV_CPU_FLAG_MMX)
317         hpeldsp_init_mmx(c, flags, mm_flags);
318
319     if (mm_flags & AV_CPU_FLAG_MMXEXT)
320         hpeldsp_init_mmxext(c, flags, mm_flags);
321
322     if (mm_flags & AV_CPU_FLAG_3DNOW)
323         hpeldsp_init_3dnow(c, flags, mm_flags);
324
325     if (mm_flags & AV_CPU_FLAG_SSE2)
326         hpeldsp_init_sse2(c, flags, mm_flags);
327 }