motion_est_mmx.c 14.1 KB
Newer Older
1 2
/*
 * MMX optimized motion estimation
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 * Copyright (c) 2002-2004 Michael Niedermayer
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * 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 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * 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
 * mostly by Michael Niedermayer <michaelni@gmx.at>
23 24
 */
#include "../dsputil.h"
25
#include "x86_cpu.h"
26

27
static const __attribute__ ((aligned(8))) uint64_t round_tab[3]={
28 29 30
0x0000000000000000ULL,
0x0001000100010001ULL,
0x0002000200020002ULL,
31
};
32

33
static attribute_used __attribute__ ((aligned(8))) uint64_t bone= 0x0101010101010101LL;
Michael Niedermayer's avatar
Michael Niedermayer committed
34

35
static inline void sad8_1_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
36
{
37
    long len= -(stride*h);
38
    asm volatile(
39
        ASMALIGN(4)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        "1:                             \n\t"
        "movq (%1, %%"REG_a"), %%mm0    \n\t"
        "movq (%2, %%"REG_a"), %%mm2    \n\t"
        "movq (%2, %%"REG_a"), %%mm4    \n\t"
        "add %3, %%"REG_a"              \n\t"
        "psubusb %%mm0, %%mm2           \n\t"
        "psubusb %%mm4, %%mm0           \n\t"
        "movq (%1, %%"REG_a"), %%mm1    \n\t"
        "movq (%2, %%"REG_a"), %%mm3    \n\t"
        "movq (%2, %%"REG_a"), %%mm5    \n\t"
        "psubusb %%mm1, %%mm3           \n\t"
        "psubusb %%mm5, %%mm1           \n\t"
        "por %%mm2, %%mm0               \n\t"
        "por %%mm1, %%mm3               \n\t"
        "movq %%mm0, %%mm1              \n\t"
        "movq %%mm3, %%mm2              \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpckhbw %%mm7, %%mm1         \n\t"
        "punpcklbw %%mm7, %%mm3         \n\t"
        "punpckhbw %%mm7, %%mm2         \n\t"
        "paddw %%mm1, %%mm0             \n\t"
        "paddw %%mm3, %%mm2             \n\t"
        "paddw %%mm2, %%mm0             \n\t"
        "paddw %%mm0, %%mm6             \n\t"
        "add %3, %%"REG_a"              \n\t"
        " js 1b                         \n\t"
66
        : "+a" (len)
67
        : "r" (blk1 - len), "r" (blk2 - len), "r" ((long)stride)
68
    );
69 70
}

71
static inline void sad8_1_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
72
{
73
    long len= -(stride*h);
74
    asm volatile(
75
        ASMALIGN(4)
76 77 78 79 80 81 82 83 84 85 86 87
        "1:                             \n\t"
        "movq (%1, %%"REG_a"), %%mm0    \n\t"
        "movq (%2, %%"REG_a"), %%mm2    \n\t"
        "psadbw %%mm2, %%mm0            \n\t"
        "add %3, %%"REG_a"              \n\t"
        "movq (%1, %%"REG_a"), %%mm1    \n\t"
        "movq (%2, %%"REG_a"), %%mm3    \n\t"
        "psadbw %%mm1, %%mm3            \n\t"
        "paddw %%mm3, %%mm0             \n\t"
        "paddw %%mm0, %%mm6             \n\t"
        "add %3, %%"REG_a"              \n\t"
        " js 1b                         \n\t"
88
        : "+a" (len)
89
        : "r" (blk1 - len), "r" (blk2 - len), "r" ((long)stride)
90
    );
91 92
}

93
static inline void sad8_2_mmx2(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h)
94
{
95
    long len= -(stride*h);
96
    asm volatile(
97
        ASMALIGN(4)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        "1:                             \n\t"
        "movq (%1, %%"REG_a"), %%mm0    \n\t"
        "movq (%2, %%"REG_a"), %%mm2    \n\t"
        "pavgb %%mm2, %%mm0             \n\t"
        "movq (%3, %%"REG_a"), %%mm2    \n\t"
        "psadbw %%mm2, %%mm0            \n\t"
        "add %4, %%"REG_a"              \n\t"
        "movq (%1, %%"REG_a"), %%mm1    \n\t"
        "movq (%2, %%"REG_a"), %%mm3    \n\t"
        "pavgb %%mm1, %%mm3             \n\t"
        "movq (%3, %%"REG_a"), %%mm1    \n\t"
        "psadbw %%mm1, %%mm3            \n\t"
        "paddw %%mm3, %%mm0             \n\t"
        "paddw %%mm0, %%mm6             \n\t"
        "add %4, %%"REG_a"              \n\t"
        " js 1b                         \n\t"
114
        : "+a" (len)
115
        : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" ((long)stride)
116
    );
117 118
}

119
static inline void sad8_4_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
120
{ //FIXME reuse src
121
    long len= -(stride*h);
122
    asm volatile(
123
        ASMALIGN(4)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        "movq "MANGLE(bone)", %%mm5     \n\t"
        "1:                             \n\t"
        "movq (%1, %%"REG_a"), %%mm0    \n\t"
        "movq (%2, %%"REG_a"), %%mm2    \n\t"
        "movq 1(%1, %%"REG_a"), %%mm1   \n\t"
        "movq 1(%2, %%"REG_a"), %%mm3   \n\t"
        "pavgb %%mm2, %%mm0             \n\t"
        "pavgb %%mm1, %%mm3             \n\t"
        "psubusb %%mm5, %%mm3           \n\t"
        "pavgb %%mm3, %%mm0             \n\t"
        "movq (%3, %%"REG_a"), %%mm2    \n\t"
        "psadbw %%mm2, %%mm0            \n\t"
        "add %4, %%"REG_a"              \n\t"
        "movq (%1, %%"REG_a"), %%mm1    \n\t"
        "movq (%2, %%"REG_a"), %%mm3    \n\t"
        "movq 1(%1, %%"REG_a"), %%mm2   \n\t"
        "movq 1(%2, %%"REG_a"), %%mm4   \n\t"
        "pavgb %%mm3, %%mm1             \n\t"
        "pavgb %%mm4, %%mm2             \n\t"
        "psubusb %%mm5, %%mm2           \n\t"
        "pavgb %%mm1, %%mm2             \n\t"
        "movq (%3, %%"REG_a"), %%mm1    \n\t"
        "psadbw %%mm1, %%mm2            \n\t"
        "paddw %%mm2, %%mm0             \n\t"
        "paddw %%mm0, %%mm6             \n\t"
        "add %4, %%"REG_a"              \n\t"
        " js 1b                         \n\t"
151
        : "+a" (len)
152
        : "r" (blk1 - len), "r" (blk1 - len + stride), "r" (blk2 - len), "r" ((long)stride)
153
    );
154 155
}

156
static inline void sad8_2_mmx(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h)
157
{
158
    long len= -(stride*h);
159
    asm volatile(
160
        ASMALIGN(4)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        "1:                             \n\t"
        "movq (%1, %%"REG_a"), %%mm0    \n\t"
        "movq (%2, %%"REG_a"), %%mm1    \n\t"
        "movq (%1, %%"REG_a"), %%mm2    \n\t"
        "movq (%2, %%"REG_a"), %%mm3    \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpcklbw %%mm7, %%mm1         \n\t"
        "punpckhbw %%mm7, %%mm2         \n\t"
        "punpckhbw %%mm7, %%mm3         \n\t"
        "paddw %%mm0, %%mm1             \n\t"
        "paddw %%mm2, %%mm3             \n\t"
        "movq (%3, %%"REG_a"), %%mm4    \n\t"
        "movq (%3, %%"REG_a"), %%mm2    \n\t"
        "paddw %%mm5, %%mm1             \n\t"
        "paddw %%mm5, %%mm3             \n\t"
        "psrlw $1, %%mm1                \n\t"
        "psrlw $1, %%mm3                \n\t"
        "packuswb %%mm3, %%mm1          \n\t"
        "psubusb %%mm1, %%mm4           \n\t"
        "psubusb %%mm2, %%mm1           \n\t"
        "por %%mm4, %%mm1               \n\t"
        "movq %%mm1, %%mm0              \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpckhbw %%mm7, %%mm1         \n\t"
        "paddw %%mm1, %%mm0             \n\t"
        "paddw %%mm0, %%mm6             \n\t"
        "add %4, %%"REG_a"              \n\t"
        " js 1b                         \n\t"
189
        : "+a" (len)
190
        : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" ((long)stride)
191
    );
192 193
}

194
static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
195
{
196
    long len= -(stride*h);
197
    asm volatile(
198
        ASMALIGN(4)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        "1:                             \n\t"
        "movq (%1, %%"REG_a"), %%mm0    \n\t"
        "movq (%2, %%"REG_a"), %%mm1    \n\t"
        "movq %%mm0, %%mm4              \n\t"
        "movq %%mm1, %%mm2              \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpcklbw %%mm7, %%mm1         \n\t"
        "punpckhbw %%mm7, %%mm4         \n\t"
        "punpckhbw %%mm7, %%mm2         \n\t"
        "paddw %%mm1, %%mm0             \n\t"
        "paddw %%mm2, %%mm4             \n\t"
        "movq 1(%1, %%"REG_a"), %%mm2   \n\t"
        "movq 1(%2, %%"REG_a"), %%mm3   \n\t"
        "movq %%mm2, %%mm1              \n\t"
        "punpcklbw %%mm7, %%mm2         \n\t"
        "punpckhbw %%mm7, %%mm1         \n\t"
        "paddw %%mm0, %%mm2             \n\t"
        "paddw %%mm4, %%mm1             \n\t"
        "movq %%mm3, %%mm4              \n\t"
        "punpcklbw %%mm7, %%mm3         \n\t"
        "punpckhbw %%mm7, %%mm4         \n\t"
        "paddw %%mm3, %%mm2             \n\t"
        "paddw %%mm4, %%mm1             \n\t"
        "movq (%3, %%"REG_a"), %%mm3    \n\t"
        "movq (%3, %%"REG_a"), %%mm4    \n\t"
        "paddw %%mm5, %%mm2             \n\t"
        "paddw %%mm5, %%mm1             \n\t"
        "psrlw $2, %%mm2                \n\t"
        "psrlw $2, %%mm1                \n\t"
        "packuswb %%mm1, %%mm2          \n\t"
        "psubusb %%mm2, %%mm3           \n\t"
        "psubusb %%mm4, %%mm2           \n\t"
        "por %%mm3, %%mm2               \n\t"
        "movq %%mm2, %%mm0              \n\t"
        "punpcklbw %%mm7, %%mm0         \n\t"
        "punpckhbw %%mm7, %%mm2         \n\t"
        "paddw %%mm2, %%mm0             \n\t"
        "paddw %%mm0, %%mm6             \n\t"
        "add %4, %%"REG_a"              \n\t"
        " js 1b                         \n\t"
239
        : "+a" (len)
240
        : "r" (blk1 - len), "r" (blk1 -len + stride), "r" (blk2 - len), "r" ((long)stride)
241
    );
242 243
}

244
static inline int sum_mmx(void)
245
{
246 247
    int ret;
    asm volatile(
248 249 250 251 252 253 254
        "movq %%mm6, %%mm0              \n\t"
        "psrlq $32, %%mm6               \n\t"
        "paddw %%mm0, %%mm6             \n\t"
        "movq %%mm6, %%mm0              \n\t"
        "psrlq $16, %%mm6               \n\t"
        "paddw %%mm0, %%mm6             \n\t"
        "movd %%mm6, %0                 \n\t"
255 256 257
        : "=r" (ret)
    );
    return ret&0xFFFF;
258 259
}

260
static inline int sum_mmx2(void)
261
{
262 263
    int ret;
    asm volatile(
264
        "movd %%mm6, %0                 \n\t"
265 266 267
        : "=r" (ret)
    );
    return ret;
268 269
}

270

271
#define PIX_SAD(suf)\
272
static int sad8_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
273
{\
274
    assert(h==8);\
275 276
    asm volatile("pxor %%mm7, %%mm7     \n\t"\
                 "pxor %%mm6, %%mm6     \n\t":);\
277
\
278
    sad8_1_ ## suf(blk1, blk2, stride, 8);\
279 280 281
\
    return sum_ ## suf();\
}\
282
static int sad8_x2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
283
{\
284
    assert(h==8);\
285 286 287
    asm volatile("pxor %%mm7, %%mm7     \n\t"\
                 "pxor %%mm6, %%mm6     \n\t"\
                 "movq %0, %%mm5        \n\t"\
288 289 290
                 :: "m"(round_tab[1]) \
                 );\
\
291
    sad8_2_ ## suf(blk1, blk1+1, blk2, stride, 8);\
292 293 294 295
\
    return sum_ ## suf();\
}\
\
296
static int sad8_y2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
297
{\
298
    assert(h==8);\
299 300 301
    asm volatile("pxor %%mm7, %%mm7     \n\t"\
                 "pxor %%mm6, %%mm6     \n\t"\
                 "movq %0, %%mm5        \n\t"\
302 303 304
                 :: "m"(round_tab[1]) \
                 );\
\
305
    sad8_2_ ## suf(blk1, blk1+stride, blk2, stride, 8);\
306 307 308 309
\
    return sum_ ## suf();\
}\
\
310
static int sad8_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
311
{\
312
    assert(h==8);\
313 314 315
    asm volatile("pxor %%mm7, %%mm7     \n\t"\
                 "pxor %%mm6, %%mm6     \n\t"\
                 "movq %0, %%mm5        \n\t"\
316 317 318
                 :: "m"(round_tab[2]) \
                 );\
\
319
    sad8_4_ ## suf(blk1, blk2, stride, 8);\
320 321 322 323
\
    return sum_ ## suf();\
}\
\
324
static int sad16_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
Michael Niedermayer's avatar
Michael Niedermayer committed
325
{\
326 327
    asm volatile("pxor %%mm7, %%mm7     \n\t"\
                 "pxor %%mm6, %%mm6     \n\t":);\
Michael Niedermayer's avatar
Michael Niedermayer committed
328
\
329 330
    sad8_1_ ## suf(blk1  , blk2  , stride, h);\
    sad8_1_ ## suf(blk1+8, blk2+8, stride, h);\
Michael Niedermayer's avatar
Michael Niedermayer committed
331 332 333
\
    return sum_ ## suf();\
}\
334
static int sad16_x2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
335
{\
336 337 338
    asm volatile("pxor %%mm7, %%mm7     \n\t"\
                 "pxor %%mm6, %%mm6     \n\t"\
                 "movq %0, %%mm5        \n\t"\
339 340 341
                 :: "m"(round_tab[1]) \
                 );\
\
342 343
    sad8_2_ ## suf(blk1  , blk1+1, blk2  , stride, h);\
    sad8_2_ ## suf(blk1+8, blk1+9, blk2+8, stride, h);\
344 345 346
\
    return sum_ ## suf();\
}\
347
static int sad16_y2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
348
{\
349 350 351
    asm volatile("pxor %%mm7, %%mm7     \n\t"\
                 "pxor %%mm6, %%mm6     \n\t"\
                 "movq %0, %%mm5        \n\t"\
352 353 354
                 :: "m"(round_tab[1]) \
                 );\
\
355 356
    sad8_2_ ## suf(blk1  , blk1+stride,  blk2  , stride, h);\
    sad8_2_ ## suf(blk1+8, blk1+stride+8,blk2+8, stride, h);\
357 358 359
\
    return sum_ ## suf();\
}\
360
static int sad16_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
361
{\
362 363 364
    asm volatile("pxor %%mm7, %%mm7     \n\t"\
                 "pxor %%mm6, %%mm6     \n\t"\
                 "movq %0, %%mm5        \n\t"\
365 366 367
                 :: "m"(round_tab[2]) \
                 );\
\
368 369
    sad8_4_ ## suf(blk1  , blk2  , stride, h);\
    sad8_4_ ## suf(blk1+8, blk2+8, stride, h);\
370 371 372
\
    return sum_ ## suf();\
}\
373

374 375
PIX_SAD(mmx)
PIX_SAD(mmx2)
376

377
void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx)
378
{
Måns Rullgård's avatar
Måns Rullgård committed
379
    if (mm_flags & MM_MMX) {
380 381 382 383 384 385 386 387
        c->pix_abs[0][0] = sad16_mmx;
        c->pix_abs[0][1] = sad16_x2_mmx;
        c->pix_abs[0][2] = sad16_y2_mmx;
        c->pix_abs[0][3] = sad16_xy2_mmx;
        c->pix_abs[1][0] = sad8_mmx;
        c->pix_abs[1][1] = sad8_x2_mmx;
        c->pix_abs[1][2] = sad8_y2_mmx;
        c->pix_abs[1][3] = sad8_xy2_mmx;
388

389
        c->sad[0]= sad16_mmx;
390
        c->sad[1]= sad8_mmx;
391
    }
Måns Rullgård's avatar
Måns Rullgård committed
392
    if (mm_flags & MM_MMXEXT) {
393 394
        c->pix_abs[0][0] = sad16_mmx2;
        c->pix_abs[1][0] = sad8_mmx2;
395

396 397
        c->sad[0]= sad16_mmx2;
        c->sad[1]= sad8_mmx2;
398

399
        if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
400 401 402 403 404 405
            c->pix_abs[0][1] = sad16_x2_mmx2;
            c->pix_abs[0][2] = sad16_y2_mmx2;
            c->pix_abs[0][3] = sad16_xy2_mmx2;
            c->pix_abs[1][1] = sad8_x2_mmx2;
            c->pix_abs[1][2] = sad8_y2_mmx2;
            c->pix_abs[1][3] = sad8_xy2_mmx2;
406
        }
407 408
    }
}