Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / h264dsp_mmx.c @ 28748a91

History | View | Annotate | Download (58.6 KB)

1
/*
2
 * Copyright (c) 2004-2005 Michael Niedermayer, Loren Merritt
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

    
21
#include "dsputil_mmx.h"
22

    
23
/***********************************/
24
/* IDCT */
25

    
26
#define SUMSUB_BADC( a, b, c, d ) \
27
    "paddw "#b", "#a" \n\t"\
28
    "paddw "#d", "#c" \n\t"\
29
    "paddw "#b", "#b" \n\t"\
30
    "paddw "#d", "#d" \n\t"\
31
    "psubw "#a", "#b" \n\t"\
32
    "psubw "#c", "#d" \n\t"
33

    
34
#define SUMSUBD2_AB( a, b, t ) \
35
    "movq  "#b", "#t" \n\t"\
36
    "psraw  $1 , "#b" \n\t"\
37
    "paddw "#a", "#b" \n\t"\
38
    "psraw  $1 , "#a" \n\t"\
39
    "psubw "#t", "#a" \n\t"
40

    
41
#define IDCT4_1D( s02, s13, d02, d13, t ) \
42
    SUMSUB_BA  ( s02, d02 )\
43
    SUMSUBD2_AB( s13, d13, t )\
44
    SUMSUB_BADC( d13, s02, s13, d02 )
45

    
46
#define STORE_DIFF_4P( p, t, z ) \
47
    "psraw      $6,     "#p" \n\t"\
48
    "movd       (%0),   "#t" \n\t"\
49
    "punpcklbw "#z",    "#t" \n\t"\
50
    "paddsw    "#t",    "#p" \n\t"\
51
    "packuswb  "#z",    "#p" \n\t"\
52
    "movd      "#p",    (%0) \n\t"
53

    
54
static void ff_h264_idct_add_mmx(uint8_t *dst, int16_t *block, int stride)
55
{
56
    /* Load dct coeffs */
57
    asm volatile(
58
        "movq   (%0), %%mm0 \n\t"
59
        "movq  8(%0), %%mm1 \n\t"
60
        "movq 16(%0), %%mm2 \n\t"
61
        "movq 24(%0), %%mm3 \n\t"
62
    :: "r"(block) );
63

    
64
    asm volatile(
65
        /* mm1=s02+s13  mm2=s02-s13  mm4=d02+d13  mm0=d02-d13 */
66
        IDCT4_1D( %%mm2, %%mm1, %%mm0, %%mm3, %%mm4 )
67

    
68
        "movq      %0,    %%mm6 \n\t"
69
        /* in: 1,4,0,2  out: 1,2,3,0 */
70
        TRANSPOSE4( %%mm3, %%mm1, %%mm0, %%mm2, %%mm4 )
71

    
72
        "paddw     %%mm6, %%mm3 \n\t"
73

    
74
        /* mm2=s02+s13  mm3=s02-s13  mm4=d02+d13  mm1=d02-d13 */
75
        IDCT4_1D( %%mm4, %%mm2, %%mm3, %%mm0, %%mm1 )
76

    
77
        "pxor %%mm7, %%mm7    \n\t"
78
    :: "m"(ff_pw_32));
79

    
80
    asm volatile(
81
    STORE_DIFF_4P( %%mm0, %%mm1, %%mm7)
82
        "add %1, %0             \n\t"
83
    STORE_DIFF_4P( %%mm2, %%mm1, %%mm7)
84
        "add %1, %0             \n\t"
85
    STORE_DIFF_4P( %%mm3, %%mm1, %%mm7)
86
        "add %1, %0             \n\t"
87
    STORE_DIFF_4P( %%mm4, %%mm1, %%mm7)
88
        : "+r"(dst)
89
        : "r" ((long)stride)
90
    );
91
}
92

    
93
static inline void h264_idct8_1d(int16_t *block)
94
{
95
    asm volatile(
96
        "movq 112(%0), %%mm7  \n\t"
97
        "movq  80(%0), %%mm5  \n\t"
98
        "movq  48(%0), %%mm3  \n\t"
99
        "movq  16(%0), %%mm1  \n\t"
100

    
101
        "movq   %%mm7, %%mm4  \n\t"
102
        "movq   %%mm3, %%mm6  \n\t"
103
        "movq   %%mm5, %%mm0  \n\t"
104
        "movq   %%mm7, %%mm2  \n\t"
105
        "psraw  $1,    %%mm4  \n\t"
106
        "psraw  $1,    %%mm6  \n\t"
107
        "psubw  %%mm7, %%mm0  \n\t"
108
        "psubw  %%mm6, %%mm2  \n\t"
109
        "psubw  %%mm4, %%mm0  \n\t"
110
        "psubw  %%mm3, %%mm2  \n\t"
111
        "psubw  %%mm3, %%mm0  \n\t"
112
        "paddw  %%mm1, %%mm2  \n\t"
113

    
114
        "movq   %%mm5, %%mm4  \n\t"
115
        "movq   %%mm1, %%mm6  \n\t"
116
        "psraw  $1,    %%mm4  \n\t"
117
        "psraw  $1,    %%mm6  \n\t"
118
        "paddw  %%mm5, %%mm4  \n\t"
119
        "paddw  %%mm1, %%mm6  \n\t"
120
        "paddw  %%mm7, %%mm4  \n\t"
121
        "paddw  %%mm5, %%mm6  \n\t"
122
        "psubw  %%mm1, %%mm4  \n\t"
123
        "paddw  %%mm3, %%mm6  \n\t"
124

    
125
        "movq   %%mm0, %%mm1  \n\t"
126
        "movq   %%mm4, %%mm3  \n\t"
127
        "movq   %%mm2, %%mm5  \n\t"
128
        "movq   %%mm6, %%mm7  \n\t"
129
        "psraw  $2,    %%mm6  \n\t"
130
        "psraw  $2,    %%mm3  \n\t"
131
        "psraw  $2,    %%mm5  \n\t"
132
        "psraw  $2,    %%mm0  \n\t"
133
        "paddw  %%mm6, %%mm1  \n\t"
134
        "paddw  %%mm2, %%mm3  \n\t"
135
        "psubw  %%mm4, %%mm5  \n\t"
136
        "psubw  %%mm0, %%mm7  \n\t"
137

    
138
        "movq  32(%0), %%mm2  \n\t"
139
        "movq  96(%0), %%mm6  \n\t"
140
        "movq   %%mm2, %%mm4  \n\t"
141
        "movq   %%mm6, %%mm0  \n\t"
142
        "psraw  $1,    %%mm4  \n\t"
143
        "psraw  $1,    %%mm6  \n\t"
144
        "psubw  %%mm0, %%mm4  \n\t"
145
        "paddw  %%mm2, %%mm6  \n\t"
146

    
147
        "movq    (%0), %%mm2  \n\t"
148
        "movq  64(%0), %%mm0  \n\t"
149
        SUMSUB_BA( %%mm0, %%mm2 )
150
        SUMSUB_BA( %%mm6, %%mm0 )
151
        SUMSUB_BA( %%mm4, %%mm2 )
152
        SUMSUB_BA( %%mm7, %%mm6 )
153
        SUMSUB_BA( %%mm5, %%mm4 )
154
        SUMSUB_BA( %%mm3, %%mm2 )
155
        SUMSUB_BA( %%mm1, %%mm0 )
156
        :: "r"(block)
157
    );
158
}
159

    
160
static void ff_h264_idct8_add_mmx(uint8_t *dst, int16_t *block, int stride)
161
{
162
    int i;
163
    int16_t __attribute__ ((aligned(8))) b2[64];
164

    
165
    block[0] += 32;
166

    
167
    for(i=0; i<2; i++){
168
        DECLARE_ALIGNED_8(uint64_t, tmp);
169

    
170
        h264_idct8_1d(block+4*i);
171

    
172
        asm volatile(
173
            "movq   %%mm7,    %0   \n\t"
174
            TRANSPOSE4( %%mm0, %%mm2, %%mm4, %%mm6, %%mm7 )
175
            "movq   %%mm0,  8(%1)  \n\t"
176
            "movq   %%mm6, 24(%1)  \n\t"
177
            "movq   %%mm7, 40(%1)  \n\t"
178
            "movq   %%mm4, 56(%1)  \n\t"
179
            "movq    %0,    %%mm7  \n\t"
180
            TRANSPOSE4( %%mm7, %%mm5, %%mm3, %%mm1, %%mm0 )
181
            "movq   %%mm7,   (%1)  \n\t"
182
            "movq   %%mm1, 16(%1)  \n\t"
183
            "movq   %%mm0, 32(%1)  \n\t"
184
            "movq   %%mm3, 48(%1)  \n\t"
185
            : "=m"(tmp)
186
            : "r"(b2+32*i)
187
            : "memory"
188
        );
189
    }
190

    
191
    for(i=0; i<2; i++){
192
        h264_idct8_1d(b2+4*i);
193

    
194
        asm volatile(
195
            "psraw     $6, %%mm7  \n\t"
196
            "psraw     $6, %%mm6  \n\t"
197
            "psraw     $6, %%mm5  \n\t"
198
            "psraw     $6, %%mm4  \n\t"
199
            "psraw     $6, %%mm3  \n\t"
200
            "psraw     $6, %%mm2  \n\t"
201
            "psraw     $6, %%mm1  \n\t"
202
            "psraw     $6, %%mm0  \n\t"
203

    
204
            "movq   %%mm7,    (%0)  \n\t"
205
            "movq   %%mm5,  16(%0)  \n\t"
206
            "movq   %%mm3,  32(%0)  \n\t"
207
            "movq   %%mm1,  48(%0)  \n\t"
208
            "movq   %%mm0,  64(%0)  \n\t"
209
            "movq   %%mm2,  80(%0)  \n\t"
210
            "movq   %%mm4,  96(%0)  \n\t"
211
            "movq   %%mm6, 112(%0)  \n\t"
212
            :: "r"(b2+4*i)
213
            : "memory"
214
        );
215
    }
216

    
217
    add_pixels_clamped_mmx(b2, dst, stride);
218
}
219

    
220
static void ff_h264_idct_dc_add_mmx2(uint8_t *dst, int16_t *block, int stride)
221
{
222
    int dc = (block[0] + 32) >> 6;
223
    asm volatile(
224
        "movd          %0, %%mm0 \n\t"
225
        "pshufw $0, %%mm0, %%mm0 \n\t"
226
        "pxor       %%mm1, %%mm1 \n\t"
227
        "psubw      %%mm0, %%mm1 \n\t"
228
        "packuswb   %%mm0, %%mm0 \n\t"
229
        "packuswb   %%mm1, %%mm1 \n\t"
230
        ::"r"(dc)
231
    );
232
    asm volatile(
233
        "movd          %0, %%mm2 \n\t"
234
        "movd          %1, %%mm3 \n\t"
235
        "movd          %2, %%mm4 \n\t"
236
        "movd          %3, %%mm5 \n\t"
237
        "paddusb    %%mm0, %%mm2 \n\t"
238
        "paddusb    %%mm0, %%mm3 \n\t"
239
        "paddusb    %%mm0, %%mm4 \n\t"
240
        "paddusb    %%mm0, %%mm5 \n\t"
241
        "psubusb    %%mm1, %%mm2 \n\t"
242
        "psubusb    %%mm1, %%mm3 \n\t"
243
        "psubusb    %%mm1, %%mm4 \n\t"
244
        "psubusb    %%mm1, %%mm5 \n\t"
245
        "movd       %%mm2, %0    \n\t"
246
        "movd       %%mm3, %1    \n\t"
247
        "movd       %%mm4, %2    \n\t"
248
        "movd       %%mm5, %3    \n\t"
249
        :"+m"(*(uint32_t*)(dst+0*stride)),
250
         "+m"(*(uint32_t*)(dst+1*stride)),
251
         "+m"(*(uint32_t*)(dst+2*stride)),
252
         "+m"(*(uint32_t*)(dst+3*stride))
253
    );
254
}
255

    
256
static void ff_h264_idct8_dc_add_mmx2(uint8_t *dst, int16_t *block, int stride)
257
{
258
    int dc = (block[0] + 32) >> 6;
259
    int y;
260
    asm volatile(
261
        "movd          %0, %%mm0 \n\t"
262
        "pshufw $0, %%mm0, %%mm0 \n\t"
263
        "pxor       %%mm1, %%mm1 \n\t"
264
        "psubw      %%mm0, %%mm1 \n\t"
265
        "packuswb   %%mm0, %%mm0 \n\t"
266
        "packuswb   %%mm1, %%mm1 \n\t"
267
        ::"r"(dc)
268
    );
269
    for(y=2; y--; dst += 4*stride){
270
    asm volatile(
271
        "movq          %0, %%mm2 \n\t"
272
        "movq          %1, %%mm3 \n\t"
273
        "movq          %2, %%mm4 \n\t"
274
        "movq          %3, %%mm5 \n\t"
275
        "paddusb    %%mm0, %%mm2 \n\t"
276
        "paddusb    %%mm0, %%mm3 \n\t"
277
        "paddusb    %%mm0, %%mm4 \n\t"
278
        "paddusb    %%mm0, %%mm5 \n\t"
279
        "psubusb    %%mm1, %%mm2 \n\t"
280
        "psubusb    %%mm1, %%mm3 \n\t"
281
        "psubusb    %%mm1, %%mm4 \n\t"
282
        "psubusb    %%mm1, %%mm5 \n\t"
283
        "movq       %%mm2, %0    \n\t"
284
        "movq       %%mm3, %1    \n\t"
285
        "movq       %%mm4, %2    \n\t"
286
        "movq       %%mm5, %3    \n\t"
287
        :"+m"(*(uint64_t*)(dst+0*stride)),
288
         "+m"(*(uint64_t*)(dst+1*stride)),
289
         "+m"(*(uint64_t*)(dst+2*stride)),
290
         "+m"(*(uint64_t*)(dst+3*stride))
291
    );
292
    }
293
}
294

    
295

    
296
/***********************************/
297
/* deblocking */
298

    
299
// out: o = |x-y|>a
300
// clobbers: t
301
#define DIFF_GT_MMX(x,y,a,o,t)\
302
    "movq     "#y", "#t"  \n\t"\
303
    "movq     "#x", "#o"  \n\t"\
304
    "psubusb  "#x", "#t"  \n\t"\
305
    "psubusb  "#y", "#o"  \n\t"\
306
    "por      "#t", "#o"  \n\t"\
307
    "psubusb  "#a", "#o"  \n\t"
308

    
309
// out: o = |x-y|>a
310
// clobbers: t
311
#define DIFF_GT2_MMX(x,y,a,o,t)\
312
    "movq     "#y", "#t"  \n\t"\
313
    "movq     "#x", "#o"  \n\t"\
314
    "psubusb  "#x", "#t"  \n\t"\
315
    "psubusb  "#y", "#o"  \n\t"\
316
    "psubusb  "#a", "#t"  \n\t"\
317
    "psubusb  "#a", "#o"  \n\t"\
318
    "pcmpeqb  "#t", "#o"  \n\t"\
319

    
320
// in: mm0=p1 mm1=p0 mm2=q0 mm3=q1
321
// out: mm5=beta-1, mm7=mask
322
// clobbers: mm4,mm6
323
#define H264_DEBLOCK_MASK(alpha1, beta1) \
324
    "pshufw $0, "#alpha1", %%mm4 \n\t"\
325
    "pshufw $0, "#beta1 ", %%mm5 \n\t"\
326
    "packuswb  %%mm4, %%mm4      \n\t"\
327
    "packuswb  %%mm5, %%mm5      \n\t"\
328
    DIFF_GT_MMX(%%mm1, %%mm2, %%mm4, %%mm7, %%mm6) /* |p0-q0| > alpha-1 */\
329
    DIFF_GT_MMX(%%mm0, %%mm1, %%mm5, %%mm4, %%mm6) /* |p1-p0| > beta-1 */\
330
    "por       %%mm4, %%mm7      \n\t"\
331
    DIFF_GT_MMX(%%mm3, %%mm2, %%mm5, %%mm4, %%mm6) /* |q1-q0| > beta-1 */\
332
    "por       %%mm4, %%mm7      \n\t"\
333
    "pxor      %%mm6, %%mm6      \n\t"\
334
    "pcmpeqb   %%mm6, %%mm7      \n\t"
335

    
336
// in: mm0=p1 mm1=p0 mm2=q0 mm3=q1 mm7=(tc&mask)
337
// out: mm1=p0' mm2=q0'
338
// clobbers: mm0,3-6
339
#define H264_DEBLOCK_P0_Q0(pb_01, pb_3f)\
340
        "movq    %%mm1              , %%mm5 \n\t"\
341
        "pxor    %%mm2              , %%mm5 \n\t" /* p0^q0*/\
342
        "pand    "#pb_01"           , %%mm5 \n\t" /* (p0^q0)&1*/\
343
        "pcmpeqb %%mm4              , %%mm4 \n\t"\
344
        "pxor    %%mm4              , %%mm3 \n\t"\
345
        "pavgb   %%mm0              , %%mm3 \n\t" /* (p1 - q1 + 256)>>1*/\
346
        "pavgb   "MANGLE(ff_pb_3)"  , %%mm3 \n\t" /*(((p1 - q1 + 256)>>1)+4)>>1 = 64+2+(p1-q1)>>2*/\
347
        "pxor    %%mm1              , %%mm4 \n\t"\
348
        "pavgb   %%mm2              , %%mm4 \n\t" /* (q0 - p0 + 256)>>1*/\
349
        "pavgb   %%mm5              , %%mm3 \n\t"\
350
        "paddusb %%mm4              , %%mm3 \n\t" /* d+128+33*/\
351
        "movq    "MANGLE(ff_pb_A1)" , %%mm6 \n\t"\
352
        "psubusb %%mm3              , %%mm6 \n\t"\
353
        "psubusb "MANGLE(ff_pb_A1)" , %%mm3 \n\t"\
354
        "pminub  %%mm7              , %%mm6 \n\t"\
355
        "pminub  %%mm7              , %%mm3 \n\t"\
356
        "psubusb %%mm6              , %%mm1 \n\t"\
357
        "psubusb %%mm3              , %%mm2 \n\t"\
358
        "paddusb %%mm3              , %%mm1 \n\t"\
359
        "paddusb %%mm6              , %%mm2 \n\t"
360

    
361
// in: mm0=p1 mm1=p0 mm2=q0 mm3=q1 mm7=(tc&mask) %8=ff_bone
362
// out: (q1addr) = av_clip( (q2+((p0+q0+1)>>1))>>1, q1-tc0, q1+tc0 )
363
// clobbers: q2, tmp, tc0
364
#define H264_DEBLOCK_Q1(p1, q2, q2addr, q1addr, tc0, tmp)\
365
        "movq     %%mm1,  "#tmp"   \n\t"\
366
        "pavgb    %%mm2,  "#tmp"   \n\t"\
367
        "pavgb    "#tmp", "#q2"    \n\t" /* avg(p2,avg(p0,q0)) */\
368
        "pxor   "q2addr", "#tmp"   \n\t"\
369
        "pand     %8,     "#tmp"   \n\t" /* (p2^avg(p0,q0))&1 */\
370
        "psubusb  "#tmp", "#q2"    \n\t" /* (p2+((p0+q0+1)>>1))>>1 */\
371
        "movq     "#p1",  "#tmp"   \n\t"\
372
        "psubusb  "#tc0", "#tmp"   \n\t"\
373
        "paddusb  "#p1",  "#tc0"   \n\t"\
374
        "pmaxub   "#tmp", "#q2"    \n\t"\
375
        "pminub   "#tc0", "#q2"    \n\t"\
376
        "movq     "#q2",  "q1addr" \n\t"
377

    
378
static inline void h264_loop_filter_luma_mmx2(uint8_t *pix, int stride, int alpha1, int beta1, int8_t *tc0)
379
{
380
    DECLARE_ALIGNED_8(uint64_t, tmp0[2]);
381

    
382
    asm volatile(
383
        "movq    (%1,%3), %%mm0    \n\t" //p1
384
        "movq    (%1,%3,2), %%mm1  \n\t" //p0
385
        "movq    (%2),    %%mm2    \n\t" //q0
386
        "movq    (%2,%3), %%mm3    \n\t" //q1
387
        H264_DEBLOCK_MASK(%6, %7)
388

    
389
        "movd      %5,    %%mm4    \n\t"
390
        "punpcklbw %%mm4, %%mm4    \n\t"
391
        "punpcklwd %%mm4, %%mm4    \n\t"
392
        "pcmpeqb   %%mm3, %%mm3    \n\t"
393
        "movq      %%mm4, %%mm6    \n\t"
394
        "pcmpgtb   %%mm3, %%mm4    \n\t"
395
        "movq      %%mm6, 8+%0     \n\t"
396
        "pand      %%mm4, %%mm7    \n\t"
397
        "movq      %%mm7, %0       \n\t"
398

    
399
        /* filter p1 */
400
        "movq     (%1),   %%mm3    \n\t" //p2
401
        DIFF_GT2_MMX(%%mm1, %%mm3, %%mm5, %%mm6, %%mm4) // |p2-p0|>beta-1
402
        "pand     %%mm7,  %%mm6    \n\t" // mask & |p2-p0|<beta
403
        "pand     8+%0,   %%mm7    \n\t" // mask & tc0
404
        "movq     %%mm7,  %%mm4    \n\t"
405
        "psubb    %%mm6,  %%mm7    \n\t"
406
        "pand     %%mm4,  %%mm6    \n\t" // mask & |p2-p0|<beta & tc0
407
        H264_DEBLOCK_Q1(%%mm0, %%mm3, "(%1)", "(%1,%3)", %%mm6, %%mm4)
408

    
409
        /* filter q1 */
410
        "movq    (%2,%3,2), %%mm4  \n\t" //q2
411
        DIFF_GT2_MMX(%%mm2, %%mm4, %%mm5, %%mm6, %%mm3) // |q2-q0|>beta-1
412
        "pand     %0,     %%mm6    \n\t"
413
        "movq     8+%0,   %%mm5    \n\t" // can be merged with the and below but is slower then
414
        "pand     %%mm6,  %%mm5    \n\t"
415
        "psubb    %%mm6,  %%mm7    \n\t"
416
        "movq    (%2,%3), %%mm3    \n\t"
417
        H264_DEBLOCK_Q1(%%mm3, %%mm4, "(%2,%3,2)", "(%2,%3)", %%mm5, %%mm6)
418

    
419
        /* filter p0, q0 */
420
        H264_DEBLOCK_P0_Q0(%8, unused)
421
        "movq      %%mm1, (%1,%3,2) \n\t"
422
        "movq      %%mm2, (%2)      \n\t"
423

    
424
        : "=m"(*tmp0)
425
        : "r"(pix-3*stride), "r"(pix), "r"((long)stride),
426
          "m"(*tmp0/*unused*/), "m"(*(uint32_t*)tc0), "m"(alpha1), "m"(beta1),
427
          "m"(ff_bone)
428
    );
429
}
430

    
431
static void h264_v_loop_filter_luma_mmx2(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
432
{
433
    if((tc0[0] & tc0[1]) >= 0)
434
        h264_loop_filter_luma_mmx2(pix, stride, alpha-1, beta-1, tc0);
435
    if((tc0[2] & tc0[3]) >= 0)
436
        h264_loop_filter_luma_mmx2(pix+8, stride, alpha-1, beta-1, tc0+2);
437
}
438
static void h264_h_loop_filter_luma_mmx2(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
439
{
440
    //FIXME: could cut some load/stores by merging transpose with filter
441
    // also, it only needs to transpose 6x8
442
    DECLARE_ALIGNED_8(uint8_t, trans[8*8]);
443
    int i;
444
    for(i=0; i<2; i++, pix+=8*stride, tc0+=2) {
445
        if((tc0[0] & tc0[1]) < 0)
446
            continue;
447
        transpose4x4(trans,       pix-4,          8, stride);
448
        transpose4x4(trans  +4*8, pix,            8, stride);
449
        transpose4x4(trans+4,     pix-4+4*stride, 8, stride);
450
        transpose4x4(trans+4+4*8, pix  +4*stride, 8, stride);
451
        h264_loop_filter_luma_mmx2(trans+4*8, 8, alpha-1, beta-1, tc0);
452
        transpose4x4(pix-2,          trans  +2*8, stride, 8);
453
        transpose4x4(pix-2+4*stride, trans+4+2*8, stride, 8);
454
    }
455
}
456

    
457
static inline void h264_loop_filter_chroma_mmx2(uint8_t *pix, int stride, int alpha1, int beta1, int8_t *tc0)
458
{
459
    asm volatile(
460
        "movq    (%0),    %%mm0     \n\t" //p1
461
        "movq    (%0,%2), %%mm1     \n\t" //p0
462
        "movq    (%1),    %%mm2     \n\t" //q0
463
        "movq    (%1,%2), %%mm3     \n\t" //q1
464
        H264_DEBLOCK_MASK(%4, %5)
465
        "movd      %3,    %%mm6     \n\t"
466
        "punpcklbw %%mm6, %%mm6     \n\t"
467
        "pand      %%mm6, %%mm7     \n\t" // mm7 = tc&mask
468
        H264_DEBLOCK_P0_Q0(%6, %7)
469
        "movq      %%mm1, (%0,%2)   \n\t"
470
        "movq      %%mm2, (%1)      \n\t"
471

    
472
        :: "r"(pix-2*stride), "r"(pix), "r"((long)stride),
473
           "r"(*(uint32_t*)tc0),
474
           "m"(alpha1), "m"(beta1), "m"(ff_bone), "m"(ff_pb_3F)
475
    );
476
}
477

    
478
static void h264_v_loop_filter_chroma_mmx2(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
479
{
480
    h264_loop_filter_chroma_mmx2(pix, stride, alpha-1, beta-1, tc0);
481
}
482

    
483
static void h264_h_loop_filter_chroma_mmx2(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
484
{
485
    //FIXME: could cut some load/stores by merging transpose with filter
486
    DECLARE_ALIGNED_8(uint8_t, trans[8*4]);
487
    transpose4x4(trans, pix-2, 8, stride);
488
    transpose4x4(trans+4, pix-2+4*stride, 8, stride);
489
    h264_loop_filter_chroma_mmx2(trans+2*8, 8, alpha-1, beta-1, tc0);
490
    transpose4x4(pix-2, trans, stride, 8);
491
    transpose4x4(pix-2+4*stride, trans+4, stride, 8);
492
}
493

    
494
// p0 = (p0 + q1 + 2*p1 + 2) >> 2
495
#define H264_FILTER_CHROMA4(p0, p1, q1, one) \
496
    "movq    "#p0", %%mm4  \n\t"\
497
    "pxor    "#q1", %%mm4  \n\t"\
498
    "pand   "#one", %%mm4  \n\t" /* mm4 = (p0^q1)&1 */\
499
    "pavgb   "#q1", "#p0"  \n\t"\
500
    "psubusb %%mm4, "#p0"  \n\t"\
501
    "pavgb   "#p1", "#p0"  \n\t" /* dst = avg(p1, avg(p0,q1) - ((p0^q1)&1)) */\
502

    
503
static inline void h264_loop_filter_chroma_intra_mmx2(uint8_t *pix, int stride, int alpha1, int beta1)
504
{
505
    asm volatile(
506
        "movq    (%0),    %%mm0     \n\t"
507
        "movq    (%0,%2), %%mm1     \n\t"
508
        "movq    (%1),    %%mm2     \n\t"
509
        "movq    (%1,%2), %%mm3     \n\t"
510
        H264_DEBLOCK_MASK(%3, %4)
511
        "movq    %%mm1,   %%mm5     \n\t"
512
        "movq    %%mm2,   %%mm6     \n\t"
513
        H264_FILTER_CHROMA4(%%mm1, %%mm0, %%mm3, %5) //p0'
514
        H264_FILTER_CHROMA4(%%mm2, %%mm3, %%mm0, %5) //q0'
515
        "psubb   %%mm5,   %%mm1     \n\t"
516
        "psubb   %%mm6,   %%mm2     \n\t"
517
        "pand    %%mm7,   %%mm1     \n\t"
518
        "pand    %%mm7,   %%mm2     \n\t"
519
        "paddb   %%mm5,   %%mm1     \n\t"
520
        "paddb   %%mm6,   %%mm2     \n\t"
521
        "movq    %%mm1,   (%0,%2)   \n\t"
522
        "movq    %%mm2,   (%1)      \n\t"
523
        :: "r"(pix-2*stride), "r"(pix), "r"((long)stride),
524
           "m"(alpha1), "m"(beta1), "m"(ff_bone)
525
    );
526
}
527

    
528
static void h264_v_loop_filter_chroma_intra_mmx2(uint8_t *pix, int stride, int alpha, int beta)
529
{
530
    h264_loop_filter_chroma_intra_mmx2(pix, stride, alpha-1, beta-1);
531
}
532

    
533
static void h264_h_loop_filter_chroma_intra_mmx2(uint8_t *pix, int stride, int alpha, int beta)
534
{
535
    //FIXME: could cut some load/stores by merging transpose with filter
536
    DECLARE_ALIGNED_8(uint8_t, trans[8*4]);
537
    transpose4x4(trans, pix-2, 8, stride);
538
    transpose4x4(trans+4, pix-2+4*stride, 8, stride);
539
    h264_loop_filter_chroma_intra_mmx2(trans+2*8, 8, alpha-1, beta-1);
540
    transpose4x4(pix-2, trans, stride, 8);
541
    transpose4x4(pix-2+4*stride, trans+4, stride, 8);
542
}
543

    
544
static void h264_loop_filter_strength_mmx2( int16_t bS[2][4][4], uint8_t nnz[40], int8_t ref[2][40], int16_t mv[2][40][2],
545
                                            int bidir, int edges, int step, int mask_mv0, int mask_mv1 ) {
546
    int dir;
547
    asm volatile(
548
        "pxor %%mm7, %%mm7 \n\t"
549
        "movq %0, %%mm6 \n\t"
550
        "movq %1, %%mm5 \n\t"
551
        "movq %2, %%mm4 \n\t"
552
        ::"m"(ff_pb_1), "m"(ff_pb_3), "m"(ff_pb_7)
553
    );
554
    // could do a special case for dir==0 && edges==1, but it only reduces the
555
    // average filter time by 1.2%
556
    for( dir=1; dir>=0; dir-- ) {
557
        const int d_idx = dir ? -8 : -1;
558
        const int mask_mv = dir ? mask_mv1 : mask_mv0;
559
        DECLARE_ALIGNED_8(const uint64_t, mask_dir) = dir ? 0 : 0xffffffffffffffffULL;
560
        int b_idx, edge, l;
561
        for( b_idx=12, edge=0; edge<edges; edge+=step, b_idx+=8*step ) {
562
            asm volatile(
563
                "pand %0, %%mm0 \n\t"
564
                ::"m"(mask_dir)
565
            );
566
            if(!(mask_mv & edge)) {
567
                asm volatile("pxor %%mm0, %%mm0 \n\t":);
568
                for( l = bidir; l >= 0; l-- ) {
569
                    asm volatile(
570
                        "movd %0, %%mm1 \n\t"
571
                        "punpckldq %1, %%mm1 \n\t"
572
                        "movq %%mm1, %%mm2 \n\t"
573
                        "psrlw $7, %%mm2 \n\t"
574
                        "pand %%mm6, %%mm2 \n\t"
575
                        "por %%mm2, %%mm1 \n\t" // ref_cache with -2 mapped to -1
576
                        "punpckldq %%mm1, %%mm2 \n\t"
577
                        "pcmpeqb %%mm2, %%mm1 \n\t"
578
                        "paddb %%mm6, %%mm1 \n\t"
579
                        "punpckhbw %%mm7, %%mm1 \n\t" // ref[b] != ref[bn]
580
                        "por %%mm1, %%mm0 \n\t"
581

    
582
                        "movq %2, %%mm1 \n\t"
583
                        "movq %3, %%mm2 \n\t"
584
                        "psubw %4, %%mm1 \n\t"
585
                        "psubw %5, %%mm2 \n\t"
586
                        "packsswb %%mm2, %%mm1 \n\t"
587
                        "paddb %%mm5, %%mm1 \n\t"
588
                        "pminub %%mm4, %%mm1 \n\t"
589
                        "pcmpeqb %%mm4, %%mm1 \n\t" // abs(mv[b] - mv[bn]) >= limit
590
                        "por %%mm1, %%mm0 \n\t"
591
                        ::"m"(ref[l][b_idx]),
592
                          "m"(ref[l][b_idx+d_idx]),
593
                          "m"(mv[l][b_idx][0]),
594
                          "m"(mv[l][b_idx+2][0]),
595
                          "m"(mv[l][b_idx+d_idx][0]),
596
                          "m"(mv[l][b_idx+d_idx+2][0])
597
                    );
598
                }
599
            }
600
            asm volatile(
601
                "movd %0, %%mm1 \n\t"
602
                "por  %1, %%mm1 \n\t"
603
                "punpcklbw %%mm7, %%mm1 \n\t"
604
                "pcmpgtw %%mm7, %%mm1 \n\t" // nnz[b] || nnz[bn]
605
                ::"m"(nnz[b_idx]),
606
                  "m"(nnz[b_idx+d_idx])
607
            );
608
            asm volatile(
609
                "pcmpeqw %%mm7, %%mm0 \n\t"
610
                "pcmpeqw %%mm7, %%mm0 \n\t"
611
                "psrlw $15, %%mm0 \n\t" // nonzero -> 1
612
                "psrlw $14, %%mm1 \n\t"
613
                "movq %%mm0, %%mm2 \n\t"
614
                "por %%mm1, %%mm2 \n\t"
615
                "psrlw $1, %%mm1 \n\t"
616
                "pandn %%mm2, %%mm1 \n\t"
617
                "movq %%mm1, %0 \n\t"
618
                :"=m"(*bS[dir][edge])
619
                ::"memory"
620
            );
621
        }
622
        edges = 4;
623
        step = 1;
624
    }
625
    asm volatile(
626
        "movq   (%0), %%mm0 \n\t"
627
        "movq  8(%0), %%mm1 \n\t"
628
        "movq 16(%0), %%mm2 \n\t"
629
        "movq 24(%0), %%mm3 \n\t"
630
        TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4)
631
        "movq %%mm0,   (%0) \n\t"
632
        "movq %%mm3,  8(%0) \n\t"
633
        "movq %%mm4, 16(%0) \n\t"
634
        "movq %%mm2, 24(%0) \n\t"
635
        ::"r"(bS[0])
636
        :"memory"
637
    );
638
}
639

    
640
/***********************************/
641
/* motion compensation */
642

    
643
#define QPEL_H264V(A,B,C,D,E,F,OP)\
644
        "movd (%0), "#F"            \n\t"\
645
        "movq "#C", %%mm6           \n\t"\
646
        "paddw "#D", %%mm6          \n\t"\
647
        "psllw $2, %%mm6            \n\t"\
648
        "psubw "#B", %%mm6          \n\t"\
649
        "psubw "#E", %%mm6          \n\t"\
650
        "pmullw %4, %%mm6           \n\t"\
651
        "add %2, %0                 \n\t"\
652
        "punpcklbw %%mm7, "#F"      \n\t"\
653
        "paddw %5, "#A"             \n\t"\
654
        "paddw "#F", "#A"           \n\t"\
655
        "paddw "#A", %%mm6          \n\t"\
656
        "psraw $5, %%mm6            \n\t"\
657
        "packuswb %%mm6, %%mm6      \n\t"\
658
        OP(%%mm6, (%1), A, d)\
659
        "add %3, %1                 \n\t"
660

    
661
#define QPEL_H264HV(A,B,C,D,E,F,OF)\
662
        "movd (%0), "#F"            \n\t"\
663
        "movq "#C", %%mm6           \n\t"\
664
        "paddw "#D", %%mm6          \n\t"\
665
        "psllw $2, %%mm6            \n\t"\
666
        "psubw "#B", %%mm6          \n\t"\
667
        "psubw "#E", %%mm6          \n\t"\
668
        "pmullw %3, %%mm6           \n\t"\
669
        "add %2, %0                 \n\t"\
670
        "punpcklbw %%mm7, "#F"      \n\t"\
671
        "paddw "#F", "#A"           \n\t"\
672
        "paddw "#A", %%mm6          \n\t"\
673
        "movq %%mm6, "#OF"(%1)      \n\t"
674

    
675
#define QPEL_H264(OPNAME, OP, MMX)\
676
static av_noinline void OPNAME ## h264_qpel4_h_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
677
    int h=4;\
678
\
679
    asm volatile(\
680
        "pxor %%mm7, %%mm7          \n\t"\
681
        "movq %5, %%mm4             \n\t"\
682
        "movq %6, %%mm5             \n\t"\
683
        "1:                         \n\t"\
684
        "movd  -1(%0), %%mm1        \n\t"\
685
        "movd    (%0), %%mm2        \n\t"\
686
        "movd   1(%0), %%mm3        \n\t"\
687
        "movd   2(%0), %%mm0        \n\t"\
688
        "punpcklbw %%mm7, %%mm1     \n\t"\
689
        "punpcklbw %%mm7, %%mm2     \n\t"\
690
        "punpcklbw %%mm7, %%mm3     \n\t"\
691
        "punpcklbw %%mm7, %%mm0     \n\t"\
692
        "paddw %%mm0, %%mm1         \n\t"\
693
        "paddw %%mm3, %%mm2         \n\t"\
694
        "movd  -2(%0), %%mm0        \n\t"\
695
        "movd   3(%0), %%mm3        \n\t"\
696
        "punpcklbw %%mm7, %%mm0     \n\t"\
697
        "punpcklbw %%mm7, %%mm3     \n\t"\
698
        "paddw %%mm3, %%mm0         \n\t"\
699
        "psllw $2, %%mm2            \n\t"\
700
        "psubw %%mm1, %%mm2         \n\t"\
701
        "pmullw %%mm4, %%mm2        \n\t"\
702
        "paddw %%mm5, %%mm0         \n\t"\
703
        "paddw %%mm2, %%mm0         \n\t"\
704
        "psraw $5, %%mm0            \n\t"\
705
        "packuswb %%mm0, %%mm0      \n\t"\
706
        OP(%%mm0, (%1),%%mm6, d)\
707
        "add %3, %0                 \n\t"\
708
        "add %4, %1                 \n\t"\
709
        "decl %2                    \n\t"\
710
        " jnz 1b                    \n\t"\
711
        : "+a"(src), "+c"(dst), "+m"(h)\
712
        : "d"((long)srcStride), "S"((long)dstStride), "m"(ff_pw_5), "m"(ff_pw_16)\
713
        : "memory"\
714
    );\
715
}\
716
static av_noinline void OPNAME ## h264_qpel4_h_lowpass_l2_ ## MMX(uint8_t *dst, uint8_t *src, uint8_t *src2, int dstStride, int src2Stride){\
717
    int h=4;\
718
    asm volatile(\
719
        "pxor %%mm7, %%mm7          \n\t"\
720
        "movq %0, %%mm4             \n\t"\
721
        "movq %1, %%mm5             \n\t"\
722
        :: "m"(ff_pw_5), "m"(ff_pw_16)\
723
    );\
724
    do{\
725
    asm volatile(\
726
        "movd  -1(%0), %%mm1        \n\t"\
727
        "movd    (%0), %%mm2        \n\t"\
728
        "movd   1(%0), %%mm3        \n\t"\
729
        "movd   2(%0), %%mm0        \n\t"\
730
        "punpcklbw %%mm7, %%mm1     \n\t"\
731
        "punpcklbw %%mm7, %%mm2     \n\t"\
732
        "punpcklbw %%mm7, %%mm3     \n\t"\
733
        "punpcklbw %%mm7, %%mm0     \n\t"\
734
        "paddw %%mm0, %%mm1         \n\t"\
735
        "paddw %%mm3, %%mm2         \n\t"\
736
        "movd  -2(%0), %%mm0        \n\t"\
737
        "movd   3(%0), %%mm3        \n\t"\
738
        "punpcklbw %%mm7, %%mm0     \n\t"\
739
        "punpcklbw %%mm7, %%mm3     \n\t"\
740
        "paddw %%mm3, %%mm0         \n\t"\
741
        "psllw $2, %%mm2            \n\t"\
742
        "psubw %%mm1, %%mm2         \n\t"\
743
        "pmullw %%mm4, %%mm2        \n\t"\
744
        "paddw %%mm5, %%mm0         \n\t"\
745
        "paddw %%mm2, %%mm0         \n\t"\
746
        "movd   (%2), %%mm3         \n\t"\
747
        "psraw $5, %%mm0            \n\t"\
748
        "packuswb %%mm0, %%mm0      \n\t"\
749
        PAVGB" %%mm3, %%mm0         \n\t"\
750
        OP(%%mm0, (%1),%%mm6, d)\
751
        "add %4, %0                 \n\t"\
752
        "add %4, %1                 \n\t"\
753
        "add %3, %2                 \n\t"\
754
        : "+a"(src), "+c"(dst), "+d"(src2)\
755
        : "D"((long)src2Stride), "S"((long)dstStride)\
756
        : "memory"\
757
    );\
758
    }while(--h);\
759
}\
760
static av_noinline void OPNAME ## h264_qpel4_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
761
    src -= 2*srcStride;\
762
    asm volatile(\
763
        "pxor %%mm7, %%mm7          \n\t"\
764
        "movd (%0), %%mm0           \n\t"\
765
        "add %2, %0                 \n\t"\
766
        "movd (%0), %%mm1           \n\t"\
767
        "add %2, %0                 \n\t"\
768
        "movd (%0), %%mm2           \n\t"\
769
        "add %2, %0                 \n\t"\
770
        "movd (%0), %%mm3           \n\t"\
771
        "add %2, %0                 \n\t"\
772
        "movd (%0), %%mm4           \n\t"\
773
        "add %2, %0                 \n\t"\
774
        "punpcklbw %%mm7, %%mm0     \n\t"\
775
        "punpcklbw %%mm7, %%mm1     \n\t"\
776
        "punpcklbw %%mm7, %%mm2     \n\t"\
777
        "punpcklbw %%mm7, %%mm3     \n\t"\
778
        "punpcklbw %%mm7, %%mm4     \n\t"\
779
        QPEL_H264V(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4, %%mm5, OP)\
780
        QPEL_H264V(%%mm1, %%mm2, %%mm3, %%mm4, %%mm5, %%mm0, OP)\
781
        QPEL_H264V(%%mm2, %%mm3, %%mm4, %%mm5, %%mm0, %%mm1, OP)\
782
        QPEL_H264V(%%mm3, %%mm4, %%mm5, %%mm0, %%mm1, %%mm2, OP)\
783
         \
784
        : "+a"(src), "+c"(dst)\
785
        : "S"((long)srcStride), "D"((long)dstStride), "m"(ff_pw_5), "m"(ff_pw_16)\
786
        : "memory"\
787
    );\
788
}\
789
static av_noinline void OPNAME ## h264_qpel4_hv_lowpass_ ## MMX(uint8_t *dst, int16_t *tmp, uint8_t *src, int dstStride, int tmpStride, int srcStride){\
790
    int h=4;\
791
    int w=3;\
792
    src -= 2*srcStride+2;\
793
    while(w--){\
794
        asm volatile(\
795
            "pxor %%mm7, %%mm7      \n\t"\
796
            "movd (%0), %%mm0       \n\t"\
797
            "add %2, %0             \n\t"\
798
            "movd (%0), %%mm1       \n\t"\
799
            "add %2, %0             \n\t"\
800
            "movd (%0), %%mm2       \n\t"\
801
            "add %2, %0             \n\t"\
802
            "movd (%0), %%mm3       \n\t"\
803
            "add %2, %0             \n\t"\
804
            "movd (%0), %%mm4       \n\t"\
805
            "add %2, %0             \n\t"\
806
            "punpcklbw %%mm7, %%mm0 \n\t"\
807
            "punpcklbw %%mm7, %%mm1 \n\t"\
808
            "punpcklbw %%mm7, %%mm2 \n\t"\
809
            "punpcklbw %%mm7, %%mm3 \n\t"\
810
            "punpcklbw %%mm7, %%mm4 \n\t"\
811
            QPEL_H264HV(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4, %%mm5, 0*8*3)\
812
            QPEL_H264HV(%%mm1, %%mm2, %%mm3, %%mm4, %%mm5, %%mm0, 1*8*3)\
813
            QPEL_H264HV(%%mm2, %%mm3, %%mm4, %%mm5, %%mm0, %%mm1, 2*8*3)\
814
            QPEL_H264HV(%%mm3, %%mm4, %%mm5, %%mm0, %%mm1, %%mm2, 3*8*3)\
815
             \
816
            : "+a"(src)\
817
            : "c"(tmp), "S"((long)srcStride), "m"(ff_pw_5)\
818
            : "memory"\
819
        );\
820
        tmp += 4;\
821
        src += 4 - 9*srcStride;\
822
    }\
823
    tmp -= 3*4;\
824
    asm volatile(\
825
        "movq %4, %%mm6             \n\t"\
826
        "1:                         \n\t"\
827
        "movq     (%0), %%mm0       \n\t"\
828
        "paddw  10(%0), %%mm0       \n\t"\
829
        "movq    2(%0), %%mm1       \n\t"\
830
        "paddw   8(%0), %%mm1       \n\t"\
831
        "movq    4(%0), %%mm2       \n\t"\
832
        "paddw   6(%0), %%mm2       \n\t"\
833
        "psubw %%mm1, %%mm0         \n\t"/*a-b   (abccba)*/\
834
        "psraw $2, %%mm0            \n\t"/*(a-b)/4 */\
835
        "psubw %%mm1, %%mm0         \n\t"/*(a-b)/4-b */\
836
        "paddsw %%mm2, %%mm0        \n\t"\
837
        "psraw $2, %%mm0            \n\t"/*((a-b)/4-b+c)/4 */\
838
        "paddw %%mm6, %%mm2         \n\t"\
839
        "paddw %%mm2, %%mm0         \n\t"/*(a-5*b+20*c)/16 +32 */\
840
        "psraw $6, %%mm0            \n\t"\
841
        "packuswb %%mm0, %%mm0      \n\t"\
842
        OP(%%mm0, (%1),%%mm7, d)\
843
        "add $24, %0                \n\t"\
844
        "add %3, %1                 \n\t"\
845
        "decl %2                    \n\t"\
846
        " jnz 1b                    \n\t"\
847
        : "+a"(tmp), "+c"(dst), "+m"(h)\
848
        : "S"((long)dstStride), "m"(ff_pw_32)\
849
        : "memory"\
850
    );\
851
}\
852
\
853
static av_noinline void OPNAME ## h264_qpel8_h_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
854
    int h=8;\
855
    asm volatile(\
856
        "pxor %%mm7, %%mm7          \n\t"\
857
        "movq %5, %%mm6             \n\t"\
858
        "1:                         \n\t"\
859
        "movq    (%0), %%mm0        \n\t"\
860
        "movq   1(%0), %%mm2        \n\t"\
861
        "movq %%mm0, %%mm1          \n\t"\
862
        "movq %%mm2, %%mm3          \n\t"\
863
        "punpcklbw %%mm7, %%mm0     \n\t"\
864
        "punpckhbw %%mm7, %%mm1     \n\t"\
865
        "punpcklbw %%mm7, %%mm2     \n\t"\
866
        "punpckhbw %%mm7, %%mm3     \n\t"\
867
        "paddw %%mm2, %%mm0         \n\t"\
868
        "paddw %%mm3, %%mm1         \n\t"\
869
        "psllw $2, %%mm0            \n\t"\
870
        "psllw $2, %%mm1            \n\t"\
871
        "movq   -1(%0), %%mm2       \n\t"\
872
        "movq    2(%0), %%mm4       \n\t"\
873
        "movq %%mm2, %%mm3          \n\t"\
874
        "movq %%mm4, %%mm5          \n\t"\
875
        "punpcklbw %%mm7, %%mm2     \n\t"\
876
        "punpckhbw %%mm7, %%mm3     \n\t"\
877
        "punpcklbw %%mm7, %%mm4     \n\t"\
878
        "punpckhbw %%mm7, %%mm5     \n\t"\
879
        "paddw %%mm4, %%mm2         \n\t"\
880
        "paddw %%mm3, %%mm5         \n\t"\
881
        "psubw %%mm2, %%mm0         \n\t"\
882
        "psubw %%mm5, %%mm1         \n\t"\
883
        "pmullw %%mm6, %%mm0        \n\t"\
884
        "pmullw %%mm6, %%mm1        \n\t"\
885
        "movd   -2(%0), %%mm2       \n\t"\
886
        "movd    7(%0), %%mm5       \n\t"\
887
        "punpcklbw %%mm7, %%mm2     \n\t"\
888
        "punpcklbw %%mm7, %%mm5     \n\t"\
889
        "paddw %%mm3, %%mm2         \n\t"\
890
        "paddw %%mm5, %%mm4         \n\t"\
891
        "movq %6, %%mm5             \n\t"\
892
        "paddw %%mm5, %%mm2         \n\t"\
893
        "paddw %%mm5, %%mm4         \n\t"\
894
        "paddw %%mm2, %%mm0         \n\t"\
895
        "paddw %%mm4, %%mm1         \n\t"\
896
        "psraw $5, %%mm0            \n\t"\
897
        "psraw $5, %%mm1            \n\t"\
898
        "packuswb %%mm1, %%mm0      \n\t"\
899
        OP(%%mm0, (%1),%%mm5, q)\
900
        "add %3, %0                 \n\t"\
901
        "add %4, %1                 \n\t"\
902
        "decl %2                    \n\t"\
903
        " jnz 1b                    \n\t"\
904
        : "+a"(src), "+c"(dst), "+m"(h)\
905
        : "d"((long)srcStride), "S"((long)dstStride), "m"(ff_pw_5), "m"(ff_pw_16)\
906
        : "memory"\
907
    );\
908
}\
909
\
910
static av_noinline void OPNAME ## h264_qpel8_h_lowpass_l2_ ## MMX(uint8_t *dst, uint8_t *src, uint8_t *src2, int dstStride, int src2Stride){\
911
    int h=8;\
912
    asm volatile(\
913
        "pxor %%mm7, %%mm7          \n\t"\
914
        "movq %0, %%mm6             \n\t"\
915
        :: "m"(ff_pw_5)\
916
    );\
917
    do{\
918
    asm volatile(\
919
        "movq    (%0), %%mm0        \n\t"\
920
        "movq   1(%0), %%mm2        \n\t"\
921
        "movq %%mm0, %%mm1          \n\t"\
922
        "movq %%mm2, %%mm3          \n\t"\
923
        "punpcklbw %%mm7, %%mm0     \n\t"\
924
        "punpckhbw %%mm7, %%mm1     \n\t"\
925
        "punpcklbw %%mm7, %%mm2     \n\t"\
926
        "punpckhbw %%mm7, %%mm3     \n\t"\
927
        "paddw %%mm2, %%mm0         \n\t"\
928
        "paddw %%mm3, %%mm1         \n\t"\
929
        "psllw $2, %%mm0            \n\t"\
930
        "psllw $2, %%mm1            \n\t"\
931
        "movq   -1(%0), %%mm2       \n\t"\
932
        "movq    2(%0), %%mm4       \n\t"\
933
        "movq %%mm2, %%mm3          \n\t"\
934
        "movq %%mm4, %%mm5          \n\t"\
935
        "punpcklbw %%mm7, %%mm2     \n\t"\
936
        "punpckhbw %%mm7, %%mm3     \n\t"\
937
        "punpcklbw %%mm7, %%mm4     \n\t"\
938
        "punpckhbw %%mm7, %%mm5     \n\t"\
939
        "paddw %%mm4, %%mm2         \n\t"\
940
        "paddw %%mm3, %%mm5         \n\t"\
941
        "psubw %%mm2, %%mm0         \n\t"\
942
        "psubw %%mm5, %%mm1         \n\t"\
943
        "pmullw %%mm6, %%mm0        \n\t"\
944
        "pmullw %%mm6, %%mm1        \n\t"\
945
        "movd   -2(%0), %%mm2       \n\t"\
946
        "movd    7(%0), %%mm5       \n\t"\
947
        "punpcklbw %%mm7, %%mm2     \n\t"\
948
        "punpcklbw %%mm7, %%mm5     \n\t"\
949
        "paddw %%mm3, %%mm2         \n\t"\
950
        "paddw %%mm5, %%mm4         \n\t"\
951
        "movq %5, %%mm5             \n\t"\
952
        "paddw %%mm5, %%mm2         \n\t"\
953
        "paddw %%mm5, %%mm4         \n\t"\
954
        "paddw %%mm2, %%mm0         \n\t"\
955
        "paddw %%mm4, %%mm1         \n\t"\
956
        "psraw $5, %%mm0            \n\t"\
957
        "psraw $5, %%mm1            \n\t"\
958
        "movq (%2), %%mm4           \n\t"\
959
        "packuswb %%mm1, %%mm0      \n\t"\
960
        PAVGB" %%mm4, %%mm0         \n\t"\
961
        OP(%%mm0, (%1),%%mm5, q)\
962
        "add %4, %0                 \n\t"\
963
        "add %4, %1                 \n\t"\
964
        "add %3, %2                 \n\t"\
965
        : "+a"(src), "+c"(dst), "+d"(src2)\
966
        : "D"((long)src2Stride), "S"((long)dstStride),\
967
          "m"(ff_pw_16)\
968
        : "memory"\
969
    );\
970
    }while(--h);\
971
}\
972
\
973
static av_noinline void OPNAME ## h264_qpel8or16_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
974
    int w= 2;\
975
    src -= 2*srcStride;\
976
    \
977
    while(w--){\
978
      asm volatile(\
979
        "pxor %%mm7, %%mm7          \n\t"\
980
        "movd (%0), %%mm0           \n\t"\
981
        "add %2, %0                 \n\t"\
982
        "movd (%0), %%mm1           \n\t"\
983
        "add %2, %0                 \n\t"\
984
        "movd (%0), %%mm2           \n\t"\
985
        "add %2, %0                 \n\t"\
986
        "movd (%0), %%mm3           \n\t"\
987
        "add %2, %0                 \n\t"\
988
        "movd (%0), %%mm4           \n\t"\
989
        "add %2, %0                 \n\t"\
990
        "punpcklbw %%mm7, %%mm0     \n\t"\
991
        "punpcklbw %%mm7, %%mm1     \n\t"\
992
        "punpcklbw %%mm7, %%mm2     \n\t"\
993
        "punpcklbw %%mm7, %%mm3     \n\t"\
994
        "punpcklbw %%mm7, %%mm4     \n\t"\
995
        QPEL_H264V(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4, %%mm5, OP)\
996
        QPEL_H264V(%%mm1, %%mm2, %%mm3, %%mm4, %%mm5, %%mm0, OP)\
997
        QPEL_H264V(%%mm2, %%mm3, %%mm4, %%mm5, %%mm0, %%mm1, OP)\
998
        QPEL_H264V(%%mm3, %%mm4, %%mm5, %%mm0, %%mm1, %%mm2, OP)\
999
        QPEL_H264V(%%mm4, %%mm5, %%mm0, %%mm1, %%mm2, %%mm3, OP)\
1000
        QPEL_H264V(%%mm5, %%mm0, %%mm1, %%mm2, %%mm3, %%mm4, OP)\
1001
        QPEL_H264V(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4, %%mm5, OP)\
1002
        QPEL_H264V(%%mm1, %%mm2, %%mm3, %%mm4, %%mm5, %%mm0, OP)\
1003
         \
1004
        : "+a"(src), "+c"(dst)\
1005
        : "S"((long)srcStride), "D"((long)dstStride), "m"(ff_pw_5), "m"(ff_pw_16)\
1006
        : "memory"\
1007
     );\
1008
     if(h==16){\
1009
        asm volatile(\
1010
            QPEL_H264V(%%mm2, %%mm3, %%mm4, %%mm5, %%mm0, %%mm1, OP)\
1011
            QPEL_H264V(%%mm3, %%mm4, %%mm5, %%mm0, %%mm1, %%mm2, OP)\
1012
            QPEL_H264V(%%mm4, %%mm5, %%mm0, %%mm1, %%mm2, %%mm3, OP)\
1013
            QPEL_H264V(%%mm5, %%mm0, %%mm1, %%mm2, %%mm3, %%mm4, OP)\
1014
            QPEL_H264V(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4, %%mm5, OP)\
1015
            QPEL_H264V(%%mm1, %%mm2, %%mm3, %%mm4, %%mm5, %%mm0, OP)\
1016
            QPEL_H264V(%%mm2, %%mm3, %%mm4, %%mm5, %%mm0, %%mm1, OP)\
1017
            QPEL_H264V(%%mm3, %%mm4, %%mm5, %%mm0, %%mm1, %%mm2, OP)\
1018
            \
1019
           : "+a"(src), "+c"(dst)\
1020
           : "S"((long)srcStride), "D"((long)dstStride), "m"(ff_pw_5), "m"(ff_pw_16)\
1021
           : "memory"\
1022
        );\
1023
     }\
1024
     src += 4-(h+5)*srcStride;\
1025
     dst += 4-h*dstStride;\
1026
   }\
1027
}\
1028
static av_noinline void OPNAME ## h264_qpel8or16_hv_lowpass_ ## MMX(uint8_t *dst, int16_t *tmp, uint8_t *src, int dstStride, int tmpStride, int srcStride, int size){\
1029
    int h = size;\
1030
    int w = (size+8)>>2;\
1031
    src -= 2*srcStride+2;\
1032
    while(w--){\
1033
        asm volatile(\
1034
            "pxor %%mm7, %%mm7      \n\t"\
1035
            "movd (%0), %%mm0       \n\t"\
1036
            "add %2, %0             \n\t"\
1037
            "movd (%0), %%mm1       \n\t"\
1038
            "add %2, %0             \n\t"\
1039
            "movd (%0), %%mm2       \n\t"\
1040
            "add %2, %0             \n\t"\
1041
            "movd (%0), %%mm3       \n\t"\
1042
            "add %2, %0             \n\t"\
1043
            "movd (%0), %%mm4       \n\t"\
1044
            "add %2, %0             \n\t"\
1045
            "punpcklbw %%mm7, %%mm0 \n\t"\
1046
            "punpcklbw %%mm7, %%mm1 \n\t"\
1047
            "punpcklbw %%mm7, %%mm2 \n\t"\
1048
            "punpcklbw %%mm7, %%mm3 \n\t"\
1049
            "punpcklbw %%mm7, %%mm4 \n\t"\
1050
            QPEL_H264HV(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4, %%mm5, 0*48)\
1051
            QPEL_H264HV(%%mm1, %%mm2, %%mm3, %%mm4, %%mm5, %%mm0, 1*48)\
1052
            QPEL_H264HV(%%mm2, %%mm3, %%mm4, %%mm5, %%mm0, %%mm1, 2*48)\
1053
            QPEL_H264HV(%%mm3, %%mm4, %%mm5, %%mm0, %%mm1, %%mm2, 3*48)\
1054
            QPEL_H264HV(%%mm4, %%mm5, %%mm0, %%mm1, %%mm2, %%mm3, 4*48)\
1055
            QPEL_H264HV(%%mm5, %%mm0, %%mm1, %%mm2, %%mm3, %%mm4, 5*48)\
1056
            QPEL_H264HV(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4, %%mm5, 6*48)\
1057
            QPEL_H264HV(%%mm1, %%mm2, %%mm3, %%mm4, %%mm5, %%mm0, 7*48)\
1058
            : "+a"(src)\
1059
            : "c"(tmp), "S"((long)srcStride), "m"(ff_pw_5)\
1060
            : "memory"\
1061
        );\
1062
        if(size==16){\
1063
            asm volatile(\
1064
                QPEL_H264HV(%%mm2, %%mm3, %%mm4, %%mm5, %%mm0, %%mm1,  8*48)\
1065
                QPEL_H264HV(%%mm3, %%mm4, %%mm5, %%mm0, %%mm1, %%mm2,  9*48)\
1066
                QPEL_H264HV(%%mm4, %%mm5, %%mm0, %%mm1, %%mm2, %%mm3, 10*48)\
1067
                QPEL_H264HV(%%mm5, %%mm0, %%mm1, %%mm2, %%mm3, %%mm4, 11*48)\
1068
                QPEL_H264HV(%%mm0, %%mm1, %%mm2, %%mm3, %%mm4, %%mm5, 12*48)\
1069
                QPEL_H264HV(%%mm1, %%mm2, %%mm3, %%mm4, %%mm5, %%mm0, 13*48)\
1070
                QPEL_H264HV(%%mm2, %%mm3, %%mm4, %%mm5, %%mm0, %%mm1, 14*48)\
1071
                QPEL_H264HV(%%mm3, %%mm4, %%mm5, %%mm0, %%mm1, %%mm2, 15*48)\
1072
                : "+a"(src)\
1073
                : "c"(tmp), "S"((long)srcStride), "m"(ff_pw_5)\
1074
                : "memory"\
1075
            );\
1076
        }\
1077
        tmp += 4;\
1078
        src += 4 - (size+5)*srcStride;\
1079
    }\
1080
    tmp -= size+8;\
1081
    w = size>>4;\
1082
    do{\
1083
    h = size;\
1084
    asm volatile(\
1085
        "movq %4, %%mm6             \n\t"\
1086
        "1:                         \n\t"\
1087
        "movq     (%0), %%mm0       \n\t"\
1088
        "movq    8(%0), %%mm3       \n\t"\
1089
        "movq    2(%0), %%mm1       \n\t"\
1090
        "movq   10(%0), %%mm4       \n\t"\
1091
        "paddw   %%mm4, %%mm0       \n\t"\
1092
        "paddw   %%mm3, %%mm1       \n\t"\
1093
        "paddw  18(%0), %%mm3       \n\t"\
1094
        "paddw  16(%0), %%mm4       \n\t"\
1095
        "movq    4(%0), %%mm2       \n\t"\
1096
        "movq   12(%0), %%mm5       \n\t"\
1097
        "paddw   6(%0), %%mm2       \n\t"\
1098
        "paddw  14(%0), %%mm5       \n\t"\
1099
        "psubw %%mm1, %%mm0         \n\t"\
1100
        "psubw %%mm4, %%mm3         \n\t"\
1101
        "psraw $2, %%mm0            \n\t"\
1102
        "psraw $2, %%mm3            \n\t"\
1103
        "psubw %%mm1, %%mm0         \n\t"\
1104
        "psubw %%mm4, %%mm3         \n\t"\
1105
        "paddsw %%mm2, %%mm0        \n\t"\
1106
        "paddsw %%mm5, %%mm3        \n\t"\
1107
        "psraw $2, %%mm0            \n\t"\
1108
        "psraw $2, %%mm3            \n\t"\
1109
        "paddw %%mm6, %%mm2         \n\t"\
1110
        "paddw %%mm6, %%mm5         \n\t"\
1111
        "paddw %%mm2, %%mm0         \n\t"\
1112
        "paddw %%mm5, %%mm3         \n\t"\
1113
        "psraw $6, %%mm0            \n\t"\
1114
        "psraw $6, %%mm3            \n\t"\
1115
        "packuswb %%mm3, %%mm0      \n\t"\
1116
        OP(%%mm0, (%1),%%mm7, q)\
1117
        "add $48, %0                \n\t"\
1118
        "add %3, %1                 \n\t"\
1119
        "decl %2                    \n\t"\
1120
        " jnz 1b                    \n\t"\
1121
        : "+a"(tmp), "+c"(dst), "+m"(h)\
1122
        : "S"((long)dstStride), "m"(ff_pw_32)\
1123
        : "memory"\
1124
    );\
1125
    tmp += 8 - size*24;\
1126
    dst += 8 - size*dstStride;\
1127
    }while(w--);\
1128
}\
1129
\
1130
static void OPNAME ## h264_qpel8_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
1131
    OPNAME ## h264_qpel8or16_v_lowpass_ ## MMX(dst  , src  , dstStride, srcStride, 8);\
1132
}\
1133
static av_noinline void OPNAME ## h264_qpel16_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
1134
    OPNAME ## h264_qpel8or16_v_lowpass_ ## MMX(dst  , src  , dstStride, srcStride, 16);\
1135
    OPNAME ## h264_qpel8or16_v_lowpass_ ## MMX(dst+8, src+8, dstStride, srcStride, 16);\
1136
}\
1137
\
1138
static av_noinline void OPNAME ## h264_qpel16_h_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
1139
    OPNAME ## h264_qpel8_h_lowpass_ ## MMX(dst  , src  , dstStride, srcStride);\
1140
    OPNAME ## h264_qpel8_h_lowpass_ ## MMX(dst+8, src+8, dstStride, srcStride);\
1141
    src += 8*srcStride;\
1142
    dst += 8*dstStride;\
1143
    OPNAME ## h264_qpel8_h_lowpass_ ## MMX(dst  , src  , dstStride, srcStride);\
1144
    OPNAME ## h264_qpel8_h_lowpass_ ## MMX(dst+8, src+8, dstStride, srcStride);\
1145
}\
1146
\
1147
static av_noinline void OPNAME ## h264_qpel16_h_lowpass_l2_ ## MMX(uint8_t *dst, uint8_t *src, uint8_t *src2, int dstStride, int src2Stride){\
1148
    OPNAME ## h264_qpel8_h_lowpass_l2_ ## MMX(dst  , src  , src2  , dstStride, src2Stride);\
1149
    OPNAME ## h264_qpel8_h_lowpass_l2_ ## MMX(dst+8, src+8, src2+8, dstStride, src2Stride);\
1150
    src += 8*dstStride;\
1151
    dst += 8*dstStride;\
1152
    src2 += 8*src2Stride;\
1153
    OPNAME ## h264_qpel8_h_lowpass_l2_ ## MMX(dst  , src  , src2  , dstStride, src2Stride);\
1154
    OPNAME ## h264_qpel8_h_lowpass_l2_ ## MMX(dst+8, src+8, src2+8, dstStride, src2Stride);\
1155
}\
1156
\
1157
static void OPNAME ## h264_qpel8_hv_lowpass_ ## MMX(uint8_t *dst, int16_t *tmp, uint8_t *src, int dstStride, int tmpStride, int srcStride){\
1158
    OPNAME ## h264_qpel8or16_hv_lowpass_ ## MMX(dst  , tmp  , src  , dstStride, tmpStride, srcStride, 8);\
1159
}\
1160
\
1161
static void OPNAME ## h264_qpel16_hv_lowpass_ ## MMX(uint8_t *dst, int16_t *tmp, uint8_t *src, int dstStride, int tmpStride, int srcStride){\
1162
    OPNAME ## h264_qpel8or16_hv_lowpass_ ## MMX(dst  , tmp  , src  , dstStride, tmpStride, srcStride, 16);\
1163
}\
1164
\
1165
static av_noinline void OPNAME ## pixels4_l2_shift5_ ## MMX(uint8_t *dst, int16_t *src16, uint8_t *src8, int dstStride, int src8Stride, int h)\
1166
{\
1167
    asm volatile(\
1168
        "movq       %5,  %%mm6          \n\t"\
1169
        "movq      (%1), %%mm0          \n\t"\
1170
        "movq    24(%1), %%mm1          \n\t"\
1171
        "paddw    %%mm6, %%mm0          \n\t"\
1172
        "paddw    %%mm6, %%mm1          \n\t"\
1173
        "psraw      $5,  %%mm0          \n\t"\
1174
        "psraw      $5,  %%mm1          \n\t"\
1175
        "packuswb %%mm0, %%mm0          \n\t"\
1176
        "packuswb %%mm1, %%mm1          \n\t"\
1177
        PAVGB"     (%0), %%mm0          \n\t"\
1178
        PAVGB"  (%0,%3), %%mm1          \n\t"\
1179
        OP(%%mm0, (%2),    %%mm4, d)\
1180
        OP(%%mm1, (%2,%4), %%mm5, d)\
1181
        "lea  (%0,%3,2), %0             \n\t"\
1182
        "lea  (%2,%4,2), %2             \n\t"\
1183
        "movq    48(%1), %%mm0          \n\t"\
1184
        "movq    72(%1), %%mm1          \n\t"\
1185
        "paddw    %%mm6, %%mm0          \n\t"\
1186
        "paddw    %%mm6, %%mm1          \n\t"\
1187
        "psraw      $5,  %%mm0          \n\t"\
1188
        "psraw      $5,  %%mm1          \n\t"\
1189
        "packuswb %%mm0, %%mm0          \n\t"\
1190
        "packuswb %%mm1, %%mm1          \n\t"\
1191
        PAVGB"     (%0), %%mm0          \n\t"\
1192
        PAVGB"  (%0,%3), %%mm1          \n\t"\
1193
        OP(%%mm0, (%2),    %%mm4, d)\
1194
        OP(%%mm1, (%2,%4), %%mm5, d)\
1195
        :"+a"(src8), "+c"(src16), "+d"(dst)\
1196
        :"S"((long)src8Stride), "D"((long)dstStride), "m"(ff_pw_16)\
1197
        :"memory");\
1198
}\
1199
static av_noinline void OPNAME ## pixels8_l2_shift5_ ## MMX(uint8_t *dst, int16_t *src16, uint8_t *src8, int dstStride, int src8Stride, int h)\
1200
{\
1201
    asm volatile(\
1202
        "movq       %0,  %%mm6          \n\t"\
1203
        ::"m"(ff_pw_16)\
1204
        );\
1205
    while(h--){\
1206
    asm volatile(\
1207
        "movq      (%1), %%mm0          \n\t"\
1208
        "movq     8(%1), %%mm1          \n\t"\
1209
        "paddw    %%mm6, %%mm0          \n\t"\
1210
        "paddw    %%mm6, %%mm1          \n\t"\
1211
        "psraw      $5,  %%mm0          \n\t"\
1212
        "psraw      $5,  %%mm1          \n\t"\
1213
        "packuswb %%mm1, %%mm0          \n\t"\
1214
        PAVGB"     (%0), %%mm0          \n\t"\
1215
        OP(%%mm0, (%2), %%mm5, q)\
1216
        ::"a"(src8), "c"(src16), "d"(dst)\
1217
        :"memory");\
1218
        src8 += src8Stride;\
1219
        src16 += 24;\
1220
        dst += dstStride;\
1221
    }\
1222
}\
1223
static void OPNAME ## pixels16_l2_shift5_ ## MMX(uint8_t *dst, int16_t *src16, uint8_t *src8, int dstStride, int src8Stride, int h)\
1224
{\
1225
    OPNAME ## pixels8_l2_shift5_ ## MMX(dst  , src16  , src8  , dstStride, src8Stride, h);\
1226
    OPNAME ## pixels8_l2_shift5_ ## MMX(dst+8, src16+8, src8+8, dstStride, src8Stride, h);\
1227
}\
1228

    
1229

    
1230
#define H264_MC(OPNAME, SIZE, MMX) \
1231
static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## MMX (uint8_t *dst, uint8_t *src, int stride){\
1232
    OPNAME ## pixels ## SIZE ## _mmx(dst, src, stride, SIZE);\
1233
}\
1234
\
1235
static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1236
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, src, stride, stride);\
1237
}\
1238
\
1239
static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1240
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_ ## MMX(dst, src, stride, stride);\
1241
}\
1242
\
1243
static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1244
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, src+1, stride, stride);\
1245
}\
1246
\
1247
static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1248
    uint64_t temp[SIZE*SIZE/8];\
1249
    uint8_t * const half= (uint8_t*)temp;\
1250
    put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(half, src, SIZE, stride);\
1251
    OPNAME ## pixels ## SIZE ## _l2_ ## MMX(dst, src, half, stride, stride, SIZE);\
1252
}\
1253
\
1254
static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1255
    OPNAME ## h264_qpel ## SIZE ## _v_lowpass_ ## MMX(dst, src, stride, stride);\
1256
}\
1257
\
1258
static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1259
    uint64_t temp[SIZE*SIZE/8];\
1260
    uint8_t * const half= (uint8_t*)temp;\
1261
    put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(half, src, SIZE, stride);\
1262
    OPNAME ## pixels ## SIZE ## _l2_ ## MMX(dst, src+stride, half, stride, stride, SIZE);\
1263
}\
1264
\
1265
static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1266
    uint64_t temp[SIZE*SIZE/8];\
1267
    uint8_t * const halfV= (uint8_t*)temp;\
1268
    put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(halfV, src, SIZE, stride);\
1269
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, halfV, stride, SIZE);\
1270
}\
1271
\
1272
static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1273
    uint64_t temp[SIZE*SIZE/8];\
1274
    uint8_t * const halfV= (uint8_t*)temp;\
1275
    put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(halfV, src+1, SIZE, stride);\
1276
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, halfV, stride, SIZE);\
1277
}\
1278
\
1279
static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1280
    uint64_t temp[SIZE*SIZE/8];\
1281
    uint8_t * const halfV= (uint8_t*)temp;\
1282
    put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(halfV, src, SIZE, stride);\
1283
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src+stride, halfV, stride, SIZE);\
1284
}\
1285
\
1286
static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1287
    uint64_t temp[SIZE*SIZE/8];\
1288
    uint8_t * const halfV= (uint8_t*)temp;\
1289
    put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(halfV, src+1, SIZE, stride);\
1290
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src+stride, halfV, stride, SIZE);\
1291
}\
1292
\
1293
static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1294
    uint64_t temp[SIZE*(SIZE<8?12:24)/4];\
1295
    int16_t * const tmp= (int16_t*)temp;\
1296
    OPNAME ## h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(dst, tmp, src, stride, SIZE, stride);\
1297
}\
1298
\
1299
static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1300
    uint64_t temp[SIZE*(SIZE<8?12:24)/4 + SIZE*SIZE/8];\
1301
    uint8_t * const halfHV= (uint8_t*)temp;\
1302
    int16_t * const tmp= ((int16_t*)temp) + SIZE*SIZE/2;\
1303
    assert(((int)temp & 7) == 0);\
1304
    put_h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(halfHV, tmp, src, SIZE, SIZE, stride);\
1305
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, halfHV, stride, SIZE);\
1306
}\
1307
\
1308
static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1309
    uint64_t temp[SIZE*(SIZE<8?12:24)/4 + SIZE*SIZE/8];\
1310
    uint8_t * const halfHV= (uint8_t*)temp;\
1311
    int16_t * const tmp= ((int16_t*)temp) + SIZE*SIZE/2;\
1312
    assert(((int)temp & 7) == 0);\
1313
    put_h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(halfHV, tmp, src, SIZE, SIZE, stride);\
1314
    OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src+stride, halfHV, stride, SIZE);\
1315
}\
1316
\
1317
static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1318
    uint64_t temp[SIZE*(SIZE<8?12:24)/4 + SIZE*SIZE/8];\
1319
    int16_t * const halfV= ((int16_t*)temp) + SIZE*SIZE/2;\
1320
    uint8_t * const halfHV= ((uint8_t*)temp);\
1321
    assert(((int)temp & 7) == 0);\
1322
    put_h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(halfHV, halfV, src, SIZE, SIZE, stride);\
1323
    OPNAME ## pixels ## SIZE ## _l2_shift5_ ## MMX(dst, halfV+2, halfHV, stride, SIZE, SIZE);\
1324
}\
1325
\
1326
static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
1327
    uint64_t temp[SIZE*(SIZE<8?12:24)/4 + SIZE*SIZE/8];\
1328
    int16_t * const halfV= ((int16_t*)temp) + SIZE*SIZE/2;\
1329
    uint8_t * const halfHV= ((uint8_t*)temp);\
1330
    assert(((int)temp & 7) == 0);\
1331
    put_h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(halfHV, halfV, src, SIZE, SIZE, stride);\
1332
    OPNAME ## pixels ## SIZE ## _l2_shift5_ ## MMX(dst, halfV+3, halfHV, stride, SIZE, SIZE);\
1333
}\
1334

    
1335

    
1336
#define AVG_3DNOW_OP(a,b,temp, size) \
1337
"mov" #size " " #b ", " #temp "   \n\t"\
1338
"pavgusb " #temp ", " #a "        \n\t"\
1339
"mov" #size " " #a ", " #b "      \n\t"
1340
#define AVG_MMX2_OP(a,b,temp, size) \
1341
"mov" #size " " #b ", " #temp "   \n\t"\
1342
"pavgb " #temp ", " #a "          \n\t"\
1343
"mov" #size " " #a ", " #b "      \n\t"
1344

    
1345
#define PAVGB "pavgusb"
1346
QPEL_H264(put_,       PUT_OP, 3dnow)
1347
QPEL_H264(avg_, AVG_3DNOW_OP, 3dnow)
1348
#undef PAVGB
1349
#define PAVGB "pavgb"
1350
QPEL_H264(put_,       PUT_OP, mmx2)
1351
QPEL_H264(avg_,  AVG_MMX2_OP, mmx2)
1352
#undef PAVGB
1353

    
1354
H264_MC(put_, 4, 3dnow)
1355
H264_MC(put_, 8, 3dnow)
1356
H264_MC(put_, 16,3dnow)
1357
H264_MC(avg_, 4, 3dnow)
1358
H264_MC(avg_, 8, 3dnow)
1359
H264_MC(avg_, 16,3dnow)
1360
H264_MC(put_, 4, mmx2)
1361
H264_MC(put_, 8, mmx2)
1362
H264_MC(put_, 16,mmx2)
1363
H264_MC(avg_, 4, mmx2)
1364
H264_MC(avg_, 8, mmx2)
1365
H264_MC(avg_, 16,mmx2)
1366

    
1367

    
1368
#define H264_CHROMA_OP(S,D)
1369
#define H264_CHROMA_OP4(S,D,T)
1370
#define H264_CHROMA_MC8_TMPL put_h264_chroma_mc8_mmx
1371
#define H264_CHROMA_MC4_TMPL put_h264_chroma_mc4_mmx
1372
#define H264_CHROMA_MC2_TMPL put_h264_chroma_mc2_mmx2
1373
#define H264_CHROMA_MC8_MV0 put_pixels8_mmx
1374
#include "dsputil_h264_template_mmx.c"
1375

    
1376
static void put_h264_chroma_mc8_mmx_rnd(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y)
1377
{
1378
    put_h264_chroma_mc8_mmx(dst, src, stride, h, x, y, 1);
1379
}
1380
static void put_h264_chroma_mc8_mmx_nornd(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y)
1381
{
1382
    put_h264_chroma_mc8_mmx(dst, src, stride, h, x, y, 0);
1383
}
1384

    
1385
#undef H264_CHROMA_OP
1386
#undef H264_CHROMA_OP4
1387
#undef H264_CHROMA_MC8_TMPL
1388
#undef H264_CHROMA_MC4_TMPL
1389
#undef H264_CHROMA_MC2_TMPL
1390
#undef H264_CHROMA_MC8_MV0
1391

    
1392
#define H264_CHROMA_OP(S,D) "pavgb " #S ", " #D " \n\t"
1393
#define H264_CHROMA_OP4(S,D,T) "movd  " #S ", " #T " \n\t"\
1394
                               "pavgb " #T ", " #D " \n\t"
1395
#define H264_CHROMA_MC8_TMPL avg_h264_chroma_mc8_mmx2
1396
#define H264_CHROMA_MC4_TMPL avg_h264_chroma_mc4_mmx2
1397
#define H264_CHROMA_MC2_TMPL avg_h264_chroma_mc2_mmx2
1398
#define H264_CHROMA_MC8_MV0 avg_pixels8_mmx2
1399
#include "dsputil_h264_template_mmx.c"
1400
static void avg_h264_chroma_mc8_mmx2_rnd(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y)
1401
{
1402
    avg_h264_chroma_mc8_mmx2(dst, src, stride, h, x, y, 1);
1403
}
1404
#undef H264_CHROMA_OP
1405
#undef H264_CHROMA_OP4
1406
#undef H264_CHROMA_MC8_TMPL
1407
#undef H264_CHROMA_MC4_TMPL
1408
#undef H264_CHROMA_MC2_TMPL
1409
#undef H264_CHROMA_MC8_MV0
1410

    
1411
#define H264_CHROMA_OP(S,D) "pavgusb " #S ", " #D " \n\t"
1412
#define H264_CHROMA_OP4(S,D,T) "movd " #S ", " #T " \n\t"\
1413
                               "pavgusb " #T ", " #D " \n\t"
1414
#define H264_CHROMA_MC8_TMPL avg_h264_chroma_mc8_3dnow
1415
#define H264_CHROMA_MC4_TMPL avg_h264_chroma_mc4_3dnow
1416
#define H264_CHROMA_MC8_MV0 avg_pixels8_3dnow
1417
#include "dsputil_h264_template_mmx.c"
1418
static void avg_h264_chroma_mc8_3dnow_rnd(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y)
1419
{
1420
    avg_h264_chroma_mc8_3dnow(dst, src, stride, h, x, y, 1);
1421
}
1422
#undef H264_CHROMA_OP
1423
#undef H264_CHROMA_OP4
1424
#undef H264_CHROMA_MC8_TMPL
1425
#undef H264_CHROMA_MC4_TMPL
1426
#undef H264_CHROMA_MC8_MV0
1427

    
1428
/***********************************/
1429
/* weighted prediction */
1430

    
1431
static inline void ff_h264_weight_WxH_mmx2(uint8_t *dst, int stride, int log2_denom, int weight, int offset, int w, int h)
1432
{
1433
    int x, y;
1434
    offset <<= log2_denom;
1435
    offset += (1 << log2_denom) >> 1;
1436
    asm volatile(
1437
        "movd    %0, %%mm4        \n\t"
1438
        "movd    %1, %%mm5        \n\t"
1439
        "movd    %2, %%mm6        \n\t"
1440
        "pshufw  $0, %%mm4, %%mm4 \n\t"
1441
        "pshufw  $0, %%mm5, %%mm5 \n\t"
1442
        "pxor    %%mm7, %%mm7     \n\t"
1443
        :: "g"(weight), "g"(offset), "g"(log2_denom)
1444
    );
1445
    for(y=0; y<h; y+=2){
1446
        for(x=0; x<w; x+=4){
1447
            asm volatile(
1448
                "movd      %0,    %%mm0 \n\t"
1449
                "movd      %1,    %%mm1 \n\t"
1450
                "punpcklbw %%mm7, %%mm0 \n\t"
1451
                "punpcklbw %%mm7, %%mm1 \n\t"
1452
                "pmullw    %%mm4, %%mm0 \n\t"
1453
                "pmullw    %%mm4, %%mm1 \n\t"
1454
                "paddsw    %%mm5, %%mm0 \n\t"
1455
                "paddsw    %%mm5, %%mm1 \n\t"
1456
                "psraw     %%mm6, %%mm0 \n\t"
1457
                "psraw     %%mm6, %%mm1 \n\t"
1458
                "packuswb  %%mm7, %%mm0 \n\t"
1459
                "packuswb  %%mm7, %%mm1 \n\t"
1460
                "movd      %%mm0, %0    \n\t"
1461
                "movd      %%mm1, %1    \n\t"
1462
                : "+m"(*(uint32_t*)(dst+x)),
1463
                  "+m"(*(uint32_t*)(dst+x+stride))
1464
            );
1465
        }
1466
        dst += 2*stride;
1467
    }
1468
}
1469

    
1470
static inline void ff_h264_biweight_WxH_mmx2(uint8_t *dst, uint8_t *src, int stride, int log2_denom, int weightd, int weights, int offset, int w, int h)
1471
{
1472
    int x, y;
1473
    offset = ((offset + 1) | 1) << log2_denom;
1474
    asm volatile(
1475
        "movd    %0, %%mm3        \n\t"
1476
        "movd    %1, %%mm4        \n\t"
1477
        "movd    %2, %%mm5        \n\t"
1478
        "movd    %3, %%mm6        \n\t"
1479
        "pshufw  $0, %%mm3, %%mm3 \n\t"
1480
        "pshufw  $0, %%mm4, %%mm4 \n\t"
1481
        "pshufw  $0, %%mm5, %%mm5 \n\t"
1482
        "pxor    %%mm7, %%mm7     \n\t"
1483
        :: "g"(weightd), "g"(weights), "g"(offset), "g"(log2_denom+1)
1484
    );
1485
    for(y=0; y<h; y++){
1486
        for(x=0; x<w; x+=4){
1487
            asm volatile(
1488
                "movd      %0,    %%mm0 \n\t"
1489
                "movd      %1,    %%mm1 \n\t"
1490
                "punpcklbw %%mm7, %%mm0 \n\t"
1491
                "punpcklbw %%mm7, %%mm1 \n\t"
1492
                "pmullw    %%mm3, %%mm0 \n\t"
1493
                "pmullw    %%mm4, %%mm1 \n\t"
1494
                "paddsw    %%mm1, %%mm0 \n\t"
1495
                "paddsw    %%mm5, %%mm0 \n\t"
1496
                "psraw     %%mm6, %%mm0 \n\t"
1497
                "packuswb  %%mm0, %%mm0 \n\t"
1498
                "movd      %%mm0, %0    \n\t"
1499
                : "+m"(*(uint32_t*)(dst+x))
1500
                :  "m"(*(uint32_t*)(src+x))
1501
            );
1502
        }
1503
        src += stride;
1504
        dst += stride;
1505
    }
1506
}
1507

    
1508
#define H264_WEIGHT(W,H) \
1509
static void ff_h264_biweight_ ## W ## x ## H ## _mmx2(uint8_t *dst, uint8_t *src, int stride, int log2_denom, int weightd, int weights, int offset){ \
1510
    ff_h264_biweight_WxH_mmx2(dst, src, stride, log2_denom, weightd, weights, offset, W, H); \
1511
} \
1512
static void ff_h264_weight_ ## W ## x ## H ## _mmx2(uint8_t *dst, int stride, int log2_denom, int weight, int offset){ \
1513
    ff_h264_weight_WxH_mmx2(dst, stride, log2_denom, weight, offset, W, H); \
1514
}
1515

    
1516
H264_WEIGHT(16,16)
1517
H264_WEIGHT(16, 8)
1518
H264_WEIGHT( 8,16)
1519
H264_WEIGHT( 8, 8)
1520
H264_WEIGHT( 8, 4)
1521
H264_WEIGHT( 4, 8)
1522
H264_WEIGHT( 4, 4)
1523
H264_WEIGHT( 4, 2)
1524