hpeldsp_init.c
Go to the documentation of this file.
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);
43  const uint8_t *pixels,
44  ptrdiff_t line_size, int h);
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);
57  const uint8_t *pixels,
58  ptrdiff_t line_size, int h);
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 #define JUMPALIGN() __asm__ volatile (".p2align 3"::)
81 #define MOVQ_ZERO(regd) __asm__ volatile ("pxor %%"#regd", %%"#regd ::)
82 
83 #define MOVQ_BFE(regd) \
84  __asm__ volatile ( \
85  "pcmpeqd %%"#regd", %%"#regd" \n\t" \
86  "paddb %%"#regd", %%"#regd" \n\t" ::)
87 
88 #ifndef PIC
89 #define MOVQ_BONE(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_bone))
90 #define MOVQ_WTWO(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_wtwo))
91 #else
92 // for shared library it's better to use this way for accessing constants
93 // pcmpeqd -> -1
94 #define MOVQ_BONE(regd) \
95  __asm__ volatile ( \
96  "pcmpeqd %%"#regd", %%"#regd" \n\t" \
97  "psrlw $15, %%"#regd" \n\t" \
98  "packuswb %%"#regd", %%"#regd" \n\t" ::)
99 
100 #define MOVQ_WTWO(regd) \
101  __asm__ volatile ( \
102  "pcmpeqd %%"#regd", %%"#regd" \n\t" \
103  "psrlw $15, %%"#regd" \n\t" \
104  "psllw $1, %%"#regd" \n\t"::)
105 
106 #endif
107 
108 // using regr as temporary and for the output result
109 // first argument is unmodifed and second is trashed
110 // regfe is supposed to contain 0xfefefefefefefefe
111 #define PAVGB_MMX_NO_RND(rega, regb, regr, regfe) \
112  "movq "#rega", "#regr" \n\t" \
113  "pand "#regb", "#regr" \n\t" \
114  "pxor "#rega", "#regb" \n\t" \
115  "pand "#regfe", "#regb" \n\t" \
116  "psrlq $1, "#regb" \n\t" \
117  "paddb "#regb", "#regr" \n\t"
118 
119 #define PAVGB_MMX(rega, regb, regr, regfe) \
120  "movq "#rega", "#regr" \n\t" \
121  "por "#regb", "#regr" \n\t" \
122  "pxor "#rega", "#regb" \n\t" \
123  "pand "#regfe", "#regb" \n\t" \
124  "psrlq $1, "#regb" \n\t" \
125  "psubb "#regb", "#regr" \n\t"
126 
127 // mm6 is supposed to contain 0xfefefefefefefefe
128 #define PAVGBP_MMX_NO_RND(rega, regb, regr, regc, regd, regp) \
129  "movq "#rega", "#regr" \n\t" \
130  "movq "#regc", "#regp" \n\t" \
131  "pand "#regb", "#regr" \n\t" \
132  "pand "#regd", "#regp" \n\t" \
133  "pxor "#rega", "#regb" \n\t" \
134  "pxor "#regc", "#regd" \n\t" \
135  "pand %%mm6, "#regb" \n\t" \
136  "pand %%mm6, "#regd" \n\t" \
137  "psrlq $1, "#regb" \n\t" \
138  "psrlq $1, "#regd" \n\t" \
139  "paddb "#regb", "#regr" \n\t" \
140  "paddb "#regd", "#regp" \n\t"
141 
142 #define PAVGBP_MMX(rega, regb, regr, regc, regd, regp) \
143  "movq "#rega", "#regr" \n\t" \
144  "movq "#regc", "#regp" \n\t" \
145  "por "#regb", "#regr" \n\t" \
146  "por "#regd", "#regp" \n\t" \
147  "pxor "#rega", "#regb" \n\t" \
148  "pxor "#regc", "#regd" \n\t" \
149  "pand %%mm6, "#regb" \n\t" \
150  "pand %%mm6, "#regd" \n\t" \
151  "psrlq $1, "#regd" \n\t" \
152  "psrlq $1, "#regb" \n\t" \
153  "psubb "#regb", "#regr" \n\t" \
154  "psubb "#regd", "#regp" \n\t"
155 
156 /***********************************/
157 /* MMX no rounding */
158 #define NO_RND 1
159 #define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
160 #define SET_RND MOVQ_WONE
161 #define PAVGBP(a, b, c, d, e, f) PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
162 #define PAVGB(a, b, c, e) PAVGB_MMX_NO_RND(a, b, c, e)
163 #define OP_AVG(a, b, c, e) PAVGB_MMX(a, b, c, e)
164 
165 #include "hpeldsp_rnd_template.c"
166 
167 #undef DEF
168 #undef SET_RND
169 #undef PAVGBP
170 #undef PAVGB
171 #undef NO_RND
172 /***********************************/
173 /* MMX rounding */
174 
175 #define DEF(x, y) x ## _ ## y ## _mmx
176 #define SET_RND MOVQ_WTWO
177 #define PAVGBP(a, b, c, d, e, f) PAVGBP_MMX(a, b, c, d, e, f)
178 #define PAVGB(a, b, c, e) PAVGB_MMX(a, b, c, e)
179 
180 #include "hpeldsp_rnd_template.c"
181 
182 #undef DEF
183 #undef SET_RND
184 #undef PAVGBP
185 #undef PAVGB
186 #undef OP_AVG
187 
188 #endif /* HAVE_INLINE_ASM */
189 
190 
191 #if HAVE_YASM
192 #define ff_put_pixels8_mmx ff_put_pixels8_mmxext
193 
194 /***********************************/
195 /* 3Dnow specific */
196 
197 #define DEF(x) x ## _3dnow
198 
199 #include "hpeldsp_avg_template.c"
200 
201 #undef DEF
202 
203 /***********************************/
204 /* MMXEXT specific */
205 
206 #define DEF(x) x ## _mmxext
207 
208 #include "hpeldsp_avg_template.c"
209 
210 #undef DEF
211 
212 #endif /* HAVE_YASM */
213 
214 
215 #if HAVE_INLINE_ASM
216 #define put_no_rnd_pixels16_mmx put_pixels16_mmx
217 #define put_no_rnd_pixels8_mmx put_pixels8_mmx
218 #define put_pixels16_mmxext put_pixels16_mmx
219 #define put_pixels8_mmxext put_pixels8_mmx
220 #define put_pixels4_mmxext put_pixels4_mmx
221 #define put_no_rnd_pixels16_mmxext put_no_rnd_pixels16_mmx
222 #define put_no_rnd_pixels8_mmxext put_no_rnd_pixels8_mmx
223 
224 static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels,
225  ptrdiff_t line_size, int h)
226 {
227  __asm__ volatile (
228  "lea (%3, %3), %%"REG_a" \n\t"
229  ".p2align 3 \n\t"
230  "1: \n\t"
231  "movq (%1 ), %%mm0 \n\t"
232  "movq (%1, %3), %%mm1 \n\t"
233  "movq %%mm0, (%2) \n\t"
234  "movq %%mm1, (%2, %3) \n\t"
235  "add %%"REG_a", %1 \n\t"
236  "add %%"REG_a", %2 \n\t"
237  "movq (%1 ), %%mm0 \n\t"
238  "movq (%1, %3), %%mm1 \n\t"
239  "movq %%mm0, (%2) \n\t"
240  "movq %%mm1, (%2, %3) \n\t"
241  "add %%"REG_a", %1 \n\t"
242  "add %%"REG_a", %2 \n\t"
243  "subl $4, %0 \n\t"
244  "jnz 1b \n\t"
245  : "+g"(h), "+r"(pixels), "+r"(block)
246  : "r"((x86_reg)line_size)
247  : "%"REG_a, "memory"
248  );
249 }
250 
251 static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels,
252  ptrdiff_t line_size, int h)
253 {
254  __asm__ volatile (
255  "lea (%3, %3), %%"REG_a" \n\t"
256  ".p2align 3 \n\t"
257  "1: \n\t"
258  "movq (%1 ), %%mm0 \n\t"
259  "movq 8(%1 ), %%mm4 \n\t"
260  "movq (%1, %3), %%mm1 \n\t"
261  "movq 8(%1, %3), %%mm5 \n\t"
262  "movq %%mm0, (%2) \n\t"
263  "movq %%mm4, 8(%2) \n\t"
264  "movq %%mm1, (%2, %3) \n\t"
265  "movq %%mm5, 8(%2, %3) \n\t"
266  "add %%"REG_a", %1 \n\t"
267  "add %%"REG_a", %2 \n\t"
268  "movq (%1 ), %%mm0 \n\t"
269  "movq 8(%1 ), %%mm4 \n\t"
270  "movq (%1, %3), %%mm1 \n\t"
271  "movq 8(%1, %3), %%mm5 \n\t"
272  "movq %%mm0, (%2) \n\t"
273  "movq %%mm4, 8(%2) \n\t"
274  "movq %%mm1, (%2, %3) \n\t"
275  "movq %%mm5, 8(%2, %3) \n\t"
276  "add %%"REG_a", %1 \n\t"
277  "add %%"REG_a", %2 \n\t"
278  "subl $4, %0 \n\t"
279  "jnz 1b \n\t"
280  : "+g"(h), "+r"(pixels), "+r"(block)
281  : "r"((x86_reg)line_size)
282  : "%"REG_a, "memory"
283  );
284 }
285 #endif /* HAVE_INLINE_ASM */
286 
287 void ff_put_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
288  ptrdiff_t line_size, int h);
289 void ff_avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
290  ptrdiff_t line_size, int h);
291 
292 #define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU) \
293  do { \
294  c->PFX ## _pixels_tab IDX [0] = PFX ## _pixels ## SIZE ## _ ## CPU; \
295  c->PFX ## _pixels_tab IDX [1] = PFX ## _pixels ## SIZE ## _x2_ ## CPU; \
296  c->PFX ## _pixels_tab IDX [2] = PFX ## _pixels ## SIZE ## _y2_ ## CPU; \
297  c->PFX ## _pixels_tab IDX [3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
298  } while (0)
299 
300 static void hpeldsp_init_mmx(HpelDSPContext *c, int flags, int mm_flags)
301 {
302 #if HAVE_INLINE_ASM
303  SET_HPEL_FUNCS(put, [0], 16, mmx);
304  SET_HPEL_FUNCS(put_no_rnd, [0], 16, mmx);
305  SET_HPEL_FUNCS(avg, [0], 16, mmx);
306  SET_HPEL_FUNCS(avg_no_rnd, , 16, mmx);
307  SET_HPEL_FUNCS(put, [1], 8, mmx);
308  SET_HPEL_FUNCS(put_no_rnd, [1], 8, mmx);
309  SET_HPEL_FUNCS(avg, [1], 8, mmx);
310 #endif /* HAVE_INLINE_ASM */
311 }
312 
313 static void hpeldsp_init_mmxext(HpelDSPContext *c, int flags, int mm_flags)
314 {
315 #if HAVE_YASM
317  c->put_pixels_tab[0][2] = ff_put_pixels16_y2_mmxext;
318 
319  c->avg_pixels_tab[0][0] = ff_avg_pixels16_mmxext;
320  c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_mmxext;
321  c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_mmxext;
322 
325 
329 
330  if (!(flags & CODEC_FLAG_BITEXACT)) {
331  c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_mmxext;
332  c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_mmxext;
335 
336  c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_mmxext;
338  }
339 
340  if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
343  }
344 #endif /* HAVE_YASM */
345 }
346 
347 static void hpeldsp_init_3dnow(HpelDSPContext *c, int flags, int mm_flags)
348 {
349 #if HAVE_YASM
351  c->put_pixels_tab[0][2] = ff_put_pixels16_y2_3dnow;
352 
353  c->avg_pixels_tab[0][0] = ff_avg_pixels16_3dnow;
354  c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_3dnow;
355  c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_3dnow;
356 
359 
363 
364  if (!(flags & CODEC_FLAG_BITEXACT)){
365  c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_3dnow;
366  c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_3dnow;
369 
370  c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_3dnow;
372  }
373 
374  if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
377  }
378 #endif /* HAVE_YASM */
379 }
380 
381 static void hpeldsp_init_sse2(HpelDSPContext *c, int flags, int mm_flags)
382 {
383 #if HAVE_YASM
384  if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
385  // these functions are slower than mmx on AMD, but faster on Intel
389  }
390 #endif /* HAVE_YASM */
391 }
392 
394 {
395  int mm_flags = av_get_cpu_flags();
396 
397  if (HAVE_MMX && mm_flags & AV_CPU_FLAG_MMX)
398  hpeldsp_init_mmx(c, flags, mm_flags);
399 
400  if (mm_flags & AV_CPU_FLAG_MMXEXT)
401  hpeldsp_init_mmxext(c, flags, mm_flags);
402 
403  if (mm_flags & AV_CPU_FLAG_3DNOW)
404  hpeldsp_init_3dnow(c, flags, mm_flags);
405 
406  if (mm_flags & AV_CPU_FLAG_SSE2)
407  hpeldsp_init_sse2(c, flags, mm_flags);
408 }
void ff_put_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_put_no_rnd_pixels8_y2_exact_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
op_pixels_func avg_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:68
void ff_avg_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
uint8_t
void ff_put_pixels16_sse2(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_put_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_put_no_rnd_pixels8_y2_exact_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_avg_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
#define put(d, s)
Definition: dsputil_align.c:51
#define AV_CPU_FLAG_MMXEXT
SSE integer functions or AMD MMX ext.
Definition: cpu.h:30
void ff_avg_pixels8_xy2_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_put_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CPU_FLAG_SSE2SLOW
SSE2 supported, but usually not faster.
Definition: cpu.h:35
void ff_put_pixels16_x2_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_hpeldsp_init_x86(HpelDSPContext *c, int flags)
Definition: hpeldsp_init.c:393
void ff_put_no_rnd_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
Half-pel DSP context.
Definition: hpeldsp.h:45
static void hpeldsp_init_3dnow(HpelDSPContext *c, int flags, int mm_flags)
Definition: hpeldsp_init.c:347
#define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU)
Definition: hpeldsp_init.c:292
void ff_avg_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_put_no_rnd_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
#define AV_CPU_FLAG_3DNOW
AMD 3DNOW.
Definition: cpu.h:32
Half-pel DSP functions.
void ff_put_pixels16_x2_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:29
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
void ff_put_no_rnd_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
op_pixels_func put_no_rnd_pixels_tab[4][4]
Halfpel motion compensation with no rounding (a+b)>>1.
Definition: hpeldsp.h:80
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:30
void ff_avg_pixels8_xy2_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
static int flags
Definition: cpu.c:23
void ff_put_no_rnd_pixels8_x2_exact_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
static void hpeldsp_init_sse2(HpelDSPContext *c, int flags, int mm_flags)
Definition: hpeldsp_init.c:381
static double c[64]
int x86_reg
#define avg(d, s)
Definition: dsputil_align.c:52
void ff_avg_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
Definition: cpu.h:34
#define CONFIG_VP3_DECODER
Definition: config.h:616
void ff_put_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
static void hpeldsp_init_mmxext(HpelDSPContext *c, int flags, int mm_flags)
Definition: hpeldsp_init.c:313
static void hpeldsp_init_mmx(HpelDSPContext *c, int flags, int mm_flags)
Definition: hpeldsp_init.c:300
void ff_avg_pixels8_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_put_no_rnd_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_put_no_rnd_pixels8_x2_exact_3dnow(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
void ff_avg_pixels8_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
#define HAVE_MMX
Definition: config.h:48