• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/x86/motion_est_mmx.c

Go to the documentation of this file.
00001 /*
00002  * MMX optimized motion estimation
00003  * Copyright (c) 2001 Fabrice Bellard
00004  * Copyright (c) 2002-2004 Michael Niedermayer
00005  *
00006  * mostly by Michael Niedermayer <michaelni@gmx.at>
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00025 #include "libavutil/x86_cpu.h"
00026 #include "libavcodec/dsputil.h"
00027 
00028 DECLARE_ASM_CONST(8, uint64_t, round_tab[3])={
00029 0x0000000000000000ULL,
00030 0x0001000100010001ULL,
00031 0x0002000200020002ULL,
00032 };
00033 
00034 DECLARE_ASM_CONST(8, uint64_t, bone)= 0x0101010101010101LL;
00035 
00036 static inline void sad8_1_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
00037 {
00038     x86_reg len= -(stride*h);
00039     __asm__ volatile(
00040         ASMALIGN(4)
00041         "1:                             \n\t"
00042         "movq (%1, %%"REG_a"), %%mm0    \n\t"
00043         "movq (%2, %%"REG_a"), %%mm2    \n\t"
00044         "movq (%2, %%"REG_a"), %%mm4    \n\t"
00045         "add %3, %%"REG_a"              \n\t"
00046         "psubusb %%mm0, %%mm2           \n\t"
00047         "psubusb %%mm4, %%mm0           \n\t"
00048         "movq (%1, %%"REG_a"), %%mm1    \n\t"
00049         "movq (%2, %%"REG_a"), %%mm3    \n\t"
00050         "movq (%2, %%"REG_a"), %%mm5    \n\t"
00051         "psubusb %%mm1, %%mm3           \n\t"
00052         "psubusb %%mm5, %%mm1           \n\t"
00053         "por %%mm2, %%mm0               \n\t"
00054         "por %%mm1, %%mm3               \n\t"
00055         "movq %%mm0, %%mm1              \n\t"
00056         "movq %%mm3, %%mm2              \n\t"
00057         "punpcklbw %%mm7, %%mm0         \n\t"
00058         "punpckhbw %%mm7, %%mm1         \n\t"
00059         "punpcklbw %%mm7, %%mm3         \n\t"
00060         "punpckhbw %%mm7, %%mm2         \n\t"
00061         "paddw %%mm1, %%mm0             \n\t"
00062         "paddw %%mm3, %%mm2             \n\t"
00063         "paddw %%mm2, %%mm0             \n\t"
00064         "paddw %%mm0, %%mm6             \n\t"
00065         "add %3, %%"REG_a"              \n\t"
00066         " js 1b                         \n\t"
00067         : "+a" (len)
00068         : "r" (blk1 - len), "r" (blk2 - len), "r" ((x86_reg)stride)
00069     );
00070 }
00071 
00072 static inline void sad8_1_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
00073 {
00074     __asm__ volatile(
00075         ASMALIGN(4)
00076         "1:                             \n\t"
00077         "movq (%1), %%mm0               \n\t"
00078         "movq (%1, %3), %%mm1           \n\t"
00079         "psadbw (%2), %%mm0             \n\t"
00080         "psadbw (%2, %3), %%mm1         \n\t"
00081         "paddw %%mm0, %%mm6             \n\t"
00082         "paddw %%mm1, %%mm6             \n\t"
00083         "lea (%1,%3,2), %1              \n\t"
00084         "lea (%2,%3,2), %2              \n\t"
00085         "sub $2, %0                     \n\t"
00086         " jg 1b                         \n\t"
00087         : "+r" (h), "+r" (blk1), "+r" (blk2)
00088         : "r" ((x86_reg)stride)
00089     );
00090 }
00091 
00092 static int sad16_sse2(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)
00093 {
00094     int ret;
00095     __asm__ volatile(
00096         "pxor %%xmm6, %%xmm6            \n\t"
00097         ASMALIGN(4)
00098         "1:                             \n\t"
00099         "movdqu (%1), %%xmm0            \n\t"
00100         "movdqu (%1, %3), %%xmm1        \n\t"
00101         "psadbw (%2), %%xmm0            \n\t"
00102         "psadbw (%2, %3), %%xmm1        \n\t"
00103         "paddw %%xmm0, %%xmm6           \n\t"
00104         "paddw %%xmm1, %%xmm6           \n\t"
00105         "lea (%1,%3,2), %1              \n\t"
00106         "lea (%2,%3,2), %2              \n\t"
00107         "sub $2, %0                     \n\t"
00108         " jg 1b                         \n\t"
00109         : "+r" (h), "+r" (blk1), "+r" (blk2)
00110         : "r" ((x86_reg)stride)
00111     );
00112     __asm__ volatile(
00113         "movhlps %%xmm6, %%xmm0         \n\t"
00114         "paddw   %%xmm0, %%xmm6         \n\t"
00115         "movd    %%xmm6, %0             \n\t"
00116         : "=r"(ret)
00117     );
00118     return ret;
00119 }
00120 
00121 static inline void sad8_x2a_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
00122 {
00123     __asm__ volatile(
00124         ASMALIGN(4)
00125         "1:                             \n\t"
00126         "movq (%1), %%mm0               \n\t"
00127         "movq (%1, %3), %%mm1           \n\t"
00128         "pavgb 1(%1), %%mm0             \n\t"
00129         "pavgb 1(%1, %3), %%mm1         \n\t"
00130         "psadbw (%2), %%mm0             \n\t"
00131         "psadbw (%2, %3), %%mm1         \n\t"
00132         "paddw %%mm0, %%mm6             \n\t"
00133         "paddw %%mm1, %%mm6             \n\t"
00134         "lea (%1,%3,2), %1              \n\t"
00135         "lea (%2,%3,2), %2              \n\t"
00136         "sub $2, %0                     \n\t"
00137         " jg 1b                         \n\t"
00138         : "+r" (h), "+r" (blk1), "+r" (blk2)
00139         : "r" ((x86_reg)stride)
00140     );
00141 }
00142 
00143 static inline void sad8_y2a_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
00144 {
00145     __asm__ volatile(
00146         "movq (%1), %%mm0               \n\t"
00147         "add %3, %1                     \n\t"
00148         ASMALIGN(4)
00149         "1:                             \n\t"
00150         "movq (%1), %%mm1               \n\t"
00151         "movq (%1, %3), %%mm2           \n\t"
00152         "pavgb %%mm1, %%mm0             \n\t"
00153         "pavgb %%mm2, %%mm1             \n\t"
00154         "psadbw (%2), %%mm0             \n\t"
00155         "psadbw (%2, %3), %%mm1         \n\t"
00156         "paddw %%mm0, %%mm6             \n\t"
00157         "paddw %%mm1, %%mm6             \n\t"
00158         "movq %%mm2, %%mm0              \n\t"
00159         "lea (%1,%3,2), %1              \n\t"
00160         "lea (%2,%3,2), %2              \n\t"
00161         "sub $2, %0                     \n\t"
00162         " jg 1b                         \n\t"
00163         : "+r" (h), "+r" (blk1), "+r" (blk2)
00164         : "r" ((x86_reg)stride)
00165     );
00166 }
00167 
00168 static inline void sad8_4_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
00169 {
00170     __asm__ volatile(
00171         "movq "MANGLE(bone)", %%mm5     \n\t"
00172         "movq (%1), %%mm0               \n\t"
00173         "pavgb 1(%1), %%mm0             \n\t"
00174         "add %3, %1                     \n\t"
00175         ASMALIGN(4)
00176         "1:                             \n\t"
00177         "movq (%1), %%mm1               \n\t"
00178         "movq (%1,%3), %%mm2            \n\t"
00179         "pavgb 1(%1), %%mm1             \n\t"
00180         "pavgb 1(%1,%3), %%mm2          \n\t"
00181         "psubusb %%mm5, %%mm1           \n\t"
00182         "pavgb %%mm1, %%mm0             \n\t"
00183         "pavgb %%mm2, %%mm1             \n\t"
00184         "psadbw (%2), %%mm0             \n\t"
00185         "psadbw (%2,%3), %%mm1          \n\t"
00186         "paddw %%mm0, %%mm6             \n\t"
00187         "paddw %%mm1, %%mm6             \n\t"
00188         "movq %%mm2, %%mm0              \n\t"
00189         "lea (%1,%3,2), %1              \n\t"
00190         "lea (%2,%3,2), %2              \n\t"
00191         "sub $2, %0                     \n\t"
00192         " jg 1b                         \n\t"
00193         : "+r" (h), "+r" (blk1), "+r" (blk2)
00194         : "r" ((x86_reg)stride)
00195     );
00196 }
00197 
00198 static inline void sad8_2_mmx(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h)
00199 {
00200     x86_reg len= -(stride*h);
00201     __asm__ volatile(
00202         ASMALIGN(4)
00203         "1:                             \n\t"
00204         "movq (%1, %%"REG_a"), %%mm0    \n\t"
00205         "movq (%2, %%"REG_a"), %%mm1    \n\t"
00206         "movq (%1, %%"REG_a"), %%mm2    \n\t"
00207         "movq (%2, %%"REG_a"), %%mm3    \n\t"
00208         "punpcklbw %%mm7, %%mm0         \n\t"
00209         "punpcklbw %%mm7, %%mm1         \n\t"
00210         "punpckhbw %%mm7, %%mm2         \n\t"
00211         "punpckhbw %%mm7, %%mm3         \n\t"
00212         "paddw %%mm0, %%mm1             \n\t"
00213         "paddw %%mm2, %%mm3             \n\t"
00214         "movq (%3, %%"REG_a"), %%mm4    \n\t"
00215         "movq (%3, %%"REG_a"), %%mm2    \n\t"
00216         "paddw %%mm5, %%mm1             \n\t"
00217         "paddw %%mm5, %%mm3             \n\t"
00218         "psrlw $1, %%mm1                \n\t"
00219         "psrlw $1, %%mm3                \n\t"
00220         "packuswb %%mm3, %%mm1          \n\t"
00221         "psubusb %%mm1, %%mm4           \n\t"
00222         "psubusb %%mm2, %%mm1           \n\t"
00223         "por %%mm4, %%mm1               \n\t"
00224         "movq %%mm1, %%mm0              \n\t"
00225         "punpcklbw %%mm7, %%mm0         \n\t"
00226         "punpckhbw %%mm7, %%mm1         \n\t"
00227         "paddw %%mm1, %%mm0             \n\t"
00228         "paddw %%mm0, %%mm6             \n\t"
00229         "add %4, %%"REG_a"              \n\t"
00230         " js 1b                         \n\t"
00231         : "+a" (len)
00232         : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" ((x86_reg)stride)
00233     );
00234 }
00235 
00236 static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
00237 {
00238     x86_reg len= -(stride*h);
00239     __asm__ volatile(
00240         "movq (%1, %%"REG_a"), %%mm0    \n\t"
00241         "movq 1(%1, %%"REG_a"), %%mm2   \n\t"
00242         "movq %%mm0, %%mm1              \n\t"
00243         "movq %%mm2, %%mm3              \n\t"
00244         "punpcklbw %%mm7, %%mm0         \n\t"
00245         "punpckhbw %%mm7, %%mm1         \n\t"
00246         "punpcklbw %%mm7, %%mm2         \n\t"
00247         "punpckhbw %%mm7, %%mm3         \n\t"
00248         "paddw %%mm2, %%mm0             \n\t"
00249         "paddw %%mm3, %%mm1             \n\t"
00250         ASMALIGN(4)
00251         "1:                             \n\t"
00252         "movq (%2, %%"REG_a"), %%mm2    \n\t"
00253         "movq 1(%2, %%"REG_a"), %%mm4   \n\t"
00254         "movq %%mm2, %%mm3              \n\t"
00255         "movq %%mm4, %%mm5              \n\t"
00256         "punpcklbw %%mm7, %%mm2         \n\t"
00257         "punpckhbw %%mm7, %%mm3         \n\t"
00258         "punpcklbw %%mm7, %%mm4         \n\t"
00259         "punpckhbw %%mm7, %%mm5         \n\t"
00260         "paddw %%mm4, %%mm2             \n\t"
00261         "paddw %%mm5, %%mm3             \n\t"
00262         "movq 16+"MANGLE(round_tab)", %%mm5 \n\t"
00263         "paddw %%mm2, %%mm0             \n\t"
00264         "paddw %%mm3, %%mm1             \n\t"
00265         "paddw %%mm5, %%mm0             \n\t"
00266         "paddw %%mm5, %%mm1             \n\t"
00267         "movq (%3, %%"REG_a"), %%mm4    \n\t"
00268         "movq (%3, %%"REG_a"), %%mm5    \n\t"
00269         "psrlw $2, %%mm0                \n\t"
00270         "psrlw $2, %%mm1                \n\t"
00271         "packuswb %%mm1, %%mm0          \n\t"
00272         "psubusb %%mm0, %%mm4           \n\t"
00273         "psubusb %%mm5, %%mm0           \n\t"
00274         "por %%mm4, %%mm0               \n\t"
00275         "movq %%mm0, %%mm4              \n\t"
00276         "punpcklbw %%mm7, %%mm0         \n\t"
00277         "punpckhbw %%mm7, %%mm4         \n\t"
00278         "paddw %%mm0, %%mm6             \n\t"
00279         "paddw %%mm4, %%mm6             \n\t"
00280         "movq  %%mm2, %%mm0             \n\t"
00281         "movq  %%mm3, %%mm1             \n\t"
00282         "add %4, %%"REG_a"              \n\t"
00283         " js 1b                         \n\t"
00284         : "+a" (len)
00285         : "r" (blk1 - len), "r" (blk1 -len + stride), "r" (blk2 - len), "r" ((x86_reg)stride)
00286     );
00287 }
00288 
00289 static inline int sum_mmx(void)
00290 {
00291     int ret;
00292     __asm__ volatile(
00293         "movq %%mm6, %%mm0              \n\t"
00294         "psrlq $32, %%mm6               \n\t"
00295         "paddw %%mm0, %%mm6             \n\t"
00296         "movq %%mm6, %%mm0              \n\t"
00297         "psrlq $16, %%mm6               \n\t"
00298         "paddw %%mm0, %%mm6             \n\t"
00299         "movd %%mm6, %0                 \n\t"
00300         : "=r" (ret)
00301     );
00302     return ret&0xFFFF;
00303 }
00304 
00305 static inline int sum_mmx2(void)
00306 {
00307     int ret;
00308     __asm__ volatile(
00309         "movd %%mm6, %0                 \n\t"
00310         : "=r" (ret)
00311     );
00312     return ret;
00313 }
00314 
00315 static inline void sad8_x2a_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
00316 {
00317     sad8_2_mmx(blk1, blk1+1, blk2, stride, h);
00318 }
00319 static inline void sad8_y2a_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
00320 {
00321     sad8_2_mmx(blk1, blk1+stride, blk2, stride, h);
00322 }
00323 
00324 
00325 #define PIX_SAD(suf)\
00326 static int sad8_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
00327 {\
00328     assert(h==8);\
00329     __asm__ volatile("pxor %%mm7, %%mm7     \n\t"\
00330                  "pxor %%mm6, %%mm6     \n\t":);\
00331 \
00332     sad8_1_ ## suf(blk1, blk2, stride, 8);\
00333 \
00334     return sum_ ## suf();\
00335 }\
00336 static int sad8_x2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
00337 {\
00338     assert(h==8);\
00339     __asm__ volatile("pxor %%mm7, %%mm7     \n\t"\
00340                  "pxor %%mm6, %%mm6     \n\t"\
00341                  "movq %0, %%mm5        \n\t"\
00342                  :: "m"(round_tab[1]) \
00343                  );\
00344 \
00345     sad8_x2a_ ## suf(blk1, blk2, stride, 8);\
00346 \
00347     return sum_ ## suf();\
00348 }\
00349 \
00350 static int sad8_y2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
00351 {\
00352     assert(h==8);\
00353     __asm__ volatile("pxor %%mm7, %%mm7     \n\t"\
00354                  "pxor %%mm6, %%mm6     \n\t"\
00355                  "movq %0, %%mm5        \n\t"\
00356                  :: "m"(round_tab[1]) \
00357                  );\
00358 \
00359     sad8_y2a_ ## suf(blk1, blk2, stride, 8);\
00360 \
00361     return sum_ ## suf();\
00362 }\
00363 \
00364 static int sad8_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
00365 {\
00366     assert(h==8);\
00367     __asm__ volatile("pxor %%mm7, %%mm7     \n\t"\
00368                  "pxor %%mm6, %%mm6     \n\t"\
00369                  ::);\
00370 \
00371     sad8_4_ ## suf(blk1, blk2, stride, 8);\
00372 \
00373     return sum_ ## suf();\
00374 }\
00375 \
00376 static int sad16_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
00377 {\
00378     __asm__ volatile("pxor %%mm7, %%mm7     \n\t"\
00379                  "pxor %%mm6, %%mm6     \n\t":);\
00380 \
00381     sad8_1_ ## suf(blk1  , blk2  , stride, h);\
00382     sad8_1_ ## suf(blk1+8, blk2+8, stride, h);\
00383 \
00384     return sum_ ## suf();\
00385 }\
00386 static int sad16_x2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
00387 {\
00388     __asm__ volatile("pxor %%mm7, %%mm7     \n\t"\
00389                  "pxor %%mm6, %%mm6     \n\t"\
00390                  "movq %0, %%mm5        \n\t"\
00391                  :: "m"(round_tab[1]) \
00392                  );\
00393 \
00394     sad8_x2a_ ## suf(blk1  , blk2  , stride, h);\
00395     sad8_x2a_ ## suf(blk1+8, blk2+8, stride, h);\
00396 \
00397     return sum_ ## suf();\
00398 }\
00399 static int sad16_y2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
00400 {\
00401     __asm__ volatile("pxor %%mm7, %%mm7     \n\t"\
00402                  "pxor %%mm6, %%mm6     \n\t"\
00403                  "movq %0, %%mm5        \n\t"\
00404                  :: "m"(round_tab[1]) \
00405                  );\
00406 \
00407     sad8_y2a_ ## suf(blk1  , blk2  , stride, h);\
00408     sad8_y2a_ ## suf(blk1+8, blk2+8, stride, h);\
00409 \
00410     return sum_ ## suf();\
00411 }\
00412 static int sad16_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
00413 {\
00414     __asm__ volatile("pxor %%mm7, %%mm7     \n\t"\
00415                  "pxor %%mm6, %%mm6     \n\t"\
00416                  ::);\
00417 \
00418     sad8_4_ ## suf(blk1  , blk2  , stride, h);\
00419     sad8_4_ ## suf(blk1+8, blk2+8, stride, h);\
00420 \
00421     return sum_ ## suf();\
00422 }\
00423 
00424 PIX_SAD(mmx)
00425 PIX_SAD(mmx2)
00426 
00427 void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx)
00428 {
00429     if (mm_flags & FF_MM_MMX) {
00430         c->pix_abs[0][0] = sad16_mmx;
00431         c->pix_abs[0][1] = sad16_x2_mmx;
00432         c->pix_abs[0][2] = sad16_y2_mmx;
00433         c->pix_abs[0][3] = sad16_xy2_mmx;
00434         c->pix_abs[1][0] = sad8_mmx;
00435         c->pix_abs[1][1] = sad8_x2_mmx;
00436         c->pix_abs[1][2] = sad8_y2_mmx;
00437         c->pix_abs[1][3] = sad8_xy2_mmx;
00438 
00439         c->sad[0]= sad16_mmx;
00440         c->sad[1]= sad8_mmx;
00441     }
00442     if (mm_flags & FF_MM_MMXEXT) {
00443         c->pix_abs[0][0] = sad16_mmx2;
00444         c->pix_abs[1][0] = sad8_mmx2;
00445 
00446         c->sad[0]= sad16_mmx2;
00447         c->sad[1]= sad8_mmx2;
00448 
00449         if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
00450             c->pix_abs[0][1] = sad16_x2_mmx2;
00451             c->pix_abs[0][2] = sad16_y2_mmx2;
00452             c->pix_abs[0][3] = sad16_xy2_mmx2;
00453             c->pix_abs[1][1] = sad8_x2_mmx2;
00454             c->pix_abs[1][2] = sad8_y2_mmx2;
00455             c->pix_abs[1][3] = sad8_xy2_mmx2;
00456         }
00457     }
00458     if ((mm_flags & FF_MM_SSE2) && !(mm_flags & FF_MM_3DNOW)) {
00459         c->sad[0]= sad16_sse2;
00460     }
00461 }

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1