2 * MMX optimized DSP utils
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
22 * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
25 #include "libavutil/cpu.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavcodec/hpeldsp.h"
28 #include "dsputil_mmx.h"
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);
80 /***********************************/
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)
89 #include "hpeldsp_rnd_template.c"
96 /***********************************/
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)
104 #include "hpeldsp_rnd_template.c"
112 #endif /* HAVE_INLINE_ASM */
116 #define ff_put_pixels8_mmx ff_put_pixels8_mmxext
118 /***********************************/
121 #define DEF(x) x ## _3dnow
123 #include "hpeldsp_avg_template.c"
127 /***********************************/
128 /* MMXEXT specific */
130 #define DEF(x) x ## _mmxext
132 #include "hpeldsp_avg_template.c"
136 #endif /* HAVE_YASM */
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
148 static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels,
149 ptrdiff_t line_size, int h)
152 "lea (%3, %3), %%"REG_a" \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"
169 : "+g"(h), "+r"(pixels), "+r"(block)
170 : "r"((x86_reg)line_size)
175 static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels,
176 ptrdiff_t line_size, int h)
179 "lea (%3, %3), %%"REG_a" \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"
204 : "+g"(h), "+r"(pixels), "+r"(block)
205 : "r"((x86_reg)line_size)
209 #endif /* HAVE_INLINE_ASM */
211 #define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU) \
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; \
219 static void hpeldsp_init_mmx(HpelDSPContext *c, int flags, int mm_flags)
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 */
232 static void hpeldsp_init_mmxext(HpelDSPContext *c, int flags, int mm_flags)
235 c->put_pixels_tab[0][1] = ff_put_pixels16_x2_mmxext;
236 c->put_pixels_tab[0][2] = ff_put_pixels16_y2_mmxext;
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;
242 c->put_pixels_tab[1][1] = ff_put_pixels8_x2_mmxext;
243 c->put_pixels_tab[1][2] = ff_put_pixels8_y2_mmxext;
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;
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;
255 c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_mmxext;
256 c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_mmxext;
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;
263 #endif /* HAVE_YASM */
266 static void hpeldsp_init_3dnow(HpelDSPContext *c, int flags, int mm_flags)
269 c->put_pixels_tab[0][1] = ff_put_pixels16_x2_3dnow;
270 c->put_pixels_tab[0][2] = ff_put_pixels16_y2_3dnow;
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;
276 c->put_pixels_tab[1][1] = ff_put_pixels8_x2_3dnow;
277 c->put_pixels_tab[1][2] = ff_put_pixels8_y2_3dnow;
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;
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;
289 c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_3dnow;
290 c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_3dnow;
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;
297 #endif /* HAVE_YASM */
300 static void hpeldsp_init_sse2(HpelDSPContext *c, int flags, int mm_flags)
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;
309 #endif /* HAVE_YASM */
312 void ff_hpeldsp_init_x86(HpelDSPContext *c, int flags)
314 int mm_flags = av_get_cpu_flags();
316 if (HAVE_MMX && mm_flags & AV_CPU_FLAG_MMX)
317 hpeldsp_init_mmx(c, flags, mm_flags);
319 if (mm_flags & AV_CPU_FLAG_MMXEXT)
320 hpeldsp_init_mmxext(c, flags, mm_flags);
322 if (mm_flags & AV_CPU_FLAG_3DNOW)
323 hpeldsp_init_3dnow(c, flags, mm_flags);
325 if (mm_flags & AV_CPU_FLAG_SSE2)
326 hpeldsp_init_sse2(c, flags, mm_flags);