Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / dsputil_mmx.c @ 5adf43e4

History | View | Annotate | Download (133 KB)

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 "../dsputil.h"
26
#include "../simple_idct.h"
27
#include "../mpegvideo.h"
28
#include "x86_cpu.h"
29
#include "mmx.h"
30

    
31
//#undef NDEBUG
32
//#include <assert.h>
33

    
34
extern void ff_idct_xvid_mmx(short *block);
35
extern void ff_idct_xvid_mmx2(short *block);
36

    
37
int mm_flags; /* multimedia extension flags */
38

    
39
/* pixel operations */
40
static const uint64_t mm_bone attribute_used __attribute__ ((aligned(8))) = 0x0101010101010101ULL;
41
static const uint64_t mm_wone attribute_used __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
42
static const uint64_t mm_wtwo attribute_used __attribute__ ((aligned(8))) = 0x0002000200020002ULL;
43

    
44
static const uint64_t ff_pdw_80000000[2] attribute_used __attribute__ ((aligned(16))) =
45
{0x8000000080000000ULL, 0x8000000080000000ULL};
46

    
47
static const uint64_t ff_pw_20 attribute_used __attribute__ ((aligned(8))) = 0x0014001400140014ULL;
48
static const uint64_t ff_pw_3  attribute_used __attribute__ ((aligned(8))) = 0x0003000300030003ULL;
49
static const uint64_t ff_pw_4  attribute_used __attribute__ ((aligned(8))) = 0x0004000400040004ULL;
50
static const uint64_t ff_pw_5  attribute_used __attribute__ ((aligned(8))) = 0x0005000500050005ULL;
51
static const uint64_t ff_pw_8  attribute_used __attribute__ ((aligned(8))) = 0x0008000800080008ULL;
52
static const uint64_t ff_pw_16 attribute_used __attribute__ ((aligned(8))) = 0x0010001000100010ULL;
53
static const uint64_t ff_pw_32 attribute_used __attribute__ ((aligned(8))) = 0x0020002000200020ULL;
54
static const uint64_t ff_pw_64 attribute_used __attribute__ ((aligned(8))) = 0x0040004000400040ULL;
55
static const uint64_t ff_pw_15 attribute_used __attribute__ ((aligned(8))) = 0x000F000F000F000FULL;
56

    
57
static const uint64_t ff_pb_1  attribute_used __attribute__ ((aligned(8))) = 0x0101010101010101ULL;
58
static const uint64_t ff_pb_3  attribute_used __attribute__ ((aligned(8))) = 0x0303030303030303ULL;
59
static const uint64_t ff_pb_7  attribute_used __attribute__ ((aligned(8))) = 0x0707070707070707ULL;
60
static const uint64_t ff_pb_3F attribute_used __attribute__ ((aligned(8))) = 0x3F3F3F3F3F3F3F3FULL;
61
static const uint64_t ff_pb_A1 attribute_used __attribute__ ((aligned(8))) = 0xA1A1A1A1A1A1A1A1ULL;
62
static const uint64_t ff_pb_5F attribute_used __attribute__ ((aligned(8))) = 0x5F5F5F5F5F5F5F5FULL;
63
static const uint64_t ff_pb_FC attribute_used __attribute__ ((aligned(8))) = 0xFCFCFCFCFCFCFCFCULL;
64

    
65
#define JUMPALIGN() __asm __volatile (ASMALIGN(3)::)
66
#define MOVQ_ZERO(regd)  __asm __volatile ("pxor %%" #regd ", %%" #regd ::)
67

    
68
#define MOVQ_WONE(regd) \
69
    __asm __volatile ( \
70
    "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
71
    "psrlw $15, %%" #regd ::)
72

    
73
#define MOVQ_BFE(regd) \
74
    __asm __volatile ( \
75
    "pcmpeqd %%" #regd ", %%" #regd " \n\t"\
76
    "paddb %%" #regd ", %%" #regd " \n\t" ::)
77

    
78
#ifndef PIC
79
#define MOVQ_BONE(regd)  __asm __volatile ("movq %0, %%" #regd " \n\t" ::"m"(mm_bone))
80
#define MOVQ_WTWO(regd)  __asm __volatile ("movq %0, %%" #regd " \n\t" ::"m"(mm_wtwo))
81
#else
82
// for shared library it's better to use this way for accessing constants
83
// pcmpeqd -> -1
84
#define MOVQ_BONE(regd) \
85
    __asm __volatile ( \
86
    "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
87
    "psrlw $15, %%" #regd " \n\t" \
88
    "packuswb %%" #regd ", %%" #regd " \n\t" ::)
89

    
90
#define MOVQ_WTWO(regd) \
91
    __asm __volatile ( \
92
    "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
93
    "psrlw $15, %%" #regd " \n\t" \
94
    "psllw $1, %%" #regd " \n\t"::)
95

    
96
#endif
97

    
98
// using regr as temporary and for the output result
99
// first argument is unmodifed and second is trashed
100
// regfe is supposed to contain 0xfefefefefefefefe
101
#define PAVGB_MMX_NO_RND(rega, regb, regr, regfe) \
102
    "movq " #rega ", " #regr "  \n\t"\
103
    "pand " #regb ", " #regr "  \n\t"\
104
    "pxor " #rega ", " #regb "  \n\t"\
105
    "pand " #regfe "," #regb "  \n\t"\
106
    "psrlq $1, " #regb "        \n\t"\
107
    "paddb " #regb ", " #regr " \n\t"
108

    
109
#define PAVGB_MMX(rega, regb, regr, regfe) \
110
    "movq " #rega ", " #regr "  \n\t"\
111
    "por  " #regb ", " #regr "  \n\t"\
112
    "pxor " #rega ", " #regb "  \n\t"\
113
    "pand " #regfe "," #regb "  \n\t"\
114
    "psrlq $1, " #regb "        \n\t"\
115
    "psubb " #regb ", " #regr " \n\t"
116

    
117
// mm6 is supposed to contain 0xfefefefefefefefe
118
#define PAVGBP_MMX_NO_RND(rega, regb, regr,  regc, regd, regp) \
119
    "movq " #rega ", " #regr "  \n\t"\
120
    "movq " #regc ", " #regp "  \n\t"\
121
    "pand " #regb ", " #regr "  \n\t"\
122
    "pand " #regd ", " #regp "  \n\t"\
123
    "pxor " #rega ", " #regb "  \n\t"\
124
    "pxor " #regc ", " #regd "  \n\t"\
125
    "pand %%mm6, " #regb "      \n\t"\
126
    "pand %%mm6, " #regd "      \n\t"\
127
    "psrlq $1, " #regb "        \n\t"\
128
    "psrlq $1, " #regd "        \n\t"\
129
    "paddb " #regb ", " #regr " \n\t"\
130
    "paddb " #regd ", " #regp " \n\t"
131

    
132
#define PAVGBP_MMX(rega, regb, regr, regc, regd, regp) \
133
    "movq " #rega ", " #regr "  \n\t"\
134
    "movq " #regc ", " #regp "  \n\t"\
135
    "por  " #regb ", " #regr "  \n\t"\
136
    "por  " #regd ", " #regp "  \n\t"\
137
    "pxor " #rega ", " #regb "  \n\t"\
138
    "pxor " #regc ", " #regd "  \n\t"\
139
    "pand %%mm6, " #regb "      \n\t"\
140
    "pand %%mm6, " #regd "      \n\t"\
141
    "psrlq $1, " #regd "        \n\t"\
142
    "psrlq $1, " #regb "        \n\t"\
143
    "psubb " #regb ", " #regr " \n\t"\
144
    "psubb " #regd ", " #regp " \n\t"
145

    
146
/***********************************/
147
/* MMX no rounding */
148
#define DEF(x, y) x ## _no_rnd_ ## y ##_mmx
149
#define SET_RND  MOVQ_WONE
150
#define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
151
#define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
152

    
153
#include "dsputil_mmx_rnd.h"
154

    
155
#undef DEF
156
#undef SET_RND
157
#undef PAVGBP
158
#undef PAVGB
159
/***********************************/
160
/* MMX rounding */
161

    
162
#define DEF(x, y) x ## _ ## y ##_mmx
163
#define SET_RND  MOVQ_WTWO
164
#define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
165
#define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
166

    
167
#include "dsputil_mmx_rnd.h"
168

    
169
#undef DEF
170
#undef SET_RND
171
#undef PAVGBP
172
#undef PAVGB
173

    
174
/***********************************/
175
/* 3Dnow specific */
176

    
177
#define DEF(x) x ## _3dnow
178
/* for Athlons PAVGUSB is preferred */
179
#define PAVGB "pavgusb"
180

    
181
#include "dsputil_mmx_avg.h"
182

    
183
#undef DEF
184
#undef PAVGB
185

    
186
/***********************************/
187
/* MMX2 specific */
188

    
189
#define DEF(x) x ## _mmx2
190

    
191
/* Introduced only in MMX2 set */
192
#define PAVGB "pavgb"
193

    
194
#include "dsputil_mmx_avg.h"
195

    
196
#undef DEF
197
#undef PAVGB
198

    
199
#define SBUTTERFLY(a,b,t,n)\
200
    "movq " #a ", " #t "              \n\t" /* abcd */\
201
    "punpckl" #n " " #b ", " #a "     \n\t" /* aebf */\
202
    "punpckh" #n " " #b ", " #t "     \n\t" /* cgdh */\
203

    
204
#define TRANSPOSE4(a,b,c,d,t)\
205
    SBUTTERFLY(a,b,t,wd) /* a=aebf t=cgdh */\
206
    SBUTTERFLY(c,d,b,wd) /* c=imjn b=kolp */\
207
    SBUTTERFLY(a,c,d,dq) /* a=aeim d=bfjn */\
208
    SBUTTERFLY(t,b,c,dq) /* t=cgko c=dhlp */
209

    
210
/***********************************/
211
/* standard MMX */
212

    
213
#ifdef CONFIG_ENCODERS
214
static void get_pixels_mmx(DCTELEM *block, const uint8_t *pixels, int line_size)
215
{
216
    asm volatile(
217
        "mov $-128, %%"REG_a"           \n\t"
218
        "pxor %%mm7, %%mm7              \n\t"
219
        ASMALIGN(4)
220
        "1:                             \n\t"
221
        "movq (%0), %%mm0               \n\t"
222
        "movq (%0, %2), %%mm2           \n\t"
223
        "movq %%mm0, %%mm1              \n\t"
224
        "movq %%mm2, %%mm3              \n\t"
225
        "punpcklbw %%mm7, %%mm0         \n\t"
226
        "punpckhbw %%mm7, %%mm1         \n\t"
227
        "punpcklbw %%mm7, %%mm2         \n\t"
228
        "punpckhbw %%mm7, %%mm3         \n\t"
229
        "movq %%mm0, (%1, %%"REG_a")    \n\t"
230
        "movq %%mm1, 8(%1, %%"REG_a")   \n\t"
231
        "movq %%mm2, 16(%1, %%"REG_a")  \n\t"
232
        "movq %%mm3, 24(%1, %%"REG_a")  \n\t"
233
        "add %3, %0                     \n\t"
234
        "add $32, %%"REG_a"             \n\t"
235
        "js 1b                          \n\t"
236
        : "+r" (pixels)
237
        : "r" (block+64), "r" ((long)line_size), "r" ((long)line_size*2)
238
        : "%"REG_a
239
    );
240
}
241

    
242
static inline void diff_pixels_mmx(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride)
243
{
244
    asm volatile(
245
        "pxor %%mm7, %%mm7              \n\t"
246
        "mov $-128, %%"REG_a"           \n\t"
247
        ASMALIGN(4)
248
        "1:                             \n\t"
249
        "movq (%0), %%mm0               \n\t"
250
        "movq (%1), %%mm2               \n\t"
251
        "movq %%mm0, %%mm1              \n\t"
252
        "movq %%mm2, %%mm3              \n\t"
253
        "punpcklbw %%mm7, %%mm0         \n\t"
254
        "punpckhbw %%mm7, %%mm1         \n\t"
255
        "punpcklbw %%mm7, %%mm2         \n\t"
256
        "punpckhbw %%mm7, %%mm3         \n\t"
257
        "psubw %%mm2, %%mm0             \n\t"
258
        "psubw %%mm3, %%mm1             \n\t"
259
        "movq %%mm0, (%2, %%"REG_a")    \n\t"
260
        "movq %%mm1, 8(%2, %%"REG_a")   \n\t"
261
        "add %3, %0                     \n\t"
262
        "add %3, %1                     \n\t"
263
        "add $16, %%"REG_a"             \n\t"
264
        "jnz 1b                         \n\t"
265
        : "+r" (s1), "+r" (s2)
266
        : "r" (block+64), "r" ((long)stride)
267
        : "%"REG_a
268
    );
269
}
270
#endif //CONFIG_ENCODERS
271

    
272
void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
273
{
274
    const DCTELEM *p;
275
    uint8_t *pix;
276

    
277
    /* read the pixels */
278
    p = block;
279
    pix = pixels;
280
    /* unrolled loop */
281
        __asm __volatile(
282
                "movq   %3, %%mm0               \n\t"
283
                "movq   8%3, %%mm1              \n\t"
284
                "movq   16%3, %%mm2             \n\t"
285
                "movq   24%3, %%mm3             \n\t"
286
                "movq   32%3, %%mm4             \n\t"
287
                "movq   40%3, %%mm5             \n\t"
288
                "movq   48%3, %%mm6             \n\t"
289
                "movq   56%3, %%mm7             \n\t"
290
                "packuswb %%mm1, %%mm0          \n\t"
291
                "packuswb %%mm3, %%mm2          \n\t"
292
                "packuswb %%mm5, %%mm4          \n\t"
293
                "packuswb %%mm7, %%mm6          \n\t"
294
                "movq   %%mm0, (%0)             \n\t"
295
                "movq   %%mm2, (%0, %1)         \n\t"
296
                "movq   %%mm4, (%0, %1, 2)      \n\t"
297
                "movq   %%mm6, (%0, %2)         \n\t"
298
                ::"r" (pix), "r" ((long)line_size), "r" ((long)line_size*3), "m"(*p)
299
                :"memory");
300
        pix += line_size*4;
301
        p += 32;
302

    
303
    // if here would be an exact copy of the code above
304
    // compiler would generate some very strange code
305
    // thus using "r"
306
    __asm __volatile(
307
            "movq       (%3), %%mm0             \n\t"
308
            "movq       8(%3), %%mm1            \n\t"
309
            "movq       16(%3), %%mm2           \n\t"
310
            "movq       24(%3), %%mm3           \n\t"
311
            "movq       32(%3), %%mm4           \n\t"
312
            "movq       40(%3), %%mm5           \n\t"
313
            "movq       48(%3), %%mm6           \n\t"
314
            "movq       56(%3), %%mm7           \n\t"
315
            "packuswb %%mm1, %%mm0              \n\t"
316
            "packuswb %%mm3, %%mm2              \n\t"
317
            "packuswb %%mm5, %%mm4              \n\t"
318
            "packuswb %%mm7, %%mm6              \n\t"
319
            "movq       %%mm0, (%0)             \n\t"
320
            "movq       %%mm2, (%0, %1)         \n\t"
321
            "movq       %%mm4, (%0, %1, 2)      \n\t"
322
            "movq       %%mm6, (%0, %2)         \n\t"
323
            ::"r" (pix), "r" ((long)line_size), "r" ((long)line_size*3), "r"(p)
324
            :"memory");
325
}
326

    
327
static DECLARE_ALIGNED_8(const unsigned char, vector128[8]) =
328
  { 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 };
329

    
330
void put_signed_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
331
{
332
    int i;
333

    
334
    movq_m2r(*vector128, mm1);
335
    for (i = 0; i < 8; i++) {
336
        movq_m2r(*(block), mm0);
337
        packsswb_m2r(*(block + 4), mm0);
338
        block += 8;
339
        paddb_r2r(mm1, mm0);
340
        movq_r2m(mm0, *pixels);
341
        pixels += line_size;
342
    }
343
}
344

    
345
void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
346
{
347
    const DCTELEM *p;
348
    uint8_t *pix;
349
    int i;
350

    
351
    /* read the pixels */
352
    p = block;
353
    pix = pixels;
354
    MOVQ_ZERO(mm7);
355
    i = 4;
356
    do {
357
        __asm __volatile(
358
                "movq   (%2), %%mm0     \n\t"
359
                "movq   8(%2), %%mm1    \n\t"
360
                "movq   16(%2), %%mm2   \n\t"
361
                "movq   24(%2), %%mm3   \n\t"
362
                "movq   %0, %%mm4       \n\t"
363
                "movq   %1, %%mm6       \n\t"
364
                "movq   %%mm4, %%mm5    \n\t"
365
                "punpcklbw %%mm7, %%mm4 \n\t"
366
                "punpckhbw %%mm7, %%mm5 \n\t"
367
                "paddsw %%mm4, %%mm0    \n\t"
368
                "paddsw %%mm5, %%mm1    \n\t"
369
                "movq   %%mm6, %%mm5    \n\t"
370
                "punpcklbw %%mm7, %%mm6 \n\t"
371
                "punpckhbw %%mm7, %%mm5 \n\t"
372
                "paddsw %%mm6, %%mm2    \n\t"
373
                "paddsw %%mm5, %%mm3    \n\t"
374
                "packuswb %%mm1, %%mm0  \n\t"
375
                "packuswb %%mm3, %%mm2  \n\t"
376
                "movq   %%mm0, %0       \n\t"
377
                "movq   %%mm2, %1       \n\t"
378
                :"+m"(*pix), "+m"(*(pix+line_size))
379
                :"r"(p)
380
                :"memory");
381
        pix += line_size*2;
382
        p += 16;
383
    } while (--i);
384
}
385

    
386
static void put_pixels4_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
387
{
388
    __asm __volatile(
389
         "lea (%3, %3), %%"REG_a"       \n\t"
390
         ASMALIGN(3)
391
         "1:                            \n\t"
392
         "movd (%1), %%mm0              \n\t"
393
         "movd (%1, %3), %%mm1          \n\t"
394
         "movd %%mm0, (%2)              \n\t"
395
         "movd %%mm1, (%2, %3)          \n\t"
396
         "add %%"REG_a", %1             \n\t"
397
         "add %%"REG_a", %2             \n\t"
398
         "movd (%1), %%mm0              \n\t"
399
         "movd (%1, %3), %%mm1          \n\t"
400
         "movd %%mm0, (%2)              \n\t"
401
         "movd %%mm1, (%2, %3)          \n\t"
402
         "add %%"REG_a", %1             \n\t"
403
         "add %%"REG_a", %2             \n\t"
404
         "subl $4, %0                   \n\t"
405
         "jnz 1b                        \n\t"
406
         : "+g"(h), "+r" (pixels),  "+r" (block)
407
         : "r"((long)line_size)
408
         : "%"REG_a, "memory"
409
        );
410
}
411

    
412
static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
413
{
414
    __asm __volatile(
415
         "lea (%3, %3), %%"REG_a"       \n\t"
416
         ASMALIGN(3)
417
         "1:                            \n\t"
418
         "movq (%1), %%mm0              \n\t"
419
         "movq (%1, %3), %%mm1          \n\t"
420
         "movq %%mm0, (%2)              \n\t"
421
         "movq %%mm1, (%2, %3)          \n\t"
422
         "add %%"REG_a", %1             \n\t"
423
         "add %%"REG_a", %2             \n\t"
424
         "movq (%1), %%mm0              \n\t"
425
         "movq (%1, %3), %%mm1          \n\t"
426
         "movq %%mm0, (%2)              \n\t"
427
         "movq %%mm1, (%2, %3)          \n\t"
428
         "add %%"REG_a", %1             \n\t"
429
         "add %%"REG_a", %2             \n\t"
430
         "subl $4, %0                   \n\t"
431
         "jnz 1b                        \n\t"
432
         : "+g"(h), "+r" (pixels),  "+r" (block)
433
         : "r"((long)line_size)
434
         : "%"REG_a, "memory"
435
        );
436
}
437

    
438
static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
439
{
440
    __asm __volatile(
441
         "lea (%3, %3), %%"REG_a"       \n\t"
442
         ASMALIGN(3)
443
         "1:                            \n\t"
444
         "movq (%1), %%mm0              \n\t"
445
         "movq 8(%1), %%mm4             \n\t"
446
         "movq (%1, %3), %%mm1          \n\t"
447
         "movq 8(%1, %3), %%mm5         \n\t"
448
         "movq %%mm0, (%2)              \n\t"
449
         "movq %%mm4, 8(%2)             \n\t"
450
         "movq %%mm1, (%2, %3)          \n\t"
451
         "movq %%mm5, 8(%2, %3)         \n\t"
452
         "add %%"REG_a", %1             \n\t"
453
         "add %%"REG_a", %2             \n\t"
454
         "movq (%1), %%mm0              \n\t"
455
         "movq 8(%1), %%mm4             \n\t"
456
         "movq (%1, %3), %%mm1          \n\t"
457
         "movq 8(%1, %3), %%mm5         \n\t"
458
         "movq %%mm0, (%2)              \n\t"
459
         "movq %%mm4, 8(%2)             \n\t"
460
         "movq %%mm1, (%2, %3)          \n\t"
461
         "movq %%mm5, 8(%2, %3)         \n\t"
462
         "add %%"REG_a", %1             \n\t"
463
         "add %%"REG_a", %2             \n\t"
464
         "subl $4, %0                   \n\t"
465
         "jnz 1b                        \n\t"
466
         : "+g"(h), "+r" (pixels),  "+r" (block)
467
         : "r"((long)line_size)
468
         : "%"REG_a, "memory"
469
        );
470
}
471

    
472
static void clear_blocks_mmx(DCTELEM *blocks)
473
{
474
    __asm __volatile(
475
                "pxor %%mm7, %%mm7              \n\t"
476
                "mov $-128*6, %%"REG_a"         \n\t"
477
                "1:                             \n\t"
478
                "movq %%mm7, (%0, %%"REG_a")    \n\t"
479
                "movq %%mm7, 8(%0, %%"REG_a")   \n\t"
480
                "movq %%mm7, 16(%0, %%"REG_a")  \n\t"
481
                "movq %%mm7, 24(%0, %%"REG_a")  \n\t"
482
                "add $32, %%"REG_a"             \n\t"
483
                " js 1b                         \n\t"
484
                : : "r" (((uint8_t *)blocks)+128*6)
485
                : "%"REG_a
486
        );
487
}
488

    
489
#ifdef CONFIG_ENCODERS
490
static int pix_sum16_mmx(uint8_t * pix, int line_size){
491
    const int h=16;
492
    int sum;
493
    long index= -line_size*h;
494

    
495
    __asm __volatile(
496
                "pxor %%mm7, %%mm7              \n\t"
497
                "pxor %%mm6, %%mm6              \n\t"
498
                "1:                             \n\t"
499
                "movq (%2, %1), %%mm0           \n\t"
500
                "movq (%2, %1), %%mm1           \n\t"
501
                "movq 8(%2, %1), %%mm2          \n\t"
502
                "movq 8(%2, %1), %%mm3          \n\t"
503
                "punpcklbw %%mm7, %%mm0         \n\t"
504
                "punpckhbw %%mm7, %%mm1         \n\t"
505
                "punpcklbw %%mm7, %%mm2         \n\t"
506
                "punpckhbw %%mm7, %%mm3         \n\t"
507
                "paddw %%mm0, %%mm1             \n\t"
508
                "paddw %%mm2, %%mm3             \n\t"
509
                "paddw %%mm1, %%mm3             \n\t"
510
                "paddw %%mm3, %%mm6             \n\t"
511
                "add %3, %1                     \n\t"
512
                " js 1b                         \n\t"
513
                "movq %%mm6, %%mm5              \n\t"
514
                "psrlq $32, %%mm6               \n\t"
515
                "paddw %%mm5, %%mm6             \n\t"
516
                "movq %%mm6, %%mm5              \n\t"
517
                "psrlq $16, %%mm6               \n\t"
518
                "paddw %%mm5, %%mm6             \n\t"
519
                "movd %%mm6, %0                 \n\t"
520
                "andl $0xFFFF, %0               \n\t"
521
                : "=&r" (sum), "+r" (index)
522
                : "r" (pix - index), "r" ((long)line_size)
523
        );
524

    
525
        return sum;
526
}
527
#endif //CONFIG_ENCODERS
528

    
529
static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w){
530
    long i=0;
531
    asm volatile(
532
        "1:                             \n\t"
533
        "movq  (%1, %0), %%mm0          \n\t"
534
        "movq  (%2, %0), %%mm1          \n\t"
535
        "paddb %%mm0, %%mm1             \n\t"
536
        "movq %%mm1, (%2, %0)           \n\t"
537
        "movq 8(%1, %0), %%mm0          \n\t"
538
        "movq 8(%2, %0), %%mm1          \n\t"
539
        "paddb %%mm0, %%mm1             \n\t"
540
        "movq %%mm1, 8(%2, %0)          \n\t"
541
        "add $16, %0                    \n\t"
542
        "cmp %3, %0                     \n\t"
543
        " jb 1b                         \n\t"
544
        : "+r" (i)
545
        : "r"(src), "r"(dst), "r"((long)w-15)
546
    );
547
    for(; i<w; i++)
548
        dst[i+0] += src[i+0];
549
}
550

    
551
#define H263_LOOP_FILTER \
552
        "pxor %%mm7, %%mm7              \n\t"\
553
        "movq  %0, %%mm0                \n\t"\
554
        "movq  %0, %%mm1                \n\t"\
555
        "movq  %3, %%mm2                \n\t"\
556
        "movq  %3, %%mm3                \n\t"\
557
        "punpcklbw %%mm7, %%mm0         \n\t"\
558
        "punpckhbw %%mm7, %%mm1         \n\t"\
559
        "punpcklbw %%mm7, %%mm2         \n\t"\
560
        "punpckhbw %%mm7, %%mm3         \n\t"\
561
        "psubw %%mm2, %%mm0             \n\t"\
562
        "psubw %%mm3, %%mm1             \n\t"\
563
        "movq  %1, %%mm2                \n\t"\
564
        "movq  %1, %%mm3                \n\t"\
565
        "movq  %2, %%mm4                \n\t"\
566
        "movq  %2, %%mm5                \n\t"\
567
        "punpcklbw %%mm7, %%mm2         \n\t"\
568
        "punpckhbw %%mm7, %%mm3         \n\t"\
569
        "punpcklbw %%mm7, %%mm4         \n\t"\
570
        "punpckhbw %%mm7, %%mm5         \n\t"\
571
        "psubw %%mm2, %%mm4             \n\t"\
572
        "psubw %%mm3, %%mm5             \n\t"\
573
        "psllw $2, %%mm4                \n\t"\
574
        "psllw $2, %%mm5                \n\t"\
575
        "paddw %%mm0, %%mm4             \n\t"\
576
        "paddw %%mm1, %%mm5             \n\t"\
577
        "pxor %%mm6, %%mm6              \n\t"\
578
        "pcmpgtw %%mm4, %%mm6           \n\t"\
579
        "pcmpgtw %%mm5, %%mm7           \n\t"\
580
        "pxor %%mm6, %%mm4              \n\t"\
581
        "pxor %%mm7, %%mm5              \n\t"\
582
        "psubw %%mm6, %%mm4             \n\t"\
583
        "psubw %%mm7, %%mm5             \n\t"\
584
        "psrlw $3, %%mm4                \n\t"\
585
        "psrlw $3, %%mm5                \n\t"\
586
        "packuswb %%mm5, %%mm4          \n\t"\
587
        "packsswb %%mm7, %%mm6          \n\t"\
588
        "pxor %%mm7, %%mm7              \n\t"\
589
        "movd %4, %%mm2                 \n\t"\
590
        "punpcklbw %%mm2, %%mm2         \n\t"\
591
        "punpcklbw %%mm2, %%mm2         \n\t"\
592
        "punpcklbw %%mm2, %%mm2         \n\t"\
593
        "psubusb %%mm4, %%mm2           \n\t"\
594
        "movq %%mm2, %%mm3              \n\t"\
595
        "psubusb %%mm4, %%mm3           \n\t"\
596
        "psubb %%mm3, %%mm2             \n\t"\
597
        "movq %1, %%mm3                 \n\t"\
598
        "movq %2, %%mm4                 \n\t"\
599
        "pxor %%mm6, %%mm3              \n\t"\
600
        "pxor %%mm6, %%mm4              \n\t"\
601
        "paddusb %%mm2, %%mm3           \n\t"\
602
        "psubusb %%mm2, %%mm4           \n\t"\
603
        "pxor %%mm6, %%mm3              \n\t"\
604
        "pxor %%mm6, %%mm4              \n\t"\
605
        "paddusb %%mm2, %%mm2           \n\t"\
606
        "packsswb %%mm1, %%mm0          \n\t"\
607
        "pcmpgtb %%mm0, %%mm7           \n\t"\
608
        "pxor %%mm7, %%mm0              \n\t"\
609
        "psubb %%mm7, %%mm0             \n\t"\
610
        "movq %%mm0, %%mm1              \n\t"\
611
        "psubusb %%mm2, %%mm0           \n\t"\
612
        "psubb %%mm0, %%mm1             \n\t"\
613
        "pand %5, %%mm1                 \n\t"\
614
        "psrlw $2, %%mm1                \n\t"\
615
        "pxor %%mm7, %%mm1              \n\t"\
616
        "psubb %%mm7, %%mm1             \n\t"\
617
        "movq %0, %%mm5                 \n\t"\
618
        "movq %3, %%mm6                 \n\t"\
619
        "psubb %%mm1, %%mm5             \n\t"\
620
        "paddb %%mm1, %%mm6             \n\t"
621

    
622
static void h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale){
623
    const int strength= ff_h263_loop_filter_strength[qscale];
624

    
625
    asm volatile(
626

    
627
        H263_LOOP_FILTER
628

    
629
        "movq %%mm3, %1                 \n\t"
630
        "movq %%mm4, %2                 \n\t"
631
        "movq %%mm5, %0                 \n\t"
632
        "movq %%mm6, %3                 \n\t"
633
        : "+m" (*(uint64_t*)(src - 2*stride)),
634
          "+m" (*(uint64_t*)(src - 1*stride)),
635
          "+m" (*(uint64_t*)(src + 0*stride)),
636
          "+m" (*(uint64_t*)(src + 1*stride))
637
        : "g" (2*strength), "m"(ff_pb_FC)
638
    );
639
}
640

    
641
static inline void transpose4x4(uint8_t *dst, uint8_t *src, int dst_stride, int src_stride){
642
    asm volatile( //FIXME could save 1 instruction if done as 8x4 ...
643
        "movd  %4, %%mm0                \n\t"
644
        "movd  %5, %%mm1                \n\t"
645
        "movd  %6, %%mm2                \n\t"
646
        "movd  %7, %%mm3                \n\t"
647
        "punpcklbw %%mm1, %%mm0         \n\t"
648
        "punpcklbw %%mm3, %%mm2         \n\t"
649
        "movq %%mm0, %%mm1              \n\t"
650
        "punpcklwd %%mm2, %%mm0         \n\t"
651
        "punpckhwd %%mm2, %%mm1         \n\t"
652
        "movd  %%mm0, %0                \n\t"
653
        "punpckhdq %%mm0, %%mm0         \n\t"
654
        "movd  %%mm0, %1                \n\t"
655
        "movd  %%mm1, %2                \n\t"
656
        "punpckhdq %%mm1, %%mm1         \n\t"
657
        "movd  %%mm1, %3                \n\t"
658

    
659
        : "=m" (*(uint32_t*)(dst + 0*dst_stride)),
660
          "=m" (*(uint32_t*)(dst + 1*dst_stride)),
661
          "=m" (*(uint32_t*)(dst + 2*dst_stride)),
662
          "=m" (*(uint32_t*)(dst + 3*dst_stride))
663
        :  "m" (*(uint32_t*)(src + 0*src_stride)),
664
           "m" (*(uint32_t*)(src + 1*src_stride)),
665
           "m" (*(uint32_t*)(src + 2*src_stride)),
666
           "m" (*(uint32_t*)(src + 3*src_stride))
667
    );
668
}
669

    
670
static void h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale){
671
    const int strength= ff_h263_loop_filter_strength[qscale];
672
    uint64_t temp[4] __attribute__ ((aligned(8)));
673
    uint8_t *btemp= (uint8_t*)temp;
674

    
675
    src -= 2;
676

    
677
    transpose4x4(btemp  , src           , 8, stride);
678
    transpose4x4(btemp+4, src + 4*stride, 8, stride);
679
    asm volatile(
680
        H263_LOOP_FILTER // 5 3 4 6
681

    
682
        : "+m" (temp[0]),
683
          "+m" (temp[1]),
684
          "+m" (temp[2]),
685
          "+m" (temp[3])
686
        : "g" (2*strength), "m"(ff_pb_FC)
687
    );
688

    
689
    asm volatile(
690
        "movq %%mm5, %%mm1              \n\t"
691
        "movq %%mm4, %%mm0              \n\t"
692
        "punpcklbw %%mm3, %%mm5         \n\t"
693
        "punpcklbw %%mm6, %%mm4         \n\t"
694
        "punpckhbw %%mm3, %%mm1         \n\t"
695
        "punpckhbw %%mm6, %%mm0         \n\t"
696
        "movq %%mm5, %%mm3              \n\t"
697
        "movq %%mm1, %%mm6              \n\t"
698
        "punpcklwd %%mm4, %%mm5         \n\t"
699
        "punpcklwd %%mm0, %%mm1         \n\t"
700
        "punpckhwd %%mm4, %%mm3         \n\t"
701
        "punpckhwd %%mm0, %%mm6         \n\t"
702
        "movd %%mm5, (%0)               \n\t"
703
        "punpckhdq %%mm5, %%mm5         \n\t"
704
        "movd %%mm5, (%0,%2)            \n\t"
705
        "movd %%mm3, (%0,%2,2)          \n\t"
706
        "punpckhdq %%mm3, %%mm3         \n\t"
707
        "movd %%mm3, (%0,%3)            \n\t"
708
        "movd %%mm1, (%1)               \n\t"
709
        "punpckhdq %%mm1, %%mm1         \n\t"
710
        "movd %%mm1, (%1,%2)            \n\t"
711
        "movd %%mm6, (%1,%2,2)          \n\t"
712
        "punpckhdq %%mm6, %%mm6         \n\t"
713
        "movd %%mm6, (%1,%3)            \n\t"
714
        :: "r" (src),
715
           "r" (src + 4*stride),
716
           "r" ((long)   stride ),
717
           "r" ((long)(3*stride))
718
    );
719
}
720

    
721
#ifdef CONFIG_ENCODERS
722
static int pix_norm1_mmx(uint8_t *pix, int line_size) {
723
    int tmp;
724
  asm volatile (
725
      "movl $16,%%ecx\n"
726
      "pxor %%mm0,%%mm0\n"
727
      "pxor %%mm7,%%mm7\n"
728
      "1:\n"
729
      "movq (%0),%%mm2\n"       /* mm2 = pix[0-7] */
730
      "movq 8(%0),%%mm3\n"      /* mm3 = pix[8-15] */
731

    
732
      "movq %%mm2,%%mm1\n"      /* mm1 = mm2 = pix[0-7] */
733

    
734
      "punpckhbw %%mm0,%%mm1\n" /* mm1 = [pix4-7] */
735
      "punpcklbw %%mm0,%%mm2\n" /* mm2 = [pix0-3] */
736

    
737
      "movq %%mm3,%%mm4\n"      /* mm4 = mm3 = pix[8-15] */
738
      "punpckhbw %%mm0,%%mm3\n" /* mm3 = [pix12-15] */
739
      "punpcklbw %%mm0,%%mm4\n" /* mm4 = [pix8-11] */
740

    
741
      "pmaddwd %%mm1,%%mm1\n"   /* mm1 = (pix0^2+pix1^2,pix2^2+pix3^2) */
742
      "pmaddwd %%mm2,%%mm2\n"   /* mm2 = (pix4^2+pix5^2,pix6^2+pix7^2) */
743

    
744
      "pmaddwd %%mm3,%%mm3\n"
745
      "pmaddwd %%mm4,%%mm4\n"
746

    
747
      "paddd %%mm1,%%mm2\n"     /* mm2 = (pix0^2+pix1^2+pix4^2+pix5^2,
748
                                          pix2^2+pix3^2+pix6^2+pix7^2) */
749
      "paddd %%mm3,%%mm4\n"
750
      "paddd %%mm2,%%mm7\n"
751

    
752
      "add %2, %0\n"
753
      "paddd %%mm4,%%mm7\n"
754
      "dec %%ecx\n"
755
      "jnz 1b\n"
756

    
757
      "movq %%mm7,%%mm1\n"
758
      "psrlq $32, %%mm7\n"      /* shift hi dword to lo */
759
      "paddd %%mm7,%%mm1\n"
760
      "movd %%mm1,%1\n"
761
      : "+r" (pix), "=r"(tmp) : "r" ((long)line_size) : "%ecx" );
762
    return tmp;
763
}
764

    
765
static int sse8_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
766
    int tmp;
767
  asm volatile (
768
      "movl %4,%%ecx\n"
769
      "shr $1,%%ecx\n"
770
      "pxor %%mm0,%%mm0\n"      /* mm0 = 0 */
771
      "pxor %%mm7,%%mm7\n"      /* mm7 holds the sum */
772
      "1:\n"
773
      "movq (%0),%%mm1\n"       /* mm1 = pix1[0][0-7] */
774
      "movq (%1),%%mm2\n"       /* mm2 = pix2[0][0-7] */
775
      "movq (%0,%3),%%mm3\n"    /* mm3 = pix1[1][0-7] */
776
      "movq (%1,%3),%%mm4\n"    /* mm4 = pix2[1][0-7] */
777

    
778
      /* todo: mm1-mm2, mm3-mm4 */
779
      /* algo: substract mm1 from mm2 with saturation and vice versa */
780
      /*       OR the results to get absolute difference */
781
      "movq %%mm1,%%mm5\n"
782
      "movq %%mm3,%%mm6\n"
783
      "psubusb %%mm2,%%mm1\n"
784
      "psubusb %%mm4,%%mm3\n"
785
      "psubusb %%mm5,%%mm2\n"
786
      "psubusb %%mm6,%%mm4\n"
787

    
788
      "por %%mm1,%%mm2\n"
789
      "por %%mm3,%%mm4\n"
790

    
791
      /* now convert to 16-bit vectors so we can square them */
792
      "movq %%mm2,%%mm1\n"
793
      "movq %%mm4,%%mm3\n"
794

    
795
      "punpckhbw %%mm0,%%mm2\n"
796
      "punpckhbw %%mm0,%%mm4\n"
797
      "punpcklbw %%mm0,%%mm1\n" /* mm1 now spread over (mm1,mm2) */
798
      "punpcklbw %%mm0,%%mm3\n" /* mm4 now spread over (mm3,mm4) */
799

    
800
      "pmaddwd %%mm2,%%mm2\n"
801
      "pmaddwd %%mm4,%%mm4\n"
802
      "pmaddwd %%mm1,%%mm1\n"
803
      "pmaddwd %%mm3,%%mm3\n"
804

    
805
      "lea (%0,%3,2), %0\n"     /* pix1 += 2*line_size */
806
      "lea (%1,%3,2), %1\n"     /* pix2 += 2*line_size */
807

    
808
      "paddd %%mm2,%%mm1\n"
809
      "paddd %%mm4,%%mm3\n"
810
      "paddd %%mm1,%%mm7\n"
811
      "paddd %%mm3,%%mm7\n"
812

    
813
      "decl %%ecx\n"
814
      "jnz 1b\n"
815

    
816
      "movq %%mm7,%%mm1\n"
817
      "psrlq $32, %%mm7\n"      /* shift hi dword to lo */
818
      "paddd %%mm7,%%mm1\n"
819
      "movd %%mm1,%2\n"
820
      : "+r" (pix1), "+r" (pix2), "=r"(tmp)
821
      : "r" ((long)line_size) , "m" (h)
822
      : "%ecx");
823
    return tmp;
824
}
825

    
826
static int sse16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
827
    int tmp;
828
  asm volatile (
829
      "movl %4,%%ecx\n"
830
      "pxor %%mm0,%%mm0\n"      /* mm0 = 0 */
831
      "pxor %%mm7,%%mm7\n"      /* mm7 holds the sum */
832
      "1:\n"
833
      "movq (%0),%%mm1\n"       /* mm1 = pix1[0-7] */
834
      "movq (%1),%%mm2\n"       /* mm2 = pix2[0-7] */
835
      "movq 8(%0),%%mm3\n"      /* mm3 = pix1[8-15] */
836
      "movq 8(%1),%%mm4\n"      /* mm4 = pix2[8-15] */
837

    
838
      /* todo: mm1-mm2, mm3-mm4 */
839
      /* algo: substract mm1 from mm2 with saturation and vice versa */
840
      /*       OR the results to get absolute difference */
841
      "movq %%mm1,%%mm5\n"
842
      "movq %%mm3,%%mm6\n"
843
      "psubusb %%mm2,%%mm1\n"
844
      "psubusb %%mm4,%%mm3\n"
845
      "psubusb %%mm5,%%mm2\n"
846
      "psubusb %%mm6,%%mm4\n"
847

    
848
      "por %%mm1,%%mm2\n"
849
      "por %%mm3,%%mm4\n"
850

    
851
      /* now convert to 16-bit vectors so we can square them */
852
      "movq %%mm2,%%mm1\n"
853
      "movq %%mm4,%%mm3\n"
854

    
855
      "punpckhbw %%mm0,%%mm2\n"
856
      "punpckhbw %%mm0,%%mm4\n"
857
      "punpcklbw %%mm0,%%mm1\n" /* mm1 now spread over (mm1,mm2) */
858
      "punpcklbw %%mm0,%%mm3\n" /* mm4 now spread over (mm3,mm4) */
859

    
860
      "pmaddwd %%mm2,%%mm2\n"
861
      "pmaddwd %%mm4,%%mm4\n"
862
      "pmaddwd %%mm1,%%mm1\n"
863
      "pmaddwd %%mm3,%%mm3\n"
864

    
865
      "add %3,%0\n"
866
      "add %3,%1\n"
867

    
868
      "paddd %%mm2,%%mm1\n"
869
      "paddd %%mm4,%%mm3\n"
870
      "paddd %%mm1,%%mm7\n"
871
      "paddd %%mm3,%%mm7\n"
872

    
873
      "decl %%ecx\n"
874
      "jnz 1b\n"
875

    
876
      "movq %%mm7,%%mm1\n"
877
      "psrlq $32, %%mm7\n"      /* shift hi dword to lo */
878
      "paddd %%mm7,%%mm1\n"
879
      "movd %%mm1,%2\n"
880
      : "+r" (pix1), "+r" (pix2), "=r"(tmp)
881
      : "r" ((long)line_size) , "m" (h)
882
      : "%ecx");
883
    return tmp;
884
}
885

    
886
static int sse16_sse2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
887
    int tmp;
888
  asm volatile (
889
      "shr $1,%2\n"
890
      "pxor %%xmm0,%%xmm0\n"    /* mm0 = 0 */
891
      "pxor %%xmm7,%%xmm7\n"    /* mm7 holds the sum */
892
      "1:\n"
893
      "movdqu (%0),%%xmm1\n"    /* mm1 = pix1[0][0-15] */
894
      "movdqu (%1),%%xmm2\n"    /* mm2 = pix2[0][0-15] */
895
      "movdqu (%0,%4),%%xmm3\n" /* mm3 = pix1[1][0-15] */
896
      "movdqu (%1,%4),%%xmm4\n" /* mm4 = pix2[1][0-15] */
897

    
898
      /* todo: mm1-mm2, mm3-mm4 */
899
      /* algo: substract mm1 from mm2 with saturation and vice versa */
900
      /*       OR the results to get absolute difference */
901
      "movdqa %%xmm1,%%xmm5\n"
902
      "movdqa %%xmm3,%%xmm6\n"
903
      "psubusb %%xmm2,%%xmm1\n"
904
      "psubusb %%xmm4,%%xmm3\n"
905
      "psubusb %%xmm5,%%xmm2\n"
906
      "psubusb %%xmm6,%%xmm4\n"
907

    
908
      "por %%xmm1,%%xmm2\n"
909
      "por %%xmm3,%%xmm4\n"
910

    
911
      /* now convert to 16-bit vectors so we can square them */
912
      "movdqa %%xmm2,%%xmm1\n"
913
      "movdqa %%xmm4,%%xmm3\n"
914

    
915
      "punpckhbw %%xmm0,%%xmm2\n"
916
      "punpckhbw %%xmm0,%%xmm4\n"
917
      "punpcklbw %%xmm0,%%xmm1\n"  /* mm1 now spread over (mm1,mm2) */
918
      "punpcklbw %%xmm0,%%xmm3\n"  /* mm4 now spread over (mm3,mm4) */
919

    
920
      "pmaddwd %%xmm2,%%xmm2\n"
921
      "pmaddwd %%xmm4,%%xmm4\n"
922
      "pmaddwd %%xmm1,%%xmm1\n"
923
      "pmaddwd %%xmm3,%%xmm3\n"
924

    
925
      "lea (%0,%4,2), %0\n"        /* pix1 += 2*line_size */
926
      "lea (%1,%4,2), %1\n"        /* pix2 += 2*line_size */
927

    
928
      "paddd %%xmm2,%%xmm1\n"
929
      "paddd %%xmm4,%%xmm3\n"
930
      "paddd %%xmm1,%%xmm7\n"
931
      "paddd %%xmm3,%%xmm7\n"
932

    
933
      "decl %2\n"
934
      "jnz 1b\n"
935

    
936
      "movdqa %%xmm7,%%xmm1\n"
937
      "psrldq $8, %%xmm7\n"        /* shift hi qword to lo */
938
      "paddd %%xmm1,%%xmm7\n"
939
      "movdqa %%xmm7,%%xmm1\n"
940
      "psrldq $4, %%xmm7\n"        /* shift hi dword to lo */
941
      "paddd %%xmm1,%%xmm7\n"
942
      "movd %%xmm7,%3\n"
943
      : "+r" (pix1), "+r" (pix2), "+r"(h), "=r"(tmp)
944
      : "r" ((long)line_size));
945
    return tmp;
946
}
947

    
948
static int hf_noise8_mmx(uint8_t * pix1, int line_size, int h) {
949
    int tmp;
950
  asm volatile (
951
      "movl %3,%%ecx\n"
952
      "pxor %%mm7,%%mm7\n"
953
      "pxor %%mm6,%%mm6\n"
954

    
955
      "movq (%0),%%mm0\n"
956
      "movq %%mm0, %%mm1\n"
957
      "psllq $8, %%mm0\n"
958
      "psrlq $8, %%mm1\n"
959
      "psrlq $8, %%mm0\n"
960
      "movq %%mm0, %%mm2\n"
961
      "movq %%mm1, %%mm3\n"
962
      "punpcklbw %%mm7,%%mm0\n"
963
      "punpcklbw %%mm7,%%mm1\n"
964
      "punpckhbw %%mm7,%%mm2\n"
965
      "punpckhbw %%mm7,%%mm3\n"
966
      "psubw %%mm1, %%mm0\n"
967
      "psubw %%mm3, %%mm2\n"
968

    
969
      "add %2,%0\n"
970

    
971
      "movq (%0),%%mm4\n"
972
      "movq %%mm4, %%mm1\n"
973
      "psllq $8, %%mm4\n"
974
      "psrlq $8, %%mm1\n"
975
      "psrlq $8, %%mm4\n"
976
      "movq %%mm4, %%mm5\n"
977
      "movq %%mm1, %%mm3\n"
978
      "punpcklbw %%mm7,%%mm4\n"
979
      "punpcklbw %%mm7,%%mm1\n"
980
      "punpckhbw %%mm7,%%mm5\n"
981
      "punpckhbw %%mm7,%%mm3\n"
982
      "psubw %%mm1, %%mm4\n"
983
      "psubw %%mm3, %%mm5\n"
984
      "psubw %%mm4, %%mm0\n"
985
      "psubw %%mm5, %%mm2\n"
986
      "pxor %%mm3, %%mm3\n"
987
      "pxor %%mm1, %%mm1\n"
988
      "pcmpgtw %%mm0, %%mm3\n\t"
989
      "pcmpgtw %%mm2, %%mm1\n\t"
990
      "pxor %%mm3, %%mm0\n"
991
      "pxor %%mm1, %%mm2\n"
992
      "psubw %%mm3, %%mm0\n"
993
      "psubw %%mm1, %%mm2\n"
994
      "paddw %%mm0, %%mm2\n"
995
      "paddw %%mm2, %%mm6\n"
996

    
997
      "add %2,%0\n"
998
      "1:\n"
999

    
1000
      "movq (%0),%%mm0\n"
1001
      "movq %%mm0, %%mm1\n"
1002
      "psllq $8, %%mm0\n"
1003
      "psrlq $8, %%mm1\n"
1004
      "psrlq $8, %%mm0\n"
1005
      "movq %%mm0, %%mm2\n"
1006
      "movq %%mm1, %%mm3\n"
1007
      "punpcklbw %%mm7,%%mm0\n"
1008
      "punpcklbw %%mm7,%%mm1\n"
1009
      "punpckhbw %%mm7,%%mm2\n"
1010
      "punpckhbw %%mm7,%%mm3\n"
1011
      "psubw %%mm1, %%mm0\n"
1012
      "psubw %%mm3, %%mm2\n"
1013
      "psubw %%mm0, %%mm4\n"
1014
      "psubw %%mm2, %%mm5\n"
1015
      "pxor %%mm3, %%mm3\n"
1016
      "pxor %%mm1, %%mm1\n"
1017
      "pcmpgtw %%mm4, %%mm3\n\t"
1018
      "pcmpgtw %%mm5, %%mm1\n\t"
1019
      "pxor %%mm3, %%mm4\n"
1020
      "pxor %%mm1, %%mm5\n"
1021
      "psubw %%mm3, %%mm4\n"
1022
      "psubw %%mm1, %%mm5\n"
1023
      "paddw %%mm4, %%mm5\n"
1024
      "paddw %%mm5, %%mm6\n"
1025

    
1026
      "add %2,%0\n"
1027

    
1028
      "movq (%0),%%mm4\n"
1029
      "movq %%mm4, %%mm1\n"
1030
      "psllq $8, %%mm4\n"
1031
      "psrlq $8, %%mm1\n"
1032
      "psrlq $8, %%mm4\n"
1033
      "movq %%mm4, %%mm5\n"
1034
      "movq %%mm1, %%mm3\n"
1035
      "punpcklbw %%mm7,%%mm4\n"
1036
      "punpcklbw %%mm7,%%mm1\n"
1037
      "punpckhbw %%mm7,%%mm5\n"
1038
      "punpckhbw %%mm7,%%mm3\n"
1039
      "psubw %%mm1, %%mm4\n"
1040
      "psubw %%mm3, %%mm5\n"
1041
      "psubw %%mm4, %%mm0\n"
1042
      "psubw %%mm5, %%mm2\n"
1043
      "pxor %%mm3, %%mm3\n"
1044
      "pxor %%mm1, %%mm1\n"
1045
      "pcmpgtw %%mm0, %%mm3\n\t"
1046
      "pcmpgtw %%mm2, %%mm1\n\t"
1047
      "pxor %%mm3, %%mm0\n"
1048
      "pxor %%mm1, %%mm2\n"
1049
      "psubw %%mm3, %%mm0\n"
1050
      "psubw %%mm1, %%mm2\n"
1051
      "paddw %%mm0, %%mm2\n"
1052
      "paddw %%mm2, %%mm6\n"
1053

    
1054
      "add %2,%0\n"
1055
      "subl $2, %%ecx\n"
1056
      " jnz 1b\n"
1057

    
1058
      "movq %%mm6, %%mm0\n"
1059
      "punpcklwd %%mm7,%%mm0\n"
1060
      "punpckhwd %%mm7,%%mm6\n"
1061
      "paddd %%mm0, %%mm6\n"
1062

    
1063
      "movq %%mm6,%%mm0\n"
1064
      "psrlq $32, %%mm6\n"
1065
      "paddd %%mm6,%%mm0\n"
1066
      "movd %%mm0,%1\n"
1067
      : "+r" (pix1), "=r"(tmp)
1068
      : "r" ((long)line_size) , "g" (h-2)
1069
      : "%ecx");
1070
      return tmp;
1071
}
1072

    
1073
static int hf_noise16_mmx(uint8_t * pix1, int line_size, int h) {
1074
    int tmp;
1075
    uint8_t * pix= pix1;
1076
  asm volatile (
1077
      "movl %3,%%ecx\n"
1078
      "pxor %%mm7,%%mm7\n"
1079
      "pxor %%mm6,%%mm6\n"
1080

    
1081
      "movq (%0),%%mm0\n"
1082
      "movq 1(%0),%%mm1\n"
1083
      "movq %%mm0, %%mm2\n"
1084
      "movq %%mm1, %%mm3\n"
1085
      "punpcklbw %%mm7,%%mm0\n"
1086
      "punpcklbw %%mm7,%%mm1\n"
1087
      "punpckhbw %%mm7,%%mm2\n"
1088
      "punpckhbw %%mm7,%%mm3\n"
1089
      "psubw %%mm1, %%mm0\n"
1090
      "psubw %%mm3, %%mm2\n"
1091

    
1092
      "add %2,%0\n"
1093

    
1094
      "movq (%0),%%mm4\n"
1095
      "movq 1(%0),%%mm1\n"
1096
      "movq %%mm4, %%mm5\n"
1097
      "movq %%mm1, %%mm3\n"
1098
      "punpcklbw %%mm7,%%mm4\n"
1099
      "punpcklbw %%mm7,%%mm1\n"
1100
      "punpckhbw %%mm7,%%mm5\n"
1101
      "punpckhbw %%mm7,%%mm3\n"
1102
      "psubw %%mm1, %%mm4\n"
1103
      "psubw %%mm3, %%mm5\n"
1104
      "psubw %%mm4, %%mm0\n"
1105
      "psubw %%mm5, %%mm2\n"
1106
      "pxor %%mm3, %%mm3\n"
1107
      "pxor %%mm1, %%mm1\n"
1108
      "pcmpgtw %%mm0, %%mm3\n\t"
1109
      "pcmpgtw %%mm2, %%mm1\n\t"
1110
      "pxor %%mm3, %%mm0\n"
1111
      "pxor %%mm1, %%mm2\n"
1112
      "psubw %%mm3, %%mm0\n"
1113
      "psubw %%mm1, %%mm2\n"
1114
      "paddw %%mm0, %%mm2\n"
1115
      "paddw %%mm2, %%mm6\n"
1116

    
1117
      "add %2,%0\n"
1118
      "1:\n"
1119

    
1120
      "movq (%0),%%mm0\n"
1121
      "movq 1(%0),%%mm1\n"
1122
      "movq %%mm0, %%mm2\n"
1123
      "movq %%mm1, %%mm3\n"
1124
      "punpcklbw %%mm7,%%mm0\n"
1125
      "punpcklbw %%mm7,%%mm1\n"
1126
      "punpckhbw %%mm7,%%mm2\n"
1127
      "punpckhbw %%mm7,%%mm3\n"
1128
      "psubw %%mm1, %%mm0\n"
1129
      "psubw %%mm3, %%mm2\n"
1130
      "psubw %%mm0, %%mm4\n"
1131
      "psubw %%mm2, %%mm5\n"
1132
      "pxor %%mm3, %%mm3\n"
1133
      "pxor %%mm1, %%mm1\n"
1134
      "pcmpgtw %%mm4, %%mm3\n\t"
1135
      "pcmpgtw %%mm5, %%mm1\n\t"
1136
      "pxor %%mm3, %%mm4\n"
1137
      "pxor %%mm1, %%mm5\n"
1138
      "psubw %%mm3, %%mm4\n"
1139
      "psubw %%mm1, %%mm5\n"
1140
      "paddw %%mm4, %%mm5\n"
1141
      "paddw %%mm5, %%mm6\n"
1142

    
1143
      "add %2,%0\n"
1144

    
1145
      "movq (%0),%%mm4\n"
1146
      "movq 1(%0),%%mm1\n"
1147
      "movq %%mm4, %%mm5\n"
1148
      "movq %%mm1, %%mm3\n"
1149
      "punpcklbw %%mm7,%%mm4\n"
1150
      "punpcklbw %%mm7,%%mm1\n"
1151
      "punpckhbw %%mm7,%%mm5\n"
1152
      "punpckhbw %%mm7,%%mm3\n"
1153
      "psubw %%mm1, %%mm4\n"
1154
      "psubw %%mm3, %%mm5\n"
1155
      "psubw %%mm4, %%mm0\n"
1156
      "psubw %%mm5, %%mm2\n"
1157
      "pxor %%mm3, %%mm3\n"
1158
      "pxor %%mm1, %%mm1\n"
1159
      "pcmpgtw %%mm0, %%mm3\n\t"
1160
      "pcmpgtw %%mm2, %%mm1\n\t"
1161
      "pxor %%mm3, %%mm0\n"
1162
      "pxor %%mm1, %%mm2\n"
1163
      "psubw %%mm3, %%mm0\n"
1164
      "psubw %%mm1, %%mm2\n"
1165
      "paddw %%mm0, %%mm2\n"
1166
      "paddw %%mm2, %%mm6\n"
1167

    
1168
      "add %2,%0\n"
1169
      "subl $2, %%ecx\n"
1170
      " jnz 1b\n"
1171

    
1172
      "movq %%mm6, %%mm0\n"
1173
      "punpcklwd %%mm7,%%mm0\n"
1174
      "punpckhwd %%mm7,%%mm6\n"
1175
      "paddd %%mm0, %%mm6\n"
1176

    
1177
      "movq %%mm6,%%mm0\n"
1178
      "psrlq $32, %%mm6\n"
1179
      "paddd %%mm6,%%mm0\n"
1180
      "movd %%mm0,%1\n"
1181
      : "+r" (pix1), "=r"(tmp)
1182
      : "r" ((long)line_size) , "g" (h-2)
1183
      : "%ecx");
1184
      return tmp + hf_noise8_mmx(pix+8, line_size, h);
1185
}
1186

    
1187
static int nsse16_mmx(void *p, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
1188
    MpegEncContext *c = p;
1189
    int score1, score2;
1190

    
1191
    if(c) score1 = c->dsp.sse[0](c, pix1, pix2, line_size, h);
1192
    else  score1 = sse16_mmx(c, pix1, pix2, line_size, h);
1193
    score2= hf_noise16_mmx(pix1, line_size, h) - hf_noise16_mmx(pix2, line_size, h);
1194

    
1195
    if(c) return score1 + FFABS(score2)*c->avctx->nsse_weight;
1196
    else  return score1 + FFABS(score2)*8;
1197
}
1198

    
1199
static int nsse8_mmx(void *p, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
1200
    MpegEncContext *c = p;
1201
    int score1= sse8_mmx(c, pix1, pix2, line_size, h);
1202
    int score2= hf_noise8_mmx(pix1, line_size, h) - hf_noise8_mmx(pix2, line_size, h);
1203

    
1204
    if(c) return score1 + FFABS(score2)*c->avctx->nsse_weight;
1205
    else  return score1 + FFABS(score2)*8;
1206
}
1207

    
1208
static int vsad_intra16_mmx(void *v, uint8_t * pix, uint8_t * dummy, int line_size, int h) {
1209
    int tmp;
1210

    
1211
    assert( (((int)pix) & 7) == 0);
1212
    assert((line_size &7) ==0);
1213

    
1214
#define SUM(in0, in1, out0, out1) \
1215
      "movq (%0), %%mm2\n"\
1216
      "movq 8(%0), %%mm3\n"\
1217
      "add %2,%0\n"\
1218
      "movq %%mm2, " #out0 "\n"\
1219
      "movq %%mm3, " #out1 "\n"\
1220
      "psubusb " #in0 ", %%mm2\n"\
1221
      "psubusb " #in1 ", %%mm3\n"\
1222
      "psubusb " #out0 ", " #in0 "\n"\
1223
      "psubusb " #out1 ", " #in1 "\n"\
1224
      "por %%mm2, " #in0 "\n"\
1225
      "por %%mm3, " #in1 "\n"\
1226
      "movq " #in0 ", %%mm2\n"\
1227
      "movq " #in1 ", %%mm3\n"\
1228
      "punpcklbw %%mm7, " #in0 "\n"\
1229
      "punpcklbw %%mm7, " #in1 "\n"\
1230
      "punpckhbw %%mm7, %%mm2\n"\
1231
      "punpckhbw %%mm7, %%mm3\n"\
1232
      "paddw " #in1 ", " #in0 "\n"\
1233
      "paddw %%mm3, %%mm2\n"\
1234
      "paddw %%mm2, " #in0 "\n"\
1235
      "paddw " #in0 ", %%mm6\n"
1236

    
1237

    
1238
  asm volatile (
1239
      "movl %3,%%ecx\n"
1240
      "pxor %%mm6,%%mm6\n"
1241
      "pxor %%mm7,%%mm7\n"
1242
      "movq (%0),%%mm0\n"
1243
      "movq 8(%0),%%mm1\n"
1244
      "add %2,%0\n"
1245
      "subl $2, %%ecx\n"
1246
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1247
      "1:\n"
1248

    
1249
      SUM(%%mm4, %%mm5, %%mm0, %%mm1)
1250

    
1251
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1252

    
1253
      "subl $2, %%ecx\n"
1254
      "jnz 1b\n"
1255

    
1256
      "movq %%mm6,%%mm0\n"
1257
      "psrlq $32, %%mm6\n"
1258
      "paddw %%mm6,%%mm0\n"
1259
      "movq %%mm0,%%mm6\n"
1260
      "psrlq $16, %%mm0\n"
1261
      "paddw %%mm6,%%mm0\n"
1262
      "movd %%mm0,%1\n"
1263
      : "+r" (pix), "=r"(tmp)
1264
      : "r" ((long)line_size) , "m" (h)
1265
      : "%ecx");
1266
    return tmp & 0xFFFF;
1267
}
1268
#undef SUM
1269

    
1270
static int vsad_intra16_mmx2(void *v, uint8_t * pix, uint8_t * dummy, int line_size, int h) {
1271
    int tmp;
1272

    
1273
    assert( (((int)pix) & 7) == 0);
1274
    assert((line_size &7) ==0);
1275

    
1276
#define SUM(in0, in1, out0, out1) \
1277
      "movq (%0), " #out0 "\n"\
1278
      "movq 8(%0), " #out1 "\n"\
1279
      "add %2,%0\n"\
1280
      "psadbw " #out0 ", " #in0 "\n"\
1281
      "psadbw " #out1 ", " #in1 "\n"\
1282
      "paddw " #in1 ", " #in0 "\n"\
1283
      "paddw " #in0 ", %%mm6\n"
1284

    
1285
  asm volatile (
1286
      "movl %3,%%ecx\n"
1287
      "pxor %%mm6,%%mm6\n"
1288
      "pxor %%mm7,%%mm7\n"
1289
      "movq (%0),%%mm0\n"
1290
      "movq 8(%0),%%mm1\n"
1291
      "add %2,%0\n"
1292
      "subl $2, %%ecx\n"
1293
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1294
      "1:\n"
1295

    
1296
      SUM(%%mm4, %%mm5, %%mm0, %%mm1)
1297

    
1298
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1299

    
1300
      "subl $2, %%ecx\n"
1301
      "jnz 1b\n"
1302

    
1303
      "movd %%mm6,%1\n"
1304
      : "+r" (pix), "=r"(tmp)
1305
      : "r" ((long)line_size) , "m" (h)
1306
      : "%ecx");
1307
    return tmp;
1308
}
1309
#undef SUM
1310

    
1311
static int vsad16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
1312
    int tmp;
1313

    
1314
    assert( (((int)pix1) & 7) == 0);
1315
    assert( (((int)pix2) & 7) == 0);
1316
    assert((line_size &7) ==0);
1317

    
1318
#define SUM(in0, in1, out0, out1) \
1319
      "movq (%0),%%mm2\n"\
1320
      "movq (%1)," #out0 "\n"\
1321
      "movq 8(%0),%%mm3\n"\
1322
      "movq 8(%1)," #out1 "\n"\
1323
      "add %3,%0\n"\
1324
      "add %3,%1\n"\
1325
      "psubb " #out0 ", %%mm2\n"\
1326
      "psubb " #out1 ", %%mm3\n"\
1327
      "pxor %%mm7, %%mm2\n"\
1328
      "pxor %%mm7, %%mm3\n"\
1329
      "movq %%mm2, " #out0 "\n"\
1330
      "movq %%mm3, " #out1 "\n"\
1331
      "psubusb " #in0 ", %%mm2\n"\
1332
      "psubusb " #in1 ", %%mm3\n"\
1333
      "psubusb " #out0 ", " #in0 "\n"\
1334
      "psubusb " #out1 ", " #in1 "\n"\
1335
      "por %%mm2, " #in0 "\n"\
1336
      "por %%mm3, " #in1 "\n"\
1337
      "movq " #in0 ", %%mm2\n"\
1338
      "movq " #in1 ", %%mm3\n"\
1339
      "punpcklbw %%mm7, " #in0 "\n"\
1340
      "punpcklbw %%mm7, " #in1 "\n"\
1341
      "punpckhbw %%mm7, %%mm2\n"\
1342
      "punpckhbw %%mm7, %%mm3\n"\
1343
      "paddw " #in1 ", " #in0 "\n"\
1344
      "paddw %%mm3, %%mm2\n"\
1345
      "paddw %%mm2, " #in0 "\n"\
1346
      "paddw " #in0 ", %%mm6\n"
1347

    
1348

    
1349
  asm volatile (
1350
      "movl %4,%%ecx\n"
1351
      "pxor %%mm6,%%mm6\n"
1352
      "pcmpeqw %%mm7,%%mm7\n"
1353
      "psllw $15, %%mm7\n"
1354
      "packsswb %%mm7, %%mm7\n"
1355
      "movq (%0),%%mm0\n"
1356
      "movq (%1),%%mm2\n"
1357
      "movq 8(%0),%%mm1\n"
1358
      "movq 8(%1),%%mm3\n"
1359
      "add %3,%0\n"
1360
      "add %3,%1\n"
1361
      "subl $2, %%ecx\n"
1362
      "psubb %%mm2, %%mm0\n"
1363
      "psubb %%mm3, %%mm1\n"
1364
      "pxor %%mm7, %%mm0\n"
1365
      "pxor %%mm7, %%mm1\n"
1366
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1367
      "1:\n"
1368

    
1369
      SUM(%%mm4, %%mm5, %%mm0, %%mm1)
1370

    
1371
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1372

    
1373
      "subl $2, %%ecx\n"
1374
      "jnz 1b\n"
1375

    
1376
      "movq %%mm6,%%mm0\n"
1377
      "psrlq $32, %%mm6\n"
1378
      "paddw %%mm6,%%mm0\n"
1379
      "movq %%mm0,%%mm6\n"
1380
      "psrlq $16, %%mm0\n"
1381
      "paddw %%mm6,%%mm0\n"
1382
      "movd %%mm0,%2\n"
1383
      : "+r" (pix1), "+r" (pix2), "=r"(tmp)
1384
      : "r" ((long)line_size) , "m" (h)
1385
      : "%ecx");
1386
    return tmp & 0x7FFF;
1387
}
1388
#undef SUM
1389

    
1390
static int vsad16_mmx2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
1391
    int tmp;
1392

    
1393
    assert( (((int)pix1) & 7) == 0);
1394
    assert( (((int)pix2) & 7) == 0);
1395
    assert((line_size &7) ==0);
1396

    
1397
#define SUM(in0, in1, out0, out1) \
1398
      "movq (%0)," #out0 "\n"\
1399
      "movq (%1),%%mm2\n"\
1400
      "movq 8(%0)," #out1 "\n"\
1401
      "movq 8(%1),%%mm3\n"\
1402
      "add %3,%0\n"\
1403
      "add %3,%1\n"\
1404
      "psubb %%mm2, " #out0 "\n"\
1405
      "psubb %%mm3, " #out1 "\n"\
1406
      "pxor %%mm7, " #out0 "\n"\
1407
      "pxor %%mm7, " #out1 "\n"\
1408
      "psadbw " #out0 ", " #in0 "\n"\
1409
      "psadbw " #out1 ", " #in1 "\n"\
1410
      "paddw " #in1 ", " #in0 "\n"\
1411
      "paddw " #in0 ", %%mm6\n"
1412

    
1413
  asm volatile (
1414
      "movl %4,%%ecx\n"
1415
      "pxor %%mm6,%%mm6\n"
1416
      "pcmpeqw %%mm7,%%mm7\n"
1417
      "psllw $15, %%mm7\n"
1418
      "packsswb %%mm7, %%mm7\n"
1419
      "movq (%0),%%mm0\n"
1420
      "movq (%1),%%mm2\n"
1421
      "movq 8(%0),%%mm1\n"
1422
      "movq 8(%1),%%mm3\n"
1423
      "add %3,%0\n"
1424
      "add %3,%1\n"
1425
      "subl $2, %%ecx\n"
1426
      "psubb %%mm2, %%mm0\n"
1427
      "psubb %%mm3, %%mm1\n"
1428
      "pxor %%mm7, %%mm0\n"
1429
      "pxor %%mm7, %%mm1\n"
1430
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1431
      "1:\n"
1432

    
1433
      SUM(%%mm4, %%mm5, %%mm0, %%mm1)
1434

    
1435
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1436

    
1437
      "subl $2, %%ecx\n"
1438
      "jnz 1b\n"
1439

    
1440
      "movd %%mm6,%2\n"
1441
      : "+r" (pix1), "+r" (pix2), "=r"(tmp)
1442
      : "r" ((long)line_size) , "m" (h)
1443
      : "%ecx");
1444
    return tmp;
1445
}
1446
#undef SUM
1447

    
1448
static void diff_bytes_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){
1449
    long i=0;
1450
    asm volatile(
1451
        "1:                             \n\t"
1452
        "movq  (%2, %0), %%mm0          \n\t"
1453
        "movq  (%1, %0), %%mm1          \n\t"
1454
        "psubb %%mm0, %%mm1             \n\t"
1455
        "movq %%mm1, (%3, %0)           \n\t"
1456
        "movq 8(%2, %0), %%mm0          \n\t"
1457
        "movq 8(%1, %0), %%mm1          \n\t"
1458
        "psubb %%mm0, %%mm1             \n\t"
1459
        "movq %%mm1, 8(%3, %0)          \n\t"
1460
        "add $16, %0                    \n\t"
1461
        "cmp %4, %0                     \n\t"
1462
        " jb 1b                         \n\t"
1463
        : "+r" (i)
1464
        : "r"(src1), "r"(src2), "r"(dst), "r"((long)w-15)
1465
    );
1466
    for(; i<w; i++)
1467
        dst[i+0] = src1[i+0]-src2[i+0];
1468
}
1469

    
1470
static void sub_hfyu_median_prediction_mmx2(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top){
1471
    long i=0;
1472
    uint8_t l, lt;
1473

    
1474
    asm volatile(
1475
        "1:                             \n\t"
1476
        "movq  -1(%1, %0), %%mm0        \n\t" // LT
1477
        "movq  (%1, %0), %%mm1          \n\t" // T
1478
        "movq  -1(%2, %0), %%mm2        \n\t" // L
1479
        "movq  (%2, %0), %%mm3          \n\t" // X
1480
        "movq %%mm2, %%mm4              \n\t" // L
1481
        "psubb %%mm0, %%mm2             \n\t"
1482
        "paddb %%mm1, %%mm2             \n\t" // L + T - LT
1483
        "movq %%mm4, %%mm5              \n\t" // L
1484
        "pmaxub %%mm1, %%mm4            \n\t" // max(T, L)
1485
        "pminub %%mm5, %%mm1            \n\t" // min(T, L)
1486
        "pminub %%mm2, %%mm4            \n\t"
1487
        "pmaxub %%mm1, %%mm4            \n\t"
1488
        "psubb %%mm4, %%mm3             \n\t" // dst - pred
1489
        "movq %%mm3, (%3, %0)           \n\t"
1490
        "add $8, %0                     \n\t"
1491
        "cmp %4, %0                     \n\t"
1492
        " jb 1b                         \n\t"
1493
        : "+r" (i)
1494
        : "r"(src1), "r"(src2), "r"(dst), "r"((long)w)
1495
    );
1496

    
1497
    l= *left;
1498
    lt= *left_top;
1499

    
1500
    dst[0]= src2[0] - mid_pred(l, src1[0], (l + src1[0] - lt)&0xFF);
1501

    
1502
    *left_top= src1[w-1];
1503
    *left    = src2[w-1];
1504
}
1505

    
1506
#define LBUTTERFLY2(a1,b1,a2,b2)\
1507
    "paddw " #b1 ", " #a1 "           \n\t"\
1508
    "paddw " #b2 ", " #a2 "           \n\t"\
1509
    "paddw " #b1 ", " #b1 "           \n\t"\
1510
    "paddw " #b2 ", " #b2 "           \n\t"\
1511
    "psubw " #a1 ", " #b1 "           \n\t"\
1512
    "psubw " #a2 ", " #b2 "           \n\t"
1513

    
1514
#define HADAMARD48\
1515
        LBUTTERFLY2(%%mm0, %%mm1, %%mm2, %%mm3)\
1516
        LBUTTERFLY2(%%mm4, %%mm5, %%mm6, %%mm7)\
1517
        LBUTTERFLY2(%%mm0, %%mm2, %%mm1, %%mm3)\
1518
        LBUTTERFLY2(%%mm4, %%mm6, %%mm5, %%mm7)\
1519
        LBUTTERFLY2(%%mm0, %%mm4, %%mm1, %%mm5)\
1520
        LBUTTERFLY2(%%mm2, %%mm6, %%mm3, %%mm7)\
1521

    
1522
#define MMABS_MMX(a,z)\
1523
    "pxor " #z ", " #z "              \n\t"\
1524
    "pcmpgtw " #a ", " #z "           \n\t"\
1525
    "pxor " #z ", " #a "              \n\t"\
1526
    "psubw " #z ", " #a "             \n\t"
1527

    
1528
#define MMABS_MMX2(a,z)\
1529
    "pxor " #z ", " #z "              \n\t"\
1530
    "psubw " #a ", " #z "             \n\t"\
1531
    "pmaxsw " #z ", " #a "            \n\t"
1532

    
1533
#define MMABS_SUM_MMX(a,z, sum)\
1534
    MMABS_MMX(a,z)\
1535
    "paddusw " #a ", " #sum "         \n\t"
1536

    
1537
#define MMABS_SUM_MMX2(a,z, sum)\
1538
    MMABS_MMX2(a,z)\
1539
    "paddusw " #a ", " #sum "         \n\t"
1540

    
1541
#define LOAD4(o, a, b, c, d)\
1542
        "movq "#o"(%1), " #a "        \n\t"\
1543
        "movq "#o"+16(%1), " #b "     \n\t"\
1544
        "movq "#o"+32(%1), " #c "     \n\t"\
1545
        "movq "#o"+48(%1), " #d "     \n\t"
1546

    
1547
#define STORE4(o, a, b, c, d)\
1548
        "movq "#a", "#o"(%1)          \n\t"\
1549
        "movq "#b", "#o"+16(%1)       \n\t"\
1550
        "movq "#c", "#o"+32(%1)       \n\t"\
1551
        "movq "#d", "#o"+48(%1)       \n\t"\
1552

    
1553
#define HSUM_MMX(a, t, dst)\
1554
    "movq "#a", "#t"                  \n\t"\
1555
    "psrlq $32, "#a"                  \n\t"\
1556
    "paddusw "#t", "#a"               \n\t"\
1557
    "movq "#a", "#t"                  \n\t"\
1558
    "psrlq $16, "#a"                  \n\t"\
1559
    "paddusw "#t", "#a"               \n\t"\
1560
    "movd "#a", "#dst"                \n\t"\
1561

    
1562
#define HSUM_MMX2(a, t, dst)\
1563
    "pshufw $0x0E, "#a", "#t"         \n\t"\
1564
    "paddusw "#t", "#a"               \n\t"\
1565
    "pshufw $0x01, "#a", "#t"         \n\t"\
1566
    "paddusw "#t", "#a"               \n\t"\
1567
    "movd "#a", "#dst"                \n\t"\
1568

    
1569
#define HADAMARD8_DIFF_MMX(cpu) \
1570
static int hadamard8_diff_##cpu(void *s, uint8_t *src1, uint8_t *src2, int stride, int h){\
1571
    DECLARE_ALIGNED_8(uint64_t, temp[16]);\
1572
    int sum=0;\
1573
\
1574
    assert(h==8);\
1575
\
1576
    diff_pixels_mmx((DCTELEM*)temp, src1, src2, stride);\
1577
\
1578
    asm volatile(\
1579
        LOAD4(0 , %%mm0, %%mm1, %%mm2, %%mm3)\
1580
        LOAD4(64, %%mm4, %%mm5, %%mm6, %%mm7)\
1581
\
1582
        HADAMARD48\
1583
\
1584
        "movq %%mm7, 112(%1)            \n\t"\
1585
\
1586
        TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm7)\
1587
        STORE4(0 , %%mm0, %%mm3, %%mm7, %%mm2)\
1588
\
1589
        "movq 112(%1), %%mm7            \n\t"\
1590
        TRANSPOSE4(%%mm4, %%mm5, %%mm6, %%mm7, %%mm0)\
1591
        STORE4(64, %%mm4, %%mm7, %%mm0, %%mm6)\
1592
\
1593
        LOAD4(8 , %%mm0, %%mm1, %%mm2, %%mm3)\
1594
        LOAD4(72, %%mm4, %%mm5, %%mm6, %%mm7)\
1595
\
1596
        HADAMARD48\
1597
\
1598
        "movq %%mm7, 120(%1)            \n\t"\
1599
\
1600
        TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm7)\
1601
        STORE4(8 , %%mm0, %%mm3, %%mm7, %%mm2)\
1602
\
1603
        "movq 120(%1), %%mm7            \n\t"\
1604
        TRANSPOSE4(%%mm4, %%mm5, %%mm6, %%mm7, %%mm0)\
1605
        "movq %%mm7, %%mm5              \n\t"/*FIXME remove*/\
1606
        "movq %%mm6, %%mm7              \n\t"\
1607
        "movq %%mm0, %%mm6              \n\t"\
1608
\
1609
        LOAD4(64, %%mm0, %%mm1, %%mm2, %%mm3)\
1610
\
1611
        HADAMARD48\
1612
        "movq %%mm7, 64(%1)             \n\t"\
1613
        MMABS(%%mm0, %%mm7)\
1614
        MMABS_SUM(%%mm1, %%mm7, %%mm0)\
1615
        MMABS_SUM(%%mm2, %%mm7, %%mm0)\
1616
        MMABS_SUM(%%mm3, %%mm7, %%mm0)\
1617
        MMABS_SUM(%%mm4, %%mm7, %%mm0)\
1618
        MMABS_SUM(%%mm5, %%mm7, %%mm0)\
1619
        MMABS_SUM(%%mm6, %%mm7, %%mm0)\
1620
        "movq 64(%1), %%mm1             \n\t"\
1621
        MMABS_SUM(%%mm1, %%mm7, %%mm0)\
1622
        "movq %%mm0, 64(%1)             \n\t"\
1623
\
1624
        LOAD4(0 , %%mm0, %%mm1, %%mm2, %%mm3)\
1625
        LOAD4(8 , %%mm4, %%mm5, %%mm6, %%mm7)\
1626
\
1627
        HADAMARD48\
1628
        "movq %%mm7, (%1)               \n\t"\
1629
        MMABS(%%mm0, %%mm7)\
1630
        MMABS_SUM(%%mm1, %%mm7, %%mm0)\
1631
        MMABS_SUM(%%mm2, %%mm7, %%mm0)\
1632
        MMABS_SUM(%%mm3, %%mm7, %%mm0)\
1633
        MMABS_SUM(%%mm4, %%mm7, %%mm0)\
1634
        MMABS_SUM(%%mm5, %%mm7, %%mm0)\
1635
        MMABS_SUM(%%mm6, %%mm7, %%mm0)\
1636
        "movq (%1), %%mm1               \n\t"\
1637
        MMABS_SUM(%%mm1, %%mm7, %%mm0)\
1638
        "movq 64(%1), %%mm1             \n\t"\
1639
        MMABS_SUM(%%mm1, %%mm7, %%mm0)\
1640
\
1641
        HSUM(%%mm0, %%mm1, %0)\
1642
\
1643
        : "=r" (sum)\
1644
        : "r"(temp)\
1645
    );\
1646
    return sum&0xFFFF;\
1647
}
1648

    
1649
#define MMABS(a,z)         MMABS_MMX(a,z)
1650
#define MMABS_SUM(a,z,sum) MMABS_SUM_MMX(a,z,sum)
1651
#define HSUM(a,t,dst)      HSUM_MMX(a,t,dst)
1652
HADAMARD8_DIFF_MMX(mmx)
1653
#undef MMABS
1654
#undef MMABS_SUM
1655
#undef HSUM
1656

    
1657
#define MMABS(a,z)         MMABS_MMX2(a,z)
1658
#define MMABS_SUM(a,z,sum) MMABS_SUM_MMX2(a,z,sum)
1659
#define HSUM(a,t,dst)      HSUM_MMX2(a,t,dst)
1660
HADAMARD8_DIFF_MMX(mmx2)
1661
#undef MMABS
1662
#undef MMABS_SUM
1663
#undef HSUM
1664

    
1665
WARPER8_16_SQ(hadamard8_diff_mmx, hadamard8_diff16_mmx)
1666
WARPER8_16_SQ(hadamard8_diff_mmx2, hadamard8_diff16_mmx2)
1667

    
1668
static int ssd_int8_vs_int16_mmx(int8_t *pix1, int16_t *pix2, int size){
1669
    int sum;
1670
    long i=size;
1671
    asm volatile(
1672
        "pxor %%mm4, %%mm4 \n"
1673
        "1: \n"
1674
        "sub $8, %0 \n"
1675
        "movq (%2,%0), %%mm2 \n"
1676
        "movq (%3,%0,2), %%mm0 \n"
1677
        "movq 8(%3,%0,2), %%mm1 \n"
1678
        "punpckhbw %%mm2, %%mm3 \n"
1679
        "punpcklbw %%mm2, %%mm2 \n"
1680
        "psraw $8, %%mm3 \n"
1681
        "psraw $8, %%mm2 \n"
1682
        "psubw %%mm3, %%mm1 \n"
1683
        "psubw %%mm2, %%mm0 \n"
1684
        "pmaddwd %%mm1, %%mm1 \n"
1685
        "pmaddwd %%mm0, %%mm0 \n"
1686
        "paddd %%mm1, %%mm4 \n"
1687
        "paddd %%mm0, %%mm4 \n"
1688
        "jg 1b \n"
1689
        "movq %%mm4, %%mm3 \n"
1690
        "psrlq $32, %%mm3 \n"
1691
        "paddd %%mm3, %%mm4 \n"
1692
        "movd %%mm4, %1 \n"
1693
        :"+r"(i), "=r"(sum)
1694
        :"r"(pix1), "r"(pix2)
1695
    );
1696
    return sum;
1697
}
1698

    
1699
#endif //CONFIG_ENCODERS
1700

    
1701
#define put_no_rnd_pixels8_mmx(a,b,c,d) put_pixels8_mmx(a,b,c,d)
1702
#define put_no_rnd_pixels16_mmx(a,b,c,d) put_pixels16_mmx(a,b,c,d)
1703

    
1704
#define QPEL_V_LOW(m3,m4,m5,m6, pw_20, pw_3, rnd, in0, in1, in2, in7, out, OP)\
1705
        "paddw " #m4 ", " #m3 "           \n\t" /* x1 */\
1706
        "movq "MANGLE(ff_pw_20)", %%mm4   \n\t" /* 20 */\
1707
        "pmullw " #m3 ", %%mm4            \n\t" /* 20x1 */\
1708
        "movq "#in7", " #m3 "             \n\t" /* d */\
1709
        "movq "#in0", %%mm5               \n\t" /* D */\
1710
        "paddw " #m3 ", %%mm5             \n\t" /* x4 */\
1711
        "psubw %%mm5, %%mm4               \n\t" /* 20x1 - x4 */\
1712
        "movq "#in1", %%mm5               \n\t" /* C */\
1713
        "movq "#in2", %%mm6               \n\t" /* B */\
1714
        "paddw " #m6 ", %%mm5             \n\t" /* x3 */\
1715
        "paddw " #m5 ", %%mm6             \n\t" /* x2 */\
1716
        "paddw %%mm6, %%mm6               \n\t" /* 2x2 */\
1717
        "psubw %%mm6, %%mm5               \n\t" /* -2x2 + x3 */\
1718
        "pmullw "MANGLE(ff_pw_3)", %%mm5  \n\t" /* -6x2 + 3x3 */\
1719
        "paddw " #rnd ", %%mm4            \n\t" /* x2 */\
1720
        "paddw %%mm4, %%mm5               \n\t" /* 20x1 - 6x2 + 3x3 - x4 */\
1721
        "psraw $5, %%mm5                  \n\t"\
1722
        "packuswb %%mm5, %%mm5            \n\t"\
1723
        OP(%%mm5, out, %%mm7, d)
1724

    
1725
#define QPEL_BASE(OPNAME, ROUNDER, RND, OP_MMX2, OP_3DNOW)\
1726
static void OPNAME ## mpeg4_qpel16_h_lowpass_mmx2(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1727
    uint64_t temp;\
1728
\
1729
    asm volatile(\
1730
        "pxor %%mm7, %%mm7                \n\t"\
1731
        "1:                               \n\t"\
1732
        "movq  (%0), %%mm0                \n\t" /* ABCDEFGH */\
1733
        "movq %%mm0, %%mm1                \n\t" /* ABCDEFGH */\
1734
        "movq %%mm0, %%mm2                \n\t" /* ABCDEFGH */\
1735
        "punpcklbw %%mm7, %%mm0           \n\t" /* 0A0B0C0D */\
1736
        "punpckhbw %%mm7, %%mm1           \n\t" /* 0E0F0G0H */\
1737
        "pshufw $0x90, %%mm0, %%mm5       \n\t" /* 0A0A0B0C */\
1738
        "pshufw $0x41, %%mm0, %%mm6       \n\t" /* 0B0A0A0B */\
1739
        "movq %%mm2, %%mm3                \n\t" /* ABCDEFGH */\
1740
        "movq %%mm2, %%mm4                \n\t" /* ABCDEFGH */\
1741
        "psllq $8, %%mm2                  \n\t" /* 0ABCDEFG */\
1742
        "psllq $16, %%mm3                 \n\t" /* 00ABCDEF */\
1743
        "psllq $24, %%mm4                 \n\t" /* 000ABCDE */\
1744
        "punpckhbw %%mm7, %%mm2           \n\t" /* 0D0E0F0G */\
1745
        "punpckhbw %%mm7, %%mm3           \n\t" /* 0C0D0E0F */\
1746
        "punpckhbw %%mm7, %%mm4           \n\t" /* 0B0C0D0E */\
1747
        "paddw %%mm3, %%mm5               \n\t" /* b */\
1748
        "paddw %%mm2, %%mm6               \n\t" /* c */\
1749
        "paddw %%mm5, %%mm5               \n\t" /* 2b */\
1750
        "psubw %%mm5, %%mm6               \n\t" /* c - 2b */\
1751
        "pshufw $0x06, %%mm0, %%mm5       \n\t" /* 0C0B0A0A */\
1752
        "pmullw "MANGLE(ff_pw_3)", %%mm6  \n\t" /* 3c - 6b */\
1753
        "paddw %%mm4, %%mm0               \n\t" /* a */\
1754
        "paddw %%mm1, %%mm5               \n\t" /* d */\
1755
        "pmullw "MANGLE(ff_pw_20)", %%mm0 \n\t" /* 20a */\
1756
        "psubw %%mm5, %%mm0               \n\t" /* 20a - d */\
1757
        "paddw %6, %%mm6                  \n\t"\
1758
        "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
1759
        "psraw $5, %%mm0                  \n\t"\
1760
        "movq %%mm0, %5                   \n\t"\
1761
        /* mm1=EFGH, mm2=DEFG, mm3=CDEF, mm4=BCDE, mm7=0 */\
1762
        \
1763
        "movq 5(%0), %%mm0                \n\t" /* FGHIJKLM */\
1764
        "movq %%mm0, %%mm5                \n\t" /* FGHIJKLM */\
1765
        "movq %%mm0, %%mm6                \n\t" /* FGHIJKLM */\
1766
        "psrlq $8, %%mm0                  \n\t" /* GHIJKLM0 */\
1767
        "psrlq $16, %%mm5                 \n\t" /* HIJKLM00 */\
1768
        "punpcklbw %%mm7, %%mm0           \n\t" /* 0G0H0I0J */\
1769
        "punpcklbw %%mm7, %%mm5           \n\t" /* 0H0I0J0K */\
1770
        "paddw %%mm0, %%mm2               \n\t" /* b */\
1771
        "paddw %%mm5, %%mm3               \n\t" /* c */\
1772
        "paddw %%mm2, %%mm2               \n\t" /* 2b */\
1773
        "psubw %%mm2, %%mm3               \n\t" /* c - 2b */\
1774
        "movq %%mm6, %%mm2                \n\t" /* FGHIJKLM */\
1775
        "psrlq $24, %%mm6                 \n\t" /* IJKLM000 */\
1776
        "punpcklbw %%mm7, %%mm2           \n\t" /* 0F0G0H0I */\
1777
        "punpcklbw %%mm7, %%mm6           \n\t" /* 0I0J0K0L */\
1778
        "pmullw "MANGLE(ff_pw_3)", %%mm3  \n\t" /* 3c - 6b */\
1779
        "paddw %%mm2, %%mm1               \n\t" /* a */\
1780
        "paddw %%mm6, %%mm4               \n\t" /* d */\
1781
        "pmullw "MANGLE(ff_pw_20)", %%mm1 \n\t" /* 20a */\
1782
        "psubw %%mm4, %%mm3               \n\t" /* - 6b +3c - d */\
1783
        "paddw %6, %%mm1                  \n\t"\
1784
        "paddw %%mm1, %%mm3               \n\t" /* 20a - 6b +3c - d */\
1785
        "psraw $5, %%mm3                  \n\t"\
1786
        "movq %5, %%mm1                   \n\t"\
1787
        "packuswb %%mm3, %%mm1            \n\t"\
1788
        OP_MMX2(%%mm1, (%1),%%mm4, q)\
1789
        /* mm0= GHIJ, mm2=FGHI, mm5=HIJK, mm6=IJKL, mm7=0 */\
1790
        \
1791
        "movq 9(%0), %%mm1                \n\t" /* JKLMNOPQ */\
1792
        "movq %%mm1, %%mm4                \n\t" /* JKLMNOPQ */\
1793
        "movq %%mm1, %%mm3                \n\t" /* JKLMNOPQ */\
1794
        "psrlq $8, %%mm1                  \n\t" /* KLMNOPQ0 */\
1795
        "psrlq $16, %%mm4                 \n\t" /* LMNOPQ00 */\
1796
        "punpcklbw %%mm7, %%mm1           \n\t" /* 0K0L0M0N */\
1797
        "punpcklbw %%mm7, %%mm4           \n\t" /* 0L0M0N0O */\
1798
        "paddw %%mm1, %%mm5               \n\t" /* b */\
1799
        "paddw %%mm4, %%mm0               \n\t" /* c */\
1800
        "paddw %%mm5, %%mm5               \n\t" /* 2b */\
1801
        "psubw %%mm5, %%mm0               \n\t" /* c - 2b */\
1802
        "movq %%mm3, %%mm5                \n\t" /* JKLMNOPQ */\
1803
        "psrlq $24, %%mm3                 \n\t" /* MNOPQ000 */\
1804
        "pmullw "MANGLE(ff_pw_3)", %%mm0  \n\t" /* 3c - 6b */\
1805
        "punpcklbw %%mm7, %%mm3           \n\t" /* 0M0N0O0P */\
1806
        "paddw %%mm3, %%mm2               \n\t" /* d */\
1807
        "psubw %%mm2, %%mm0               \n\t" /* -6b + 3c - d */\
1808
        "movq %%mm5, %%mm2                \n\t" /* JKLMNOPQ */\
1809
        "punpcklbw %%mm7, %%mm2           \n\t" /* 0J0K0L0M */\
1810
        "punpckhbw %%mm7, %%mm5           \n\t" /* 0N0O0P0Q */\
1811
        "paddw %%mm2, %%mm6               \n\t" /* a */\
1812
        "pmullw "MANGLE(ff_pw_20)", %%mm6 \n\t" /* 20a */\
1813
        "paddw %6, %%mm0                  \n\t"\
1814
        "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
1815
        "psraw $5, %%mm0                  \n\t"\
1816
        /* mm1=KLMN, mm2=JKLM, mm3=MNOP, mm4=LMNO, mm5=NOPQ mm7=0 */\
1817
        \
1818
        "paddw %%mm5, %%mm3               \n\t" /* a */\
1819
        "pshufw $0xF9, %%mm5, %%mm6       \n\t" /* 0O0P0Q0Q */\
1820
        "paddw %%mm4, %%mm6               \n\t" /* b */\
1821
        "pshufw $0xBE, %%mm5, %%mm4       \n\t" /* 0P0Q0Q0P */\
1822
        "pshufw $0x6F, %%mm5, %%mm5       \n\t" /* 0Q0Q0P0O */\
1823
        "paddw %%mm1, %%mm4               \n\t" /* c */\
1824
        "paddw %%mm2, %%mm5               \n\t" /* d */\
1825
        "paddw %%mm6, %%mm6               \n\t" /* 2b */\
1826
        "psubw %%mm6, %%mm4               \n\t" /* c - 2b */\
1827
        "pmullw "MANGLE(ff_pw_20)", %%mm3 \n\t" /* 20a */\
1828
        "pmullw "MANGLE(ff_pw_3)", %%mm4  \n\t" /* 3c - 6b */\
1829
        "psubw %%mm5, %%mm3               \n\t" /* -6b + 3c - d */\
1830
        "paddw %6, %%mm4                  \n\t"\
1831
        "paddw %%mm3, %%mm4               \n\t" /* 20a - 6b + 3c - d */\
1832
        "psraw $5, %%mm4                  \n\t"\
1833
        "packuswb %%mm4, %%mm0            \n\t"\
1834
        OP_MMX2(%%mm0, 8(%1), %%mm4, q)\
1835
        \
1836
        "add %3, %0                       \n\t"\
1837
        "add %4, %1                       \n\t"\
1838
        "decl %2                          \n\t"\
1839
        " jnz 1b                          \n\t"\
1840
        : "+a"(src), "+c"(dst), "+m"(h)\
1841
        : "d"((long)srcStride), "S"((long)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(temp), "m"(ROUNDER)\
1842
        : "memory"\
1843
    );\
1844
}\
1845
\
1846
static void OPNAME ## mpeg4_qpel16_h_lowpass_3dnow(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1847
    int i;\
1848
    int16_t temp[16];\
1849
    /* quick HACK, XXX FIXME MUST be optimized */\
1850
    for(i=0; i<h; i++)\
1851
    {\
1852
        temp[ 0]= (src[ 0]+src[ 1])*20 - (src[ 0]+src[ 2])*6 + (src[ 1]+src[ 3])*3 - (src[ 2]+src[ 4]);\
1853
        temp[ 1]= (src[ 1]+src[ 2])*20 - (src[ 0]+src[ 3])*6 + (src[ 0]+src[ 4])*3 - (src[ 1]+src[ 5]);\
1854
        temp[ 2]= (src[ 2]+src[ 3])*20 - (src[ 1]+src[ 4])*6 + (src[ 0]+src[ 5])*3 - (src[ 0]+src[ 6]);\
1855
        temp[ 3]= (src[ 3]+src[ 4])*20 - (src[ 2]+src[ 5])*6 + (src[ 1]+src[ 6])*3 - (src[ 0]+src[ 7]);\
1856
        temp[ 4]= (src[ 4]+src[ 5])*20 - (src[ 3]+src[ 6])*6 + (src[ 2]+src[ 7])*3 - (src[ 1]+src[ 8]);\
1857
        temp[ 5]= (src[ 5]+src[ 6])*20 - (src[ 4]+src[ 7])*6 + (src[ 3]+src[ 8])*3 - (src[ 2]+src[ 9]);\
1858
        temp[ 6]= (src[ 6]+src[ 7])*20 - (src[ 5]+src[ 8])*6 + (src[ 4]+src[ 9])*3 - (src[ 3]+src[10]);\
1859
        temp[ 7]= (src[ 7]+src[ 8])*20 - (src[ 6]+src[ 9])*6 + (src[ 5]+src[10])*3 - (src[ 4]+src[11]);\
1860
        temp[ 8]= (src[ 8]+src[ 9])*20 - (src[ 7]+src[10])*6 + (src[ 6]+src[11])*3 - (src[ 5]+src[12]);\
1861
        temp[ 9]= (src[ 9]+src[10])*20 - (src[ 8]+src[11])*6 + (src[ 7]+src[12])*3 - (src[ 6]+src[13]);\
1862
        temp[10]= (src[10]+src[11])*20 - (src[ 9]+src[12])*6 + (src[ 8]+src[13])*3 - (src[ 7]+src[14]);\
1863
        temp[11]= (src[11]+src[12])*20 - (src[10]+src[13])*6 + (src[ 9]+src[14])*3 - (src[ 8]+src[15]);\
1864
        temp[12]= (src[12]+src[13])*20 - (src[11]+src[14])*6 + (src[10]+src[15])*3 - (src[ 9]+src[16]);\
1865
        temp[13]= (src[13]+src[14])*20 - (src[12]+src[15])*6 + (src[11]+src[16])*3 - (src[10]+src[16]);\
1866
        temp[14]= (src[14]+src[15])*20 - (src[13]+src[16])*6 + (src[12]+src[16])*3 - (src[11]+src[15]);\
1867
        temp[15]= (src[15]+src[16])*20 - (src[14]+src[16])*6 + (src[13]+src[15])*3 - (src[12]+src[14]);\
1868
        asm volatile(\
1869
            "movq (%0), %%mm0               \n\t"\
1870
            "movq 8(%0), %%mm1              \n\t"\
1871
            "paddw %2, %%mm0                \n\t"\
1872
            "paddw %2, %%mm1                \n\t"\
1873
            "psraw $5, %%mm0                \n\t"\
1874
            "psraw $5, %%mm1                \n\t"\
1875
            "packuswb %%mm1, %%mm0          \n\t"\
1876
            OP_3DNOW(%%mm0, (%1), %%mm1, q)\
1877
            "movq 16(%0), %%mm0             \n\t"\
1878
            "movq 24(%0), %%mm1             \n\t"\
1879
            "paddw %2, %%mm0                \n\t"\
1880
            "paddw %2, %%mm1                \n\t"\
1881
            "psraw $5, %%mm0                \n\t"\
1882
            "psraw $5, %%mm1                \n\t"\
1883
            "packuswb %%mm1, %%mm0          \n\t"\
1884
            OP_3DNOW(%%mm0, 8(%1), %%mm1, q)\
1885
            :: "r"(temp), "r"(dst), "m"(ROUNDER)\
1886
            : "memory"\
1887
        );\
1888
        dst+=dstStride;\
1889
        src+=srcStride;\
1890
    }\
1891
}\
1892
\
1893
static void OPNAME ## mpeg4_qpel8_h_lowpass_mmx2(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1894
    uint64_t temp;\
1895
\
1896
    asm volatile(\
1897
        "pxor %%mm7, %%mm7                \n\t"\
1898
        "1:                               \n\t"\
1899
        "movq  (%0), %%mm0                \n\t" /* ABCDEFGH */\
1900
        "movq %%mm0, %%mm1                \n\t" /* ABCDEFGH */\
1901
        "movq %%mm0, %%mm2                \n\t" /* ABCDEFGH */\
1902
        "punpcklbw %%mm7, %%mm0           \n\t" /* 0A0B0C0D */\
1903
        "punpckhbw %%mm7, %%mm1           \n\t" /* 0E0F0G0H */\
1904
        "pshufw $0x90, %%mm0, %%mm5       \n\t" /* 0A0A0B0C */\
1905
        "pshufw $0x41, %%mm0, %%mm6       \n\t" /* 0B0A0A0B */\
1906
        "movq %%mm2, %%mm3                \n\t" /* ABCDEFGH */\
1907
        "movq %%mm2, %%mm4                \n\t" /* ABCDEFGH */\
1908
        "psllq $8, %%mm2                  \n\t" /* 0ABCDEFG */\
1909
        "psllq $16, %%mm3                 \n\t" /* 00ABCDEF */\
1910
        "psllq $24, %%mm4                 \n\t" /* 000ABCDE */\
1911
        "punpckhbw %%mm7, %%mm2           \n\t" /* 0D0E0F0G */\
1912
        "punpckhbw %%mm7, %%mm3           \n\t" /* 0C0D0E0F */\
1913
        "punpckhbw %%mm7, %%mm4           \n\t" /* 0B0C0D0E */\
1914
        "paddw %%mm3, %%mm5               \n\t" /* b */\
1915
        "paddw %%mm2, %%mm6               \n\t" /* c */\
1916
        "paddw %%mm5, %%mm5               \n\t" /* 2b */\
1917
        "psubw %%mm5, %%mm6               \n\t" /* c - 2b */\
1918
        "pshufw $0x06, %%mm0, %%mm5       \n\t" /* 0C0B0A0A */\
1919
        "pmullw "MANGLE(ff_pw_3)", %%mm6  \n\t" /* 3c - 6b */\
1920
        "paddw %%mm4, %%mm0               \n\t" /* a */\
1921
        "paddw %%mm1, %%mm5               \n\t" /* d */\
1922
        "pmullw "MANGLE(ff_pw_20)", %%mm0 \n\t" /* 20a */\
1923
        "psubw %%mm5, %%mm0               \n\t" /* 20a - d */\
1924
        "paddw %6, %%mm6                  \n\t"\
1925
        "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
1926
        "psraw $5, %%mm0                  \n\t"\
1927
        /* mm1=EFGH, mm2=DEFG, mm3=CDEF, mm4=BCDE, mm7=0 */\
1928
        \
1929
        "movd 5(%0), %%mm5                \n\t" /* FGHI */\
1930
        "punpcklbw %%mm7, %%mm5           \n\t" /* 0F0G0H0I */\
1931
        "pshufw $0xF9, %%mm5, %%mm6       \n\t" /* 0G0H0I0I */\
1932
        "paddw %%mm5, %%mm1               \n\t" /* a */\
1933
        "paddw %%mm6, %%mm2               \n\t" /* b */\
1934
        "pshufw $0xBE, %%mm5, %%mm6       \n\t" /* 0H0I0I0H */\
1935
        "pshufw $0x6F, %%mm5, %%mm5       \n\t" /* 0I0I0H0G */\
1936
        "paddw %%mm6, %%mm3               \n\t" /* c */\
1937
        "paddw %%mm5, %%mm4               \n\t" /* d */\
1938
        "paddw %%mm2, %%mm2               \n\t" /* 2b */\
1939
        "psubw %%mm2, %%mm3               \n\t" /* c - 2b */\
1940
        "pmullw "MANGLE(ff_pw_20)", %%mm1 \n\t" /* 20a */\
1941
        "pmullw "MANGLE(ff_pw_3)", %%mm3  \n\t" /* 3c - 6b */\
1942
        "psubw %%mm4, %%mm3               \n\t" /* -6b + 3c - d */\
1943
        "paddw %6, %%mm1                  \n\t"\
1944
        "paddw %%mm1, %%mm3               \n\t" /* 20a - 6b + 3c - d */\
1945
        "psraw $5, %%mm3                  \n\t"\
1946
        "packuswb %%mm3, %%mm0            \n\t"\
1947
        OP_MMX2(%%mm0, (%1), %%mm4, q)\
1948
        \
1949
        "add %3, %0                       \n\t"\
1950
        "add %4, %1                       \n\t"\
1951
        "decl %2                          \n\t"\
1952
        " jnz 1b                          \n\t"\
1953
        : "+a"(src), "+c"(dst), "+m"(h)\
1954
        : "S"((long)srcStride), "D"((long)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(temp), "m"(ROUNDER)\
1955
        : "memory"\
1956
    );\
1957
}\
1958
\
1959
static void OPNAME ## mpeg4_qpel8_h_lowpass_3dnow(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1960
    int i;\
1961
    int16_t temp[8];\
1962
    /* quick HACK, XXX FIXME MUST be optimized */\
1963
    for(i=0; i<h; i++)\
1964
    {\
1965
        temp[ 0]= (src[ 0]+src[ 1])*20 - (src[ 0]+src[ 2])*6 + (src[ 1]+src[ 3])*3 - (src[ 2]+src[ 4]);\
1966
        temp[ 1]= (src[ 1]+src[ 2])*20 - (src[ 0]+src[ 3])*6 + (src[ 0]+src[ 4])*3 - (src[ 1]+src[ 5]);\
1967
        temp[ 2]= (src[ 2]+src[ 3])*20 - (src[ 1]+src[ 4])*6 + (src[ 0]+src[ 5])*3 - (src[ 0]+src[ 6]);\
1968
        temp[ 3]= (src[ 3]+src[ 4])*20 - (src[ 2]+src[ 5])*6 + (src[ 1]+src[ 6])*3 - (src[ 0]+src[ 7]);\
1969
        temp[ 4]= (src[ 4]+src[ 5])*20 - (src[ 3]+src[ 6])*6 + (src[ 2]+src[ 7])*3 - (src[ 1]+src[ 8]);\
1970
        temp[ 5]= (src[ 5]+src[ 6])*20 - (src[ 4]+src[ 7])*6 + (src[ 3]+src[ 8])*3 - (src[ 2]+src[ 8]);\
1971
        temp[ 6]= (src[ 6]+src[ 7])*20 - (src[ 5]+src[ 8])*6 + (src[ 4]+src[ 8])*3 - (src[ 3]+src[ 7]);\
1972
        temp[ 7]= (src[ 7]+src[ 8])*20 - (src[ 6]+src[ 8])*6 + (src[ 5]+src[ 7])*3 - (src[ 4]+src[ 6]);\
1973
        asm volatile(\
1974
            "movq (%0), %%mm0           \n\t"\
1975
            "movq 8(%0), %%mm1          \n\t"\
1976
            "paddw %2, %%mm0            \n\t"\
1977
            "paddw %2, %%mm1            \n\t"\
1978
            "psraw $5, %%mm0            \n\t"\
1979
            "psraw $5, %%mm1            \n\t"\
1980
            "packuswb %%mm1, %%mm0      \n\t"\
1981
            OP_3DNOW(%%mm0, (%1), %%mm1, q)\
1982
            :: "r"(temp), "r"(dst), "m"(ROUNDER)\
1983
            :"memory"\
1984
        );\
1985
        dst+=dstStride;\
1986
        src+=srcStride;\
1987
    }\
1988
}
1989

    
1990
#define QPEL_OP(OPNAME, ROUNDER, RND, OP, MMX)\
1991
\
1992
static void OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
1993
    uint64_t temp[17*4];\
1994
    uint64_t *temp_ptr= temp;\
1995
    int count= 17;\
1996
\
1997
    /*FIXME unroll */\
1998
    asm volatile(\
1999
        "pxor %%mm7, %%mm7              \n\t"\
2000
        "1:                             \n\t"\
2001
        "movq (%0), %%mm0               \n\t"\
2002
        "movq (%0), %%mm1               \n\t"\
2003
        "movq 8(%0), %%mm2              \n\t"\
2004
        "movq 8(%0), %%mm3              \n\t"\
2005
        "punpcklbw %%mm7, %%mm0         \n\t"\
2006
        "punpckhbw %%mm7, %%mm1         \n\t"\
2007
        "punpcklbw %%mm7, %%mm2         \n\t"\
2008
        "punpckhbw %%mm7, %%mm3         \n\t"\
2009
        "movq %%mm0, (%1)               \n\t"\
2010
        "movq %%mm1, 17*8(%1)           \n\t"\
2011
        "movq %%mm2, 2*17*8(%1)         \n\t"\
2012
        "movq %%mm3, 3*17*8(%1)         \n\t"\
2013
        "add $8, %1                     \n\t"\
2014
        "add %3, %0                     \n\t"\
2015
        "decl %2                        \n\t"\
2016
        " jnz 1b                        \n\t"\
2017
        : "+r" (src), "+r" (temp_ptr), "+r"(count)\
2018
        : "r" ((long)srcStride)\
2019
        : "memory"\
2020
    );\
2021
    \
2022
    temp_ptr= temp;\
2023
    count=4;\
2024
    \
2025
/*FIXME reorder for speed */\
2026
    asm volatile(\
2027
        /*"pxor %%mm7, %%mm7              \n\t"*/\
2028
        "1:                             \n\t"\
2029
        "movq (%0), %%mm0               \n\t"\
2030
        "movq 8(%0), %%mm1              \n\t"\
2031
        "movq 16(%0), %%mm2             \n\t"\
2032
        "movq 24(%0), %%mm3             \n\t"\
2033
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 16(%0),  8(%0),   (%0), 32(%0), (%1), OP)\
2034
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5,  8(%0),   (%0),   (%0), 40(%0), (%1, %3), OP)\
2035
        "add %4, %1                     \n\t"\
2036
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5,   (%0),   (%0),  8(%0), 48(%0), (%1), OP)\
2037
        \
2038
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5,   (%0),  8(%0), 16(%0), 56(%0), (%1, %3), OP)\
2039
        "add %4, %1                     \n\t"\
2040
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5,  8(%0), 16(%0), 24(%0), 64(%0), (%1), OP)\
2041
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 16(%0), 24(%0), 32(%0), 72(%0), (%1, %3), OP)\
2042
        "add %4, %1                     \n\t"\
2043
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 24(%0), 32(%0), 40(%0), 80(%0), (%1), OP)\
2044
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 32(%0), 40(%0), 48(%0), 88(%0), (%1, %3), OP)\
2045
        "add %4, %1                     \n\t"\
2046
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 40(%0), 48(%0), 56(%0), 96(%0), (%1), OP)\
2047
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 48(%0), 56(%0), 64(%0),104(%0), (%1, %3), OP)\
2048
        "add %4, %1                     \n\t"\
2049
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 56(%0), 64(%0), 72(%0),112(%0), (%1), OP)\
2050
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 64(%0), 72(%0), 80(%0),120(%0), (%1, %3), OP)\
2051
        "add %4, %1                     \n\t"\
2052
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 72(%0), 80(%0), 88(%0),128(%0), (%1), OP)\
2053
        \
2054
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 80(%0), 88(%0), 96(%0),128(%0), (%1, %3), OP)\
2055
        "add %4, %1                     \n\t"  \
2056
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 88(%0), 96(%0),104(%0),120(%0), (%1), OP)\
2057
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 96(%0),104(%0),112(%0),112(%0), (%1, %3), OP)\
2058
        \
2059
        "add $136, %0                   \n\t"\
2060
        "add %6, %1                     \n\t"\
2061
        "decl %2                        \n\t"\
2062
        " jnz 1b                        \n\t"\
2063
        \
2064
        : "+r"(temp_ptr), "+r"(dst), "+g"(count)\
2065
        : "r"((long)dstStride), "r"(2*(long)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(ROUNDER), "g"(4-14*(long)dstStride)\
2066
        :"memory"\
2067
    );\
2068
}\
2069
\
2070
static void OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
2071
    uint64_t temp[9*2];\
2072
    uint64_t *temp_ptr= temp;\
2073
    int count= 9;\
2074
\
2075
    /*FIXME unroll */\
2076
    asm volatile(\
2077
        "pxor %%mm7, %%mm7              \n\t"\
2078
        "1:                             \n\t"\
2079
        "movq (%0), %%mm0               \n\t"\
2080
        "movq (%0), %%mm1               \n\t"\
2081
        "punpcklbw %%mm7, %%mm0         \n\t"\
2082
        "punpckhbw %%mm7, %%mm1         \n\t"\
2083
        "movq %%mm0, (%1)               \n\t"\
2084
        "movq %%mm1, 9*8(%1)            \n\t"\
2085
        "add $8, %1                     \n\t"\
2086
        "add %3, %0                     \n\t"\
2087
        "decl %2                        \n\t"\
2088
        " jnz 1b                        \n\t"\
2089
        : "+r" (src), "+r" (temp_ptr), "+r"(count)\
2090
        : "r" ((long)srcStride)\
2091
        : "memory"\
2092
    );\
2093
    \
2094
    temp_ptr= temp;\
2095
    count=2;\
2096
    \
2097
/*FIXME reorder for speed */\
2098
    asm volatile(\
2099
        /*"pxor %%mm7, %%mm7              \n\t"*/\
2100
        "1:                             \n\t"\
2101
        "movq (%0), %%mm0               \n\t"\
2102
        "movq 8(%0), %%mm1              \n\t"\
2103
        "movq 16(%0), %%mm2             \n\t"\
2104
        "movq 24(%0), %%mm3             \n\t"\
2105
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 16(%0),  8(%0),   (%0), 32(%0), (%1), OP)\
2106
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5,  8(%0),   (%0),   (%0), 40(%0), (%1, %3), OP)\
2107
        "add %4, %1                     \n\t"\
2108
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5,   (%0),   (%0),  8(%0), 48(%0), (%1), OP)\
2109
        \
2110
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5,   (%0),  8(%0), 16(%0), 56(%0), (%1, %3), OP)\
2111
        "add %4, %1                     \n\t"\
2112
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5,  8(%0), 16(%0), 24(%0), 64(%0), (%1), OP)\
2113
        \
2114
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 16(%0), 24(%0), 32(%0), 64(%0), (%1, %3), OP)\
2115
        "add %4, %1                     \n\t"\
2116
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 24(%0), 32(%0), 40(%0), 56(%0), (%1), OP)\
2117
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 32(%0), 40(%0), 48(%0), 48(%0), (%1, %3), OP)\
2118
                \
2119
        "add $72, %0                    \n\t"\
2120
        "add %6, %1                     \n\t"\
2121
        "decl %2                        \n\t"\
2122
        " jnz 1b                        \n\t"\
2123
         \
2124
        : "+r"(temp_ptr), "+r"(dst), "+g"(count)\
2125
        : "r"((long)dstStride), "r"(2*(long)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(ROUNDER), "g"(4-6*(long)dstStride)\
2126
        : "memory"\
2127
   );\
2128
}\
2129
\
2130
static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src, int stride){\
2131
    OPNAME ## pixels8_mmx(dst, src, stride, 8);\
2132
}\
2133
\
2134
static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2135
    uint64_t temp[8];\
2136
    uint8_t * const half= (uint8_t*)temp;\
2137
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8, stride, 8);\
2138
    OPNAME ## pixels8_l2_ ## MMX(dst, src, half, stride, stride, 8);\
2139
}\
2140
\
2141
static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2142
    OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride, stride, 8);\
2143
}\
2144
\
2145
static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2146
    uint64_t temp[8];\
2147
    uint8_t * const half= (uint8_t*)temp;\
2148
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8, stride, 8);\
2149
    OPNAME ## pixels8_l2_ ## MMX(dst, src+1, half, stride, stride, 8);\
2150
}\
2151
\
2152
static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2153
    uint64_t temp[8];\
2154
    uint8_t * const half= (uint8_t*)temp;\
2155
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, 8, stride);\
2156
    OPNAME ## pixels8_l2_ ## MMX(dst, src, half, stride, stride, 8);\
2157
}\
2158
\
2159
static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2160
    OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src, stride, stride);\
2161
}\
2162
\
2163
static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2164
    uint64_t temp[8];\
2165
    uint8_t * const half= (uint8_t*)temp;\
2166
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, 8, stride);\
2167
    OPNAME ## pixels8_l2_ ## MMX(dst, src+stride, half, stride, stride, 8);\
2168
}\
2169
static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2170
    uint64_t half[8 + 9];\
2171
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2172
    uint8_t * const halfHV= ((uint8_t*)half);\
2173
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2174
    put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
2175
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2176
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
2177
}\
2178
static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2179
    uint64_t half[8 + 9];\
2180
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2181
    uint8_t * const halfHV= ((uint8_t*)half);\
2182
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2183
    put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
2184
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2185
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
2186
}\
2187
static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2188
    uint64_t half[8 + 9];\
2189
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2190
    uint8_t * const halfHV= ((uint8_t*)half);\
2191
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2192
    put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
2193
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2194
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
2195
}\
2196
static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2197
    uint64_t half[8 + 9];\
2198
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2199
    uint8_t * const halfHV= ((uint8_t*)half);\
2200
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2201
    put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
2202
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2203
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
2204
}\
2205
static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2206
    uint64_t half[8 + 9];\
2207
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2208
    uint8_t * const halfHV= ((uint8_t*)half);\
2209
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2210
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2211
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
2212
}\
2213
static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2214
    uint64_t half[8 + 9];\
2215
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2216
    uint8_t * const halfHV= ((uint8_t*)half);\
2217
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2218
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2219
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
2220
}\
2221
static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2222
    uint64_t half[8 + 9];\
2223
    uint8_t * const halfH= ((uint8_t*)half);\
2224
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2225
    put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
2226
    OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
2227
}\
2228
static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2229
    uint64_t half[8 + 9];\
2230
    uint8_t * const halfH= ((uint8_t*)half);\
2231
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2232
    put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
2233
    OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
2234
}\
2235
static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2236
    uint64_t half[9];\
2237
    uint8_t * const halfH= ((uint8_t*)half);\
2238
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2239
    OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
2240
}\
2241
static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src, int stride){\
2242
    OPNAME ## pixels16_mmx(dst, src, stride, 16);\
2243
}\
2244
\
2245
static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2246
    uint64_t temp[32];\
2247
    uint8_t * const half= (uint8_t*)temp;\
2248
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16, stride, 16);\
2249
    OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, stride, 16);\
2250
}\
2251
\
2252
static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2253
    OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src, stride, stride, 16);\
2254
}\
2255
\
2256
static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2257
    uint64_t temp[32];\
2258
    uint8_t * const half= (uint8_t*)temp;\
2259
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16, stride, 16);\
2260
    OPNAME ## pixels16_l2_ ## MMX(dst, src+1, half, stride, stride, 16);\
2261
}\
2262
\
2263
static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2264
    uint64_t temp[32];\
2265
    uint8_t * const half= (uint8_t*)temp;\
2266
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16, stride);\
2267
    OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, stride, 16);\
2268
}\
2269
\
2270
static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2271
    OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src, stride, stride);\
2272
}\
2273
\
2274
static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2275
    uint64_t temp[32];\
2276
    uint8_t * const half= (uint8_t*)temp;\
2277
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16, stride);\
2278
    OPNAME ## pixels16_l2_ ## MMX(dst, src+stride, half, stride, stride, 16);\
2279
}\
2280
static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2281
    uint64_t half[16*2 + 17*2];\
2282
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2283
    uint8_t * const halfHV= ((uint8_t*)half);\
2284
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2285
    put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
2286
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2287
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
2288
}\
2289
static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2290
    uint64_t half[16*2 + 17*2];\
2291
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2292
    uint8_t * const halfHV= ((uint8_t*)half);\
2293
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2294
    put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
2295
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2296
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
2297
}\
2298
static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2299
    uint64_t half[16*2 + 17*2];\
2300
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2301
    uint8_t * const halfHV= ((uint8_t*)half);\
2302
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2303
    put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
2304
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2305
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
2306
}\
2307
static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2308
    uint64_t half[16*2 + 17*2];\
2309
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2310
    uint8_t * const halfHV= ((uint8_t*)half);\
2311
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2312
    put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
2313
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2314
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
2315
}\
2316
static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2317
    uint64_t half[16*2 + 17*2];\
2318
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2319
    uint8_t * const halfHV= ((uint8_t*)half);\
2320
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2321
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2322
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
2323
}\
2324
static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2325
    uint64_t half[16*2 + 17*2];\
2326
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2327
    uint8_t * const halfHV= ((uint8_t*)half);\
2328
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2329
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2330
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
2331
}\
2332
static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2333
    uint64_t half[17*2];\
2334
    uint8_t * const halfH= ((uint8_t*)half);\
2335
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2336
    put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
2337
    OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
2338
}\
2339
static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2340
    uint64_t half[17*2];\
2341
    uint8_t * const halfH= ((uint8_t*)half);\
2342
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2343
    put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
2344
    OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
2345
}\
2346
static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2347
    uint64_t half[17*2];\
2348
    uint8_t * const halfH= ((uint8_t*)half);\
2349
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2350
    OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
2351
}
2352

    
2353
#define PUT_OP(a,b,temp, size) "mov" #size " " #a ", " #b "        \n\t"
2354
#define AVG_3DNOW_OP(a,b,temp, size) \
2355
"mov" #size " " #b ", " #temp "   \n\t"\
2356
"pavgusb " #temp ", " #a "        \n\t"\
2357
"mov" #size " " #a ", " #b "      \n\t"
2358
#define AVG_MMX2_OP(a,b,temp, size) \
2359
"mov" #size " " #b ", " #temp "   \n\t"\
2360
"pavgb " #temp ", " #a "          \n\t"\
2361
"mov" #size " " #a ", " #b "      \n\t"
2362

    
2363
QPEL_BASE(put_       , ff_pw_16, _       , PUT_OP, PUT_OP)
2364
QPEL_BASE(avg_       , ff_pw_16, _       , AVG_MMX2_OP, AVG_3DNOW_OP)
2365
QPEL_BASE(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, PUT_OP)
2366
QPEL_OP(put_       , ff_pw_16, _       , PUT_OP, 3dnow)
2367
QPEL_OP(avg_       , ff_pw_16, _       , AVG_3DNOW_OP, 3dnow)
2368
QPEL_OP(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, 3dnow)
2369
QPEL_OP(put_       , ff_pw_16, _       , PUT_OP, mmx2)
2370
QPEL_OP(avg_       , ff_pw_16, _       , AVG_MMX2_OP, mmx2)
2371
QPEL_OP(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, mmx2)
2372

    
2373
/***********************************/
2374
/* bilinear qpel: not compliant to any spec, only for -lavdopts fast */
2375

    
2376
#define QPEL_2TAP_XY(OPNAME, SIZE, MMX, XY, HPEL)\
2377
static void OPNAME ## 2tap_qpel ## SIZE ## _mc ## XY ## _ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2378
    OPNAME ## pixels ## SIZE ## HPEL(dst, src, stride, SIZE);\
2379
}
2380
#define QPEL_2TAP_L3(OPNAME, SIZE, MMX, XY, S0, S1, S2)\
2381
static void OPNAME ## 2tap_qpel ## SIZE ## _mc ## XY ## _ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2382
    OPNAME ## 2tap_qpel ## SIZE ## _l3_ ## MMX(dst, src+S0, stride, SIZE, S1, S2);\
2383
}
2384

    
2385
#define QPEL_2TAP(OPNAME, SIZE, MMX)\
2386
QPEL_2TAP_XY(OPNAME, SIZE, MMX, 20, _x2_ ## MMX)\
2387
QPEL_2TAP_XY(OPNAME, SIZE, MMX, 02, _y2_ ## MMX)\
2388
QPEL_2TAP_XY(OPNAME, SIZE, MMX, 22, _xy2_mmx)\
2389
static const qpel_mc_func OPNAME ## 2tap_qpel ## SIZE ## _mc00_ ## MMX =\
2390
                          OPNAME ## qpel ## SIZE ## _mc00_ ## MMX;\
2391
static const qpel_mc_func OPNAME ## 2tap_qpel ## SIZE ## _mc21_ ## MMX =\
2392
                          OPNAME ## 2tap_qpel ## SIZE ## _mc20_ ## MMX;\
2393
static const qpel_mc_func OPNAME ## 2tap_qpel ## SIZE ## _mc12_ ## MMX =\
2394
                          OPNAME ## 2tap_qpel ## SIZE ## _mc02_ ## MMX;\
2395
static void OPNAME ## 2tap_qpel ## SIZE ## _mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2396
    OPNAME ## pixels ## SIZE ## _y2_ ## MMX(dst, src+1, stride, SIZE);\
2397
}\
2398
static void OPNAME ## 2tap_qpel ## SIZE ## _mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2399
    OPNAME ## pixels ## SIZE ## _x2_ ## MMX(dst, src+stride, stride, SIZE);\
2400
}\
2401
QPEL_2TAP_L3(OPNAME, SIZE, MMX, 10, 0,         1,       0)\
2402
QPEL_2TAP_L3(OPNAME, SIZE, MMX, 30, 1,        -1,       0)\
2403
QPEL_2TAP_L3(OPNAME, SIZE, MMX, 01, 0,         stride,  0)\
2404
QPEL_2TAP_L3(OPNAME, SIZE, MMX, 03, stride,   -stride,  0)\
2405
QPEL_2TAP_L3(OPNAME, SIZE, MMX, 11, 0,         stride,  1)\
2406
QPEL_2TAP_L3(OPNAME, SIZE, MMX, 31, 1,         stride, -1)\
2407
QPEL_2TAP_L3(OPNAME, SIZE, MMX, 13, stride,   -stride,  1)\
2408
QPEL_2TAP_L3(OPNAME, SIZE, MMX, 33, stride+1, -stride, -1)\
2409

    
2410
QPEL_2TAP(put_, 16, mmx2)
2411
QPEL_2TAP(avg_, 16, mmx2)
2412
QPEL_2TAP(put_,  8, mmx2)
2413
QPEL_2TAP(avg_,  8, mmx2)
2414
QPEL_2TAP(put_, 16, 3dnow)
2415
QPEL_2TAP(avg_, 16, 3dnow)
2416
QPEL_2TAP(put_,  8, 3dnow)
2417
QPEL_2TAP(avg_,  8, 3dnow)
2418

    
2419

    
2420
#if 0
2421
static void just_return() { return; }
2422
#endif
2423

    
2424
#define SET_QPEL_FUNC(postfix1, postfix2) \
2425
    c->put_ ## postfix1 = put_ ## postfix2;\
2426
    c->put_no_rnd_ ## postfix1 = put_no_rnd_ ## postfix2;\
2427
    c->avg_ ## postfix1 = avg_ ## postfix2;
2428

    
2429
static void gmc_mmx(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
2430
                    int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height){
2431
    const int w = 8;
2432
    const int ix = ox>>(16+shift);
2433
    const int iy = oy>>(16+shift);
2434
    const int oxs = ox>>4;
2435
    const int oys = oy>>4;
2436
    const int dxxs = dxx>>4;
2437
    const int dxys = dxy>>4;
2438
    const int dyxs = dyx>>4;
2439
    const int dyys = dyy>>4;
2440
    const uint16_t r4[4] = {r,r,r,r};
2441
    const uint16_t dxy4[4] = {dxys,dxys,dxys,dxys};
2442
    const uint16_t dyy4[4] = {dyys,dyys,dyys,dyys};
2443
    const uint64_t shift2 = 2*shift;
2444
    uint8_t edge_buf[(h+1)*stride];
2445
    int x, y;
2446

    
2447
    const int dxw = (dxx-(1<<(16+shift)))*(w-1);
2448
    const int dyh = (dyy-(1<<(16+shift)))*(h-1);
2449
    const int dxh = dxy*(h-1);
2450
    const int dyw = dyx*(w-1);
2451
    if( // non-constant fullpel offset (3% of blocks)
2452
        (ox^(ox+dxw) | ox^(ox+dxh) | ox^(ox+dxw+dxh) |
2453
         oy^(oy+dyw) | oy^(oy+dyh) | oy^(oy+dyw+dyh)) >> (16+shift)
2454
        // uses more than 16 bits of subpel mv (only at huge resolution)
2455
        || (dxx|dxy|dyx|dyy)&15 )
2456
    {
2457
        //FIXME could still use mmx for some of the rows
2458
        ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r, width, height);
2459
        return;
2460
    }
2461

    
2462
    src += ix + iy*stride;
2463
    if( (unsigned)ix >= width-w ||
2464
        (unsigned)iy >= height-h )
2465
    {
2466
        ff_emulated_edge_mc(edge_buf, src, stride, w+1, h+1, ix, iy, width, height);
2467
        src = edge_buf;
2468
    }
2469

    
2470
    asm volatile(
2471
        "movd         %0, %%mm6 \n\t"
2472
        "pxor      %%mm7, %%mm7 \n\t"
2473
        "punpcklwd %%mm6, %%mm6 \n\t"
2474
        "punpcklwd %%mm6, %%mm6 \n\t"
2475
        :: "r"(1<<shift)
2476
    );
2477

    
2478
    for(x=0; x<w; x+=4){
2479
        uint16_t dx4[4] = { oxs - dxys + dxxs*(x+0),
2480
                            oxs - dxys + dxxs*(x+1),
2481
                            oxs - dxys + dxxs*(x+2),
2482
                            oxs - dxys + dxxs*(x+3) };
2483
        uint16_t dy4[4] = { oys - dyys + dyxs*(x+0),
2484
                            oys - dyys + dyxs*(x+1),
2485
                            oys - dyys + dyxs*(x+2),
2486
                            oys - dyys + dyxs*(x+3) };
2487

    
2488
        for(y=0; y<h; y++){
2489
            asm volatile(
2490
                "movq   %0,  %%mm4 \n\t"
2491
                "movq   %1,  %%mm5 \n\t"
2492
                "paddw  %2,  %%mm4 \n\t"
2493
                "paddw  %3,  %%mm5 \n\t"
2494
                "movq   %%mm4, %0  \n\t"
2495
                "movq   %%mm5, %1  \n\t"
2496
                "psrlw  $12, %%mm4 \n\t"
2497
                "psrlw  $12, %%mm5 \n\t"
2498
                : "+m"(*dx4), "+m"(*dy4)
2499
                : "m"(*dxy4), "m"(*dyy4)
2500
            );
2501

    
2502
            asm volatile(
2503
                "movq   %%mm6, %%mm2 \n\t"
2504
                "movq   %%mm6, %%mm1 \n\t"
2505
                "psubw  %%mm4, %%mm2 \n\t"
2506
                "psubw  %%mm5, %%mm1 \n\t"
2507
                "movq   %%mm2, %%mm0 \n\t"
2508
                "movq   %%mm4, %%mm3 \n\t"
2509
                "pmullw %%mm1, %%mm0 \n\t" // (s-dx)*(s-dy)
2510
                "pmullw %%mm5, %%mm3 \n\t" // dx*dy
2511
                "pmullw %%mm5, %%mm2 \n\t" // (s-dx)*dy
2512
                "pmullw %%mm4, %%mm1 \n\t" // dx*(s-dy)
2513

    
2514
                "movd   %4,    %%mm5 \n\t"
2515
                "movd   %3,    %%mm4 \n\t"
2516
                "punpcklbw %%mm7, %%mm5 \n\t"
2517
                "punpcklbw %%mm7, %%mm4 \n\t"
2518
                "pmullw %%mm5, %%mm3 \n\t" // src[1,1] * dx*dy
2519
                "pmullw %%mm4, %%mm2 \n\t" // src[0,1] * (s-dx)*dy
2520

    
2521
                "movd   %2,    %%mm5 \n\t"
2522
                "movd   %1,    %%mm4 \n\t"
2523
                "punpcklbw %%mm7, %%mm5 \n\t"
2524
                "punpcklbw %%mm7, %%mm4 \n\t"
2525
                "pmullw %%mm5, %%mm1 \n\t" // src[1,0] * dx*(s-dy)
2526
                "pmullw %%mm4, %%mm0 \n\t" // src[0,0] * (s-dx)*(s-dy)
2527
                "paddw  %5,    %%mm1 \n\t"
2528
                "paddw  %%mm3, %%mm2 \n\t"
2529
                "paddw  %%mm1, %%mm0 \n\t"
2530
                "paddw  %%mm2, %%mm0 \n\t"
2531

    
2532
                "psrlw    %6,    %%mm0 \n\t"
2533
                "packuswb %%mm0, %%mm0 \n\t"
2534
                "movd     %%mm0, %0    \n\t"
2535

    
2536
                : "=m"(dst[x+y*stride])
2537
                : "m"(src[0]), "m"(src[1]),
2538
                  "m"(src[stride]), "m"(src[stride+1]),
2539
                  "m"(*r4), "m"(shift2)
2540
            );
2541
            src += stride;
2542
        }
2543
        src += 4-h*stride;
2544
    }
2545
}
2546

    
2547
#ifdef CONFIG_ENCODERS
2548
static int try_8x8basis_mmx(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale){
2549
    long i=0;
2550

    
2551
    assert(FFABS(scale) < 256);
2552
    scale<<= 16 + 1 - BASIS_SHIFT + RECON_SHIFT;
2553

    
2554
    asm volatile(
2555
        "pcmpeqw %%mm6, %%mm6           \n\t" // -1w
2556
        "psrlw $15, %%mm6               \n\t" //  1w
2557
        "pxor %%mm7, %%mm7              \n\t"
2558
        "movd  %4, %%mm5                \n\t"
2559
        "punpcklwd %%mm5, %%mm5         \n\t"
2560
        "punpcklwd %%mm5, %%mm5         \n\t"
2561
        "1:                             \n\t"
2562
        "movq  (%1, %0), %%mm0          \n\t"
2563
        "movq  8(%1, %0), %%mm1         \n\t"
2564
        "pmulhw %%mm5, %%mm0            \n\t"
2565
        "pmulhw %%mm5, %%mm1            \n\t"
2566
        "paddw %%mm6, %%mm0             \n\t"
2567
        "paddw %%mm6, %%mm1             \n\t"
2568
        "psraw $1, %%mm0                \n\t"
2569
        "psraw $1, %%mm1                \n\t"
2570
        "paddw (%2, %0), %%mm0          \n\t"
2571
        "paddw 8(%2, %0), %%mm1         \n\t"
2572
        "psraw $6, %%mm0                \n\t"
2573
        "psraw $6, %%mm1                \n\t"
2574
        "pmullw (%3, %0), %%mm0         \n\t"
2575
        "pmullw 8(%3, %0), %%mm1        \n\t"
2576
        "pmaddwd %%mm0, %%mm0           \n\t"
2577
        "pmaddwd %%mm1, %%mm1           \n\t"
2578
        "paddd %%mm1, %%mm0             \n\t"
2579
        "psrld $4, %%mm0                \n\t"
2580
        "paddd %%mm0, %%mm7             \n\t"
2581
        "add $16, %0                    \n\t"
2582
        "cmp $128, %0                   \n\t" //FIXME optimize & bench
2583
        " jb 1b                         \n\t"
2584
        "movq %%mm7, %%mm6              \n\t"
2585
        "psrlq $32, %%mm7               \n\t"
2586
        "paddd %%mm6, %%mm7             \n\t"
2587
        "psrld $2, %%mm7                \n\t"
2588
        "movd %%mm7, %0                 \n\t"
2589

    
2590
        : "+r" (i)
2591
        : "r"(basis), "r"(rem), "r"(weight), "g"(scale)
2592
    );
2593
    return i;
2594
}
2595

    
2596
static void add_8x8basis_mmx(int16_t rem[64], int16_t basis[64], int scale){
2597
    long i=0;
2598

    
2599
    if(FFABS(scale) < 256){
2600
        scale<<= 16 + 1 - BASIS_SHIFT + RECON_SHIFT;
2601
        asm volatile(
2602
                "pcmpeqw %%mm6, %%mm6   \n\t" // -1w
2603
                "psrlw $15, %%mm6       \n\t" //  1w
2604
                "movd  %3, %%mm5        \n\t"
2605
                "punpcklwd %%mm5, %%mm5 \n\t"
2606
                "punpcklwd %%mm5, %%mm5 \n\t"
2607
                "1:                     \n\t"
2608
                "movq  (%1, %0), %%mm0  \n\t"
2609
                "movq  8(%1, %0), %%mm1 \n\t"
2610
                "pmulhw %%mm5, %%mm0    \n\t"
2611
                "pmulhw %%mm5, %%mm1    \n\t"
2612
                "paddw %%mm6, %%mm0     \n\t"
2613
                "paddw %%mm6, %%mm1     \n\t"
2614
                "psraw $1, %%mm0        \n\t"
2615
                "psraw $1, %%mm1        \n\t"
2616
                "paddw (%2, %0), %%mm0  \n\t"
2617
                "paddw 8(%2, %0), %%mm1 \n\t"
2618
                "movq %%mm0, (%2, %0)   \n\t"
2619
                "movq %%mm1, 8(%2, %0)  \n\t"
2620
                "add $16, %0            \n\t"
2621
                "cmp $128, %0           \n\t" //FIXME optimize & bench
2622
                " jb 1b                 \n\t"
2623

    
2624
                : "+r" (i)
2625
                : "r"(basis), "r"(rem), "g"(scale)
2626
        );
2627
    }else{
2628
        for(i=0; i<8*8; i++){
2629
            rem[i] += (basis[i]*scale + (1<<(BASIS_SHIFT - RECON_SHIFT-1)))>>(BASIS_SHIFT - RECON_SHIFT);
2630
        }
2631
    }
2632
}
2633
#endif /* CONFIG_ENCODERS */
2634

    
2635
#define PREFETCH(name, op) \
2636
static void name(void *mem, int stride, int h){\
2637
    const uint8_t *p= mem;\
2638
    do{\
2639
        asm volatile(#op" %0" :: "m"(*p));\
2640
        p+= stride;\
2641
    }while(--h);\
2642
}
2643
PREFETCH(prefetch_mmx2,  prefetcht0)
2644
PREFETCH(prefetch_3dnow, prefetch)
2645
#undef PREFETCH
2646

    
2647
#include "h264dsp_mmx.c"
2648

    
2649
/* AVS specific */
2650
void ff_cavsdsp_init_mmx2(DSPContext* c, AVCodecContext *avctx);
2651

    
2652
void ff_put_cavs_qpel8_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
2653
    put_pixels8_mmx(dst, src, stride, 8);
2654
}
2655
void ff_avg_cavs_qpel8_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
2656
    avg_pixels8_mmx(dst, src, stride, 8);
2657
}
2658
void ff_put_cavs_qpel16_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
2659
    put_pixels16_mmx(dst, src, stride, 16);
2660
}
2661
void ff_avg_cavs_qpel16_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
2662
    avg_pixels16_mmx(dst, src, stride, 16);
2663
}
2664

    
2665
/* external functions, from idct_mmx.c */
2666
void ff_mmx_idct(DCTELEM *block);
2667
void ff_mmxext_idct(DCTELEM *block);
2668

    
2669
void ff_vp3_idct_sse2(int16_t *input_data);
2670
void ff_vp3_idct_mmx(int16_t *data);
2671
void ff_vp3_dsp_init_mmx(void);
2672

    
2673
/* XXX: those functions should be suppressed ASAP when all IDCTs are
2674
   converted */
2675
#ifdef CONFIG_GPL
2676
static void ff_libmpeg2mmx_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
2677
{
2678
    ff_mmx_idct (block);
2679
    put_pixels_clamped_mmx(block, dest, line_size);
2680
}
2681
static void ff_libmpeg2mmx_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
2682
{
2683
    ff_mmx_idct (block);
2684
    add_pixels_clamped_mmx(block, dest, line_size);
2685
}
2686
static void ff_libmpeg2mmx2_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
2687
{
2688
    ff_mmxext_idct (block);
2689
    put_pixels_clamped_mmx(block, dest, line_size);
2690
}
2691
static void ff_libmpeg2mmx2_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
2692
{
2693
    ff_mmxext_idct (block);
2694
    add_pixels_clamped_mmx(block, dest, line_size);
2695
}
2696
#endif
2697
static void ff_vp3_idct_put_sse2(uint8_t *dest, int line_size, DCTELEM *block)
2698
{
2699
    ff_vp3_idct_sse2(block);
2700
    put_signed_pixels_clamped_mmx(block, dest, line_size);
2701
}
2702
static void ff_vp3_idct_add_sse2(uint8_t *dest, int line_size, DCTELEM *block)
2703
{
2704
    ff_vp3_idct_sse2(block);
2705
    add_pixels_clamped_mmx(block, dest, line_size);
2706
}
2707
static void ff_vp3_idct_put_mmx(uint8_t *dest, int line_size, DCTELEM *block)
2708
{
2709
    ff_vp3_idct_mmx(block);
2710
    put_signed_pixels_clamped_mmx(block, dest, line_size);
2711
}
2712
static void ff_vp3_idct_add_mmx(uint8_t *dest, int line_size, DCTELEM *block)
2713
{
2714
    ff_vp3_idct_mmx(block);
2715
    add_pixels_clamped_mmx(block, dest, line_size);
2716
}
2717
static void ff_idct_xvid_mmx_put(uint8_t *dest, int line_size, DCTELEM *block)
2718
{
2719
    ff_idct_xvid_mmx (block);
2720
    put_pixels_clamped_mmx(block, dest, line_size);
2721
}
2722
static void ff_idct_xvid_mmx_add(uint8_t *dest, int line_size, DCTELEM *block)
2723
{
2724
    ff_idct_xvid_mmx (block);
2725
    add_pixels_clamped_mmx(block, dest, line_size);
2726
}
2727
static void ff_idct_xvid_mmx2_put(uint8_t *dest, int line_size, DCTELEM *block)
2728
{
2729
    ff_idct_xvid_mmx2 (block);
2730
    put_pixels_clamped_mmx(block, dest, line_size);
2731
}
2732
static void ff_idct_xvid_mmx2_add(uint8_t *dest, int line_size, DCTELEM *block)
2733
{
2734
    ff_idct_xvid_mmx2 (block);
2735
    add_pixels_clamped_mmx(block, dest, line_size);
2736
}
2737

    
2738
static void vorbis_inverse_coupling_3dnow(float *mag, float *ang, int blocksize)
2739
{
2740
    int i;
2741
    asm volatile("pxor %%mm7, %%mm7":);
2742
    for(i=0; i<blocksize; i+=2) {
2743
        asm volatile(
2744
            "movq    %0,    %%mm0 \n\t"
2745
            "movq    %1,    %%mm1 \n\t"
2746
            "movq    %%mm0, %%mm2 \n\t"
2747
            "movq    %%mm1, %%mm3 \n\t"
2748
            "pfcmpge %%mm7, %%mm2 \n\t" // m <= 0.0
2749
            "pfcmpge %%mm7, %%mm3 \n\t" // a <= 0.0
2750
            "pslld   $31,   %%mm2 \n\t" // keep only the sign bit
2751
            "pxor    %%mm2, %%mm1 \n\t"
2752
            "movq    %%mm3, %%mm4 \n\t"
2753
            "pand    %%mm1, %%mm3 \n\t"
2754
            "pandn   %%mm1, %%mm4 \n\t"
2755
            "pfadd   %%mm0, %%mm3 \n\t" // a = m + ((a<0) & (a ^ sign(m)))
2756
            "pfsub   %%mm4, %%mm0 \n\t" // m = m + ((a>0) & (a ^ sign(m)))
2757
            "movq    %%mm3, %1    \n\t"
2758
            "movq    %%mm0, %0    \n\t"
2759
            :"+m"(mag[i]), "+m"(ang[i])
2760
            ::"memory"
2761
        );
2762
    }
2763
    asm volatile("femms");
2764
}
2765
static void vorbis_inverse_coupling_sse(float *mag, float *ang, int blocksize)
2766
{
2767
    int i;
2768

    
2769
    asm volatile(
2770
            "movaps  %0,     %%xmm5 \n\t"
2771
        ::"m"(ff_pdw_80000000[0])
2772
    );
2773
    for(i=0; i<blocksize; i+=4) {
2774
        asm volatile(
2775
            "movaps  %0,     %%xmm0 \n\t"
2776
            "movaps  %1,     %%xmm1 \n\t"
2777
            "xorps   %%xmm2, %%xmm2 \n\t"
2778
            "xorps   %%xmm3, %%xmm3 \n\t"
2779
            "cmpleps %%xmm0, %%xmm2 \n\t" // m <= 0.0
2780
            "cmpleps %%xmm1, %%xmm3 \n\t" // a <= 0.0
2781
            "andps   %%xmm5, %%xmm2 \n\t" // keep only the sign bit
2782
            "xorps   %%xmm2, %%xmm1 \n\t"
2783
            "movaps  %%xmm3, %%xmm4 \n\t"
2784
            "andps   %%xmm1, %%xmm3 \n\t"
2785
            "andnps  %%xmm1, %%xmm4 \n\t"
2786
            "addps   %%xmm0, %%xmm3 \n\t" // a = m + ((a<0) & (a ^ sign(m)))
2787
            "subps   %%xmm4, %%xmm0 \n\t" // m = m + ((a>0) & (a ^ sign(m)))
2788
            "movaps  %%xmm3, %1     \n\t"
2789
            "movaps  %%xmm0, %0     \n\t"
2790
            :"+m"(mag[i]), "+m"(ang[i])
2791
            ::"memory"
2792
        );
2793
    }
2794
}
2795

    
2796
static void vector_fmul_3dnow(float *dst, const float *src, int len){
2797
    long i = (len-4)*4;
2798
    asm volatile(
2799
        "1: \n\t"
2800
        "movq    (%1,%0), %%mm0 \n\t"
2801
        "movq   8(%1,%0), %%mm1 \n\t"
2802
        "pfmul   (%2,%0), %%mm0 \n\t"
2803
        "pfmul  8(%2,%0), %%mm1 \n\t"
2804
        "movq   %%mm0,  (%1,%0) \n\t"
2805
        "movq   %%mm1, 8(%1,%0) \n\t"
2806
        "sub  $16, %0 \n\t"
2807
        "jge 1b \n\t"
2808
        "femms  \n\t"
2809
        :"+r"(i)
2810
        :"r"(dst), "r"(src)
2811
        :"memory"
2812
    );
2813
}
2814
static void vector_fmul_sse(float *dst, const float *src, int len){
2815
    long i = (len-8)*4;
2816
    asm volatile(
2817
        "1: \n\t"
2818
        "movaps    (%1,%0), %%xmm0 \n\t"
2819
        "movaps  16(%1,%0), %%xmm1 \n\t"
2820
        "mulps     (%2,%0), %%xmm0 \n\t"
2821
        "mulps   16(%2,%0), %%xmm1 \n\t"
2822
        "movaps  %%xmm0,   (%1,%0) \n\t"
2823
        "movaps  %%xmm1, 16(%1,%0) \n\t"
2824
        "sub  $32, %0 \n\t"
2825
        "jge 1b \n\t"
2826
        :"+r"(i)
2827
        :"r"(dst), "r"(src)
2828
        :"memory"
2829
    );
2830
}
2831

    
2832
static void vector_fmul_reverse_3dnow2(float *dst, const float *src0, const float *src1, int len){
2833
    long i = len*4-16;
2834
    asm volatile(
2835
        "1: \n\t"
2836
        "pswapd   8(%1), %%mm0 \n\t"
2837
        "pswapd    (%1), %%mm1 \n\t"
2838
        "pfmul  (%3,%0), %%mm0 \n\t"
2839
        "pfmul 8(%3,%0), %%mm1 \n\t"
2840
        "movq  %%mm0,  (%2,%0) \n\t"
2841
        "movq  %%mm1, 8(%2,%0) \n\t"
2842
        "add   $16, %1 \n\t"
2843
        "sub   $16, %0 \n\t"
2844
        "jge   1b \n\t"
2845
        :"+r"(i), "+r"(src1)
2846
        :"r"(dst), "r"(src0)
2847
    );
2848
    asm volatile("femms");
2849
}
2850
static void vector_fmul_reverse_sse(float *dst, const float *src0, const float *src1, int len){
2851
    long i = len*4-32;
2852
    asm volatile(
2853
        "1: \n\t"
2854
        "movaps        16(%1), %%xmm0 \n\t"
2855
        "movaps          (%1), %%xmm1 \n\t"
2856
        "shufps $0x1b, %%xmm0, %%xmm0 \n\t"
2857
        "shufps $0x1b, %%xmm1, %%xmm1 \n\t"
2858
        "mulps        (%3,%0), %%xmm0 \n\t"
2859
        "mulps      16(%3,%0), %%xmm1 \n\t"
2860
        "movaps     %%xmm0,   (%2,%0) \n\t"
2861
        "movaps     %%xmm1, 16(%2,%0) \n\t"
2862
        "add    $32, %1 \n\t"
2863
        "sub    $32, %0 \n\t"
2864
        "jge    1b \n\t"
2865
        :"+r"(i), "+r"(src1)
2866
        :"r"(dst), "r"(src0)
2867
    );
2868
}
2869

    
2870
static void vector_fmul_add_add_3dnow(float *dst, const float *src0, const float *src1,
2871
                                      const float *src2, int src3, int len, int step){
2872
    long i = (len-4)*4;
2873
    if(step == 2 && src3 == 0){
2874
        dst += (len-4)*2;
2875
        asm volatile(
2876
            "1: \n\t"
2877
            "movq   (%2,%0),  %%mm0 \n\t"
2878
            "movq  8(%2,%0),  %%mm1 \n\t"
2879
            "pfmul  (%3,%0),  %%mm0 \n\t"
2880
            "pfmul 8(%3,%0),  %%mm1 \n\t"
2881
            "pfadd  (%4,%0),  %%mm0 \n\t"
2882
            "pfadd 8(%4,%0),  %%mm1 \n\t"
2883
            "movd     %%mm0,   (%1) \n\t"
2884
            "movd     %%mm1, 16(%1) \n\t"
2885
            "psrlq      $32,  %%mm0 \n\t"
2886
            "psrlq      $32,  %%mm1 \n\t"
2887
            "movd     %%mm0,  8(%1) \n\t"
2888
            "movd     %%mm1, 24(%1) \n\t"
2889
            "sub  $32, %1 \n\t"
2890
            "sub  $16, %0 \n\t"
2891
            "jge  1b \n\t"
2892
            :"+r"(i), "+r"(dst)
2893
            :"r"(src0), "r"(src1), "r"(src2)
2894
            :"memory"
2895
        );
2896
    }
2897
    else if(step == 1 && src3 == 0){
2898
        asm volatile(
2899
            "1: \n\t"
2900
            "movq    (%2,%0), %%mm0 \n\t"
2901
            "movq   8(%2,%0), %%mm1 \n\t"
2902
            "pfmul   (%3,%0), %%mm0 \n\t"
2903
            "pfmul  8(%3,%0), %%mm1 \n\t"
2904
            "pfadd   (%4,%0), %%mm0 \n\t"
2905
            "pfadd  8(%4,%0), %%mm1 \n\t"
2906
            "movq  %%mm0,   (%1,%0) \n\t"
2907
            "movq  %%mm1,  8(%1,%0) \n\t"
2908
            "sub  $16, %0 \n\t"
2909
            "jge  1b \n\t"
2910
            :"+r"(i)
2911
            :"r"(dst), "r"(src0), "r"(src1), "r"(src2)
2912
            :"memory"
2913
        );
2914
    }
2915
    else
2916
        ff_vector_fmul_add_add_c(dst, src0, src1, src2, src3, len, step);
2917
    asm volatile("femms");
2918
}
2919
static void vector_fmul_add_add_sse(float *dst, const float *src0, const float *src1,
2920
                                    const float *src2, int src3, int len, int step){
2921
    long i = (len-8)*4;
2922
    if(step == 2 && src3 == 0){
2923
        dst += (len-8)*2;
2924
        asm volatile(
2925
            "1: \n\t"
2926
            "movaps   (%2,%0), %%xmm0 \n\t"
2927
            "movaps 16(%2,%0), %%xmm1 \n\t"
2928
            "mulps    (%3,%0), %%xmm0 \n\t"
2929
            "mulps  16(%3,%0), %%xmm1 \n\t"
2930
            "addps    (%4,%0), %%xmm0 \n\t"
2931
            "addps  16(%4,%0), %%xmm1 \n\t"
2932
            "movss     %%xmm0,   (%1) \n\t"
2933
            "movss     %%xmm1, 32(%1) \n\t"
2934
            "movhlps   %%xmm0, %%xmm2 \n\t"
2935
            "movhlps   %%xmm1, %%xmm3 \n\t"
2936
            "movss     %%xmm2, 16(%1) \n\t"
2937
            "movss     %%xmm3, 48(%1) \n\t"
2938
            "shufps $0xb1, %%xmm0, %%xmm0 \n\t"
2939
            "shufps $0xb1, %%xmm1, %%xmm1 \n\t"
2940
            "movss     %%xmm0,  8(%1) \n\t"
2941
            "movss     %%xmm1, 40(%1) \n\t"
2942
            "movhlps   %%xmm0, %%xmm2 \n\t"
2943
            "movhlps   %%xmm1, %%xmm3 \n\t"
2944
            "movss     %%xmm2, 24(%1) \n\t"
2945
            "movss     %%xmm3, 56(%1) \n\t"
2946
            "sub  $64, %1 \n\t"
2947
            "sub  $32, %0 \n\t"
2948
            "jge  1b \n\t"
2949
            :"+r"(i), "+r"(dst)
2950
            :"r"(src0), "r"(src1), "r"(src2)
2951
            :"memory"
2952
        );
2953
    }
2954
    else if(step == 1 && src3 == 0){
2955
        asm volatile(
2956
            "1: \n\t"
2957
            "movaps   (%2,%0), %%xmm0 \n\t"
2958
            "movaps 16(%2,%0), %%xmm1 \n\t"
2959
            "mulps    (%3,%0), %%xmm0 \n\t"
2960
            "mulps  16(%3,%0), %%xmm1 \n\t"
2961
            "addps    (%4,%0), %%xmm0 \n\t"
2962
            "addps  16(%4,%0), %%xmm1 \n\t"
2963
            "movaps %%xmm0,   (%1,%0) \n\t"
2964
            "movaps %%xmm1, 16(%1,%0) \n\t"
2965
            "sub  $32, %0 \n\t"
2966
            "jge  1b \n\t"
2967
            :"+r"(i)
2968
            :"r"(dst), "r"(src0), "r"(src1), "r"(src2)
2969
            :"memory"
2970
        );
2971
    }
2972
    else
2973
        ff_vector_fmul_add_add_c(dst, src0, src1, src2, src3, len, step);
2974
}
2975

    
2976
static void float_to_int16_3dnow(int16_t *dst, const float *src, int len){
2977
    // not bit-exact: pf2id uses different rounding than C and SSE
2978
    int i;
2979
    for(i=0; i<len; i+=4) {
2980
        asm volatile(
2981
            "pf2id       %1, %%mm0 \n\t"
2982
            "pf2id       %2, %%mm1 \n\t"
2983
            "packssdw %%mm1, %%mm0 \n\t"
2984
            "movq     %%mm0, %0    \n\t"
2985
            :"=m"(dst[i])
2986
            :"m"(src[i]), "m"(src[i+2])
2987
        );
2988
    }
2989
    asm volatile("femms");
2990
}
2991
static void float_to_int16_sse(int16_t *dst, const float *src, int len){
2992
    int i;
2993
    for(i=0; i<len; i+=4) {
2994
        asm volatile(
2995
            "cvtps2pi    %1, %%mm0 \n\t"
2996
            "cvtps2pi    %2, %%mm1 \n\t"
2997
            "packssdw %%mm1, %%mm0 \n\t"
2998
            "movq     %%mm0, %0    \n\t"
2999
            :"=m"(dst[i])
3000
            :"m"(src[i]), "m"(src[i+2])
3001
        );
3002
    }
3003
    asm volatile("emms");
3004
}
3005

    
3006
#ifdef CONFIG_SNOW_DECODER
3007
extern void ff_snow_horizontal_compose97i_sse2(DWTELEM *b, int width);
3008
extern void ff_snow_horizontal_compose97i_mmx(DWTELEM *b, int width);
3009
extern void ff_snow_vertical_compose97i_sse2(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width);
3010
extern void ff_snow_vertical_compose97i_mmx(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width);
3011
extern void ff_snow_inner_add_yblock_sse2(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
3012
                           int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
3013
extern void ff_snow_inner_add_yblock_mmx(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
3014
                          int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
3015
#endif
3016

    
3017
void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx)
3018
{
3019
    mm_flags = mm_support();
3020

    
3021
    if (avctx->dsp_mask) {
3022
        if (avctx->dsp_mask & FF_MM_FORCE)
3023
            mm_flags |= (avctx->dsp_mask & 0xffff);
3024
        else
3025
            mm_flags &= ~(avctx->dsp_mask & 0xffff);
3026
    }
3027

    
3028
#if 0
3029
    av_log(avctx, AV_LOG_INFO, "libavcodec: CPU flags:");
3030
    if (mm_flags & MM_MMX)
3031
        av_log(avctx, AV_LOG_INFO, " mmx");
3032
    if (mm_flags & MM_MMXEXT)
3033
        av_log(avctx, AV_LOG_INFO, " mmxext");
3034
    if (mm_flags & MM_3DNOW)
3035
        av_log(avctx, AV_LOG_INFO, " 3dnow");
3036
    if (mm_flags & MM_SSE)
3037
        av_log(avctx, AV_LOG_INFO, " sse");
3038
    if (mm_flags & MM_SSE2)
3039
        av_log(avctx, AV_LOG_INFO, " sse2");
3040
    av_log(avctx, AV_LOG_INFO, "\n");
3041
#endif
3042

    
3043
    if (mm_flags & MM_MMX) {
3044
        const int idct_algo= avctx->idct_algo;
3045

    
3046
#ifdef CONFIG_ENCODERS
3047
        const int dct_algo = avctx->dct_algo;
3048
        if(dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX){
3049
            if(mm_flags & MM_SSE2){
3050
                c->fdct = ff_fdct_sse2;
3051
            }else if(mm_flags & MM_MMXEXT){
3052
                c->fdct = ff_fdct_mmx2;
3053
            }else{
3054
                c->fdct = ff_fdct_mmx;
3055
            }
3056
        }
3057
#endif //CONFIG_ENCODERS
3058
        if(avctx->lowres==0){
3059
            if(idct_algo==FF_IDCT_AUTO || idct_algo==FF_IDCT_SIMPLEMMX){
3060
                c->idct_put= ff_simple_idct_put_mmx;
3061
                c->idct_add= ff_simple_idct_add_mmx;
3062
                c->idct    = ff_simple_idct_mmx;
3063
                c->idct_permutation_type= FF_SIMPLE_IDCT_PERM;
3064
#ifdef CONFIG_GPL
3065
            }else if(idct_algo==FF_IDCT_LIBMPEG2MMX){
3066
                if(mm_flags & MM_MMXEXT){
3067
                    c->idct_put= ff_libmpeg2mmx2_idct_put;
3068
                    c->idct_add= ff_libmpeg2mmx2_idct_add;
3069
                    c->idct    = ff_mmxext_idct;
3070
                }else{
3071
                    c->idct_put= ff_libmpeg2mmx_idct_put;
3072
                    c->idct_add= ff_libmpeg2mmx_idct_add;
3073
                    c->idct    = ff_mmx_idct;
3074
                }
3075
                c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;
3076
#endif
3077
            }else if(idct_algo==FF_IDCT_VP3 &&
3078
                     avctx->codec->id!=CODEC_ID_THEORA &&
3079
                     !(avctx->flags & CODEC_FLAG_BITEXACT)){
3080
                if(mm_flags & MM_SSE2){
3081
                    c->idct_put= ff_vp3_idct_put_sse2;
3082
                    c->idct_add= ff_vp3_idct_add_sse2;
3083
                    c->idct    = ff_vp3_idct_sse2;
3084
                    c->idct_permutation_type= FF_TRANSPOSE_IDCT_PERM;
3085
                }else{
3086
                    ff_vp3_dsp_init_mmx();
3087
                    c->idct_put= ff_vp3_idct_put_mmx;
3088
                    c->idct_add= ff_vp3_idct_add_mmx;
3089
                    c->idct    = ff_vp3_idct_mmx;
3090
                    c->idct_permutation_type= FF_PARTTRANS_IDCT_PERM;
3091
                }
3092
            }else if(idct_algo==FF_IDCT_CAVS){
3093
                    c->idct_permutation_type= FF_TRANSPOSE_IDCT_PERM;
3094
            }else if(idct_algo==FF_IDCT_XVIDMMX){
3095
                if(mm_flags & MM_MMXEXT){
3096
                    c->idct_put= ff_idct_xvid_mmx2_put;
3097
                    c->idct_add= ff_idct_xvid_mmx2_add;
3098
                    c->idct    = ff_idct_xvid_mmx2;
3099
                }else{
3100
                    c->idct_put= ff_idct_xvid_mmx_put;
3101
                    c->idct_add= ff_idct_xvid_mmx_add;
3102
                    c->idct    = ff_idct_xvid_mmx;
3103
                }
3104
            }
3105
        }
3106

    
3107
#ifdef CONFIG_ENCODERS
3108
        c->get_pixels = get_pixels_mmx;
3109
        c->diff_pixels = diff_pixels_mmx;
3110
#endif //CONFIG_ENCODERS
3111
        c->put_pixels_clamped = put_pixels_clamped_mmx;
3112
        c->put_signed_pixels_clamped = put_signed_pixels_clamped_mmx;
3113
        c->add_pixels_clamped = add_pixels_clamped_mmx;
3114
        c->clear_blocks = clear_blocks_mmx;
3115
#ifdef CONFIG_ENCODERS
3116
        c->pix_sum = pix_sum16_mmx;
3117
#endif //CONFIG_ENCODERS
3118

    
3119
        c->put_pixels_tab[0][0] = put_pixels16_mmx;
3120
        c->put_pixels_tab[0][1] = put_pixels16_x2_mmx;
3121
        c->put_pixels_tab[0][2] = put_pixels16_y2_mmx;
3122
        c->put_pixels_tab[0][3] = put_pixels16_xy2_mmx;
3123

    
3124
        c->put_no_rnd_pixels_tab[0][0] = put_pixels16_mmx;
3125
        c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_mmx;
3126
        c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_mmx;
3127
        c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_mmx;
3128

    
3129
        c->avg_pixels_tab[0][0] = avg_pixels16_mmx;
3130
        c->avg_pixels_tab[0][1] = avg_pixels16_x2_mmx;
3131
        c->avg_pixels_tab[0][2] = avg_pixels16_y2_mmx;
3132
        c->avg_pixels_tab[0][3] = avg_pixels16_xy2_mmx;
3133

    
3134
        c->avg_no_rnd_pixels_tab[0][0] = avg_no_rnd_pixels16_mmx;
3135
        c->avg_no_rnd_pixels_tab[0][1] = avg_no_rnd_pixels16_x2_mmx;
3136
        c->avg_no_rnd_pixels_tab[0][2] = avg_no_rnd_pixels16_y2_mmx;
3137
        c->avg_no_rnd_pixels_tab[0][3] = avg_no_rnd_pixels16_xy2_mmx;
3138

    
3139
        c->put_pixels_tab[1][0] = put_pixels8_mmx;
3140
        c->put_pixels_tab[1][1] = put_pixels8_x2_mmx;
3141
        c->put_pixels_tab[1][2] = put_pixels8_y2_mmx;
3142
        c->put_pixels_tab[1][3] = put_pixels8_xy2_mmx;
3143

    
3144
        c->put_no_rnd_pixels_tab[1][0] = put_pixels8_mmx;
3145
        c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_mmx;
3146
        c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_mmx;
3147
        c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy2_mmx;
3148

    
3149
        c->avg_pixels_tab[1][0] = avg_pixels8_mmx;
3150
        c->avg_pixels_tab[1][1] = avg_pixels8_x2_mmx;
3151
        c->avg_pixels_tab[1][2] = avg_pixels8_y2_mmx;
3152
        c->avg_pixels_tab[1][3] = avg_pixels8_xy2_mmx;
3153

    
3154
        c->avg_no_rnd_pixels_tab[1][0] = avg_no_rnd_pixels8_mmx;
3155
        c->avg_no_rnd_pixels_tab[1][1] = avg_no_rnd_pixels8_x2_mmx;
3156
        c->avg_no_rnd_pixels_tab[1][2] = avg_no_rnd_pixels8_y2_mmx;
3157
        c->avg_no_rnd_pixels_tab[1][3] = avg_no_rnd_pixels8_xy2_mmx;
3158

    
3159
        c->gmc= gmc_mmx;
3160

    
3161
        c->add_bytes= add_bytes_mmx;
3162
#ifdef CONFIG_ENCODERS
3163
        c->diff_bytes= diff_bytes_mmx;
3164

    
3165
        c->hadamard8_diff[0]= hadamard8_diff16_mmx;
3166
        c->hadamard8_diff[1]= hadamard8_diff_mmx;
3167

    
3168
        c->pix_norm1 = pix_norm1_mmx;
3169
        c->sse[0] = (mm_flags & MM_SSE2) ? sse16_sse2 : sse16_mmx;
3170
          c->sse[1] = sse8_mmx;
3171
        c->vsad[4]= vsad_intra16_mmx;
3172

    
3173
        c->nsse[0] = nsse16_mmx;
3174
        c->nsse[1] = nsse8_mmx;
3175
        if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
3176
            c->vsad[0] = vsad16_mmx;
3177
        }
3178

    
3179
        if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
3180
            c->try_8x8basis= try_8x8basis_mmx;
3181
        }
3182
        c->add_8x8basis= add_8x8basis_mmx;
3183

    
3184
        c->ssd_int8_vs_int16 = ssd_int8_vs_int16_mmx;
3185

    
3186
#endif //CONFIG_ENCODERS
3187

    
3188
        c->h263_v_loop_filter= h263_v_loop_filter_mmx;
3189
        c->h263_h_loop_filter= h263_h_loop_filter_mmx;
3190
        c->put_h264_chroma_pixels_tab[0]= put_h264_chroma_mc8_mmx;
3191
        c->put_h264_chroma_pixels_tab[1]= put_h264_chroma_mc4_mmx;
3192

    
3193
        c->h264_idct_dc_add=
3194
        c->h264_idct_add= ff_h264_idct_add_mmx;
3195
        c->h264_idct8_dc_add=
3196
        c->h264_idct8_add= ff_h264_idct8_add_mmx;
3197

    
3198
        if (mm_flags & MM_MMXEXT) {
3199
            c->prefetch = prefetch_mmx2;
3200

    
3201
            c->put_pixels_tab[0][1] = put_pixels16_x2_mmx2;
3202
            c->put_pixels_tab[0][2] = put_pixels16_y2_mmx2;
3203

    
3204
            c->avg_pixels_tab[0][0] = avg_pixels16_mmx2;
3205
            c->avg_pixels_tab[0][1] = avg_pixels16_x2_mmx2;
3206
            c->avg_pixels_tab[0][2] = avg_pixels16_y2_mmx2;
3207

    
3208
            c->put_pixels_tab[1][1] = put_pixels8_x2_mmx2;
3209
            c->put_pixels_tab[1][2] = put_pixels8_y2_mmx2;
3210

    
3211
            c->avg_pixels_tab[1][0] = avg_pixels8_mmx2;
3212
            c->avg_pixels_tab[1][1] = avg_pixels8_x2_mmx2;
3213
            c->avg_pixels_tab[1][2] = avg_pixels8_y2_mmx2;
3214

    
3215
#ifdef CONFIG_ENCODERS
3216
            c->hadamard8_diff[0]= hadamard8_diff16_mmx2;
3217
            c->hadamard8_diff[1]= hadamard8_diff_mmx2;
3218
            c->vsad[4]= vsad_intra16_mmx2;
3219
#endif //CONFIG_ENCODERS
3220

    
3221
            c->h264_idct_dc_add= ff_h264_idct_dc_add_mmx2;
3222
            c->h264_idct8_dc_add= ff_h264_idct8_dc_add_mmx2;
3223

    
3224
            if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
3225
                c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_mmx2;
3226
                c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_mmx2;
3227
                c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_mmx2;
3228
                c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_mmx2;
3229
                c->avg_pixels_tab[0][3] = avg_pixels16_xy2_mmx2;
3230
                c->avg_pixels_tab[1][3] = avg_pixels8_xy2_mmx2;
3231
#ifdef CONFIG_ENCODERS
3232
                c->vsad[0] = vsad16_mmx2;
3233
#endif //CONFIG_ENCODERS
3234
            }
3235

    
3236
#if 1
3237
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 0], qpel16_mc00_mmx2)
3238
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 1], qpel16_mc10_mmx2)
3239
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 2], qpel16_mc20_mmx2)
3240
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 3], qpel16_mc30_mmx2)
3241
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 4], qpel16_mc01_mmx2)
3242
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_mmx2)
3243
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 6], qpel16_mc21_mmx2)
3244
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_mmx2)
3245
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 8], qpel16_mc02_mmx2)
3246
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_mmx2)
3247
            SET_QPEL_FUNC(qpel_pixels_tab[0][10], qpel16_mc22_mmx2)
3248
            SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_mmx2)
3249
            SET_QPEL_FUNC(qpel_pixels_tab[0][12], qpel16_mc03_mmx2)
3250
            SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_mmx2)
3251
            SET_QPEL_FUNC(qpel_pixels_tab[0][14], qpel16_mc23_mmx2)
3252
            SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_mmx2)
3253
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 0], qpel8_mc00_mmx2)
3254
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 1], qpel8_mc10_mmx2)
3255
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 2], qpel8_mc20_mmx2)
3256
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 3], qpel8_mc30_mmx2)
3257
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 4], qpel8_mc01_mmx2)
3258
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_mmx2)
3259
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 6], qpel8_mc21_mmx2)
3260
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_mmx2)
3261
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 8], qpel8_mc02_mmx2)
3262
            SET_QPEL_FUNC(qpel_pixels_tab[1][