]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/dsputil_mmx.c
Merge commit 'cdab9db2adeec46b3984309c8c651bdd737d2b6b'
[ffmpeg] / libavcodec / x86 / dsputil_mmx.c
1 /*
2  * MMX optimized DSP utils
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
23  */
24
25 #include "config.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/cpu.h"
28 #include "libavutil/x86/asm.h"
29 #include "libavcodec/pixels.h"
30 #include "libavcodec/videodsp.h"
31 #include "dsputil_x86.h"
32 #include "inline_asm.h"
33
34 #if HAVE_INLINE_ASM
35
36 /* Draw the edges of width 'w' of an image of size width, height
37  * this MMX version can only handle w == 8 || w == 16. */
38 void ff_draw_edges_mmx(uint8_t *buf, int wrap, int width, int height,
39                        int w, int h, int sides)
40 {
41     uint8_t *ptr, *last_line;
42     int i;
43
44     last_line = buf + (height - 1) * wrap;
45     /* left and right */
46     ptr = buf;
47     if (w == 8) {
48         __asm__ volatile (
49             "1:                             \n\t"
50             "movd            (%0), %%mm0    \n\t"
51             "punpcklbw      %%mm0, %%mm0    \n\t"
52             "punpcklwd      %%mm0, %%mm0    \n\t"
53             "punpckldq      %%mm0, %%mm0    \n\t"
54             "movq           %%mm0, -8(%0)   \n\t"
55             "movq      -8(%0, %2), %%mm1    \n\t"
56             "punpckhbw      %%mm1, %%mm1    \n\t"
57             "punpckhwd      %%mm1, %%mm1    \n\t"
58             "punpckhdq      %%mm1, %%mm1    \n\t"
59             "movq           %%mm1, (%0, %2) \n\t"
60             "add               %1, %0       \n\t"
61             "cmp               %3, %0       \n\t"
62             "jb                1b           \n\t"
63             : "+r" (ptr)
64             : "r" ((x86_reg) wrap), "r" ((x86_reg) width),
65               "r" (ptr + wrap * height));
66     } else if (w == 16) {
67         __asm__ volatile (
68             "1:                                 \n\t"
69             "movd            (%0), %%mm0        \n\t"
70             "punpcklbw      %%mm0, %%mm0        \n\t"
71             "punpcklwd      %%mm0, %%mm0        \n\t"
72             "punpckldq      %%mm0, %%mm0        \n\t"
73             "movq           %%mm0, -8(%0)       \n\t"
74             "movq           %%mm0, -16(%0)      \n\t"
75             "movq      -8(%0, %2), %%mm1        \n\t"
76             "punpckhbw      %%mm1, %%mm1        \n\t"
77             "punpckhwd      %%mm1, %%mm1        \n\t"
78             "punpckhdq      %%mm1, %%mm1        \n\t"
79             "movq           %%mm1,  (%0, %2)    \n\t"
80             "movq           %%mm1, 8(%0, %2)    \n\t"
81             "add               %1, %0           \n\t"
82             "cmp               %3, %0           \n\t"
83             "jb                1b               \n\t"
84             : "+r"(ptr)
85             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
86             );
87     } else {
88         av_assert1(w == 4);
89         __asm__ volatile (
90             "1:                             \n\t"
91             "movd            (%0), %%mm0    \n\t"
92             "punpcklbw      %%mm0, %%mm0    \n\t"
93             "punpcklwd      %%mm0, %%mm0    \n\t"
94             "movd           %%mm0, -4(%0)   \n\t"
95             "movd      -4(%0, %2), %%mm1    \n\t"
96             "punpcklbw      %%mm1, %%mm1    \n\t"
97             "punpckhwd      %%mm1, %%mm1    \n\t"
98             "punpckhdq      %%mm1, %%mm1    \n\t"
99             "movd           %%mm1, (%0, %2) \n\t"
100             "add               %1, %0       \n\t"
101             "cmp               %3, %0       \n\t"
102             "jb                1b           \n\t"
103             : "+r" (ptr)
104             : "r" ((x86_reg) wrap), "r" ((x86_reg) width),
105               "r" (ptr + wrap * height));
106     }
107
108     /* top and bottom (and hopefully also the corners) */
109     if (sides & EDGE_TOP) {
110         for (i = 0; i < h; i += 4) {
111             ptr = buf - (i + 1) * wrap - w;
112             __asm__ volatile (
113                 "1:                             \n\t"
114                 "movq (%1, %0), %%mm0           \n\t"
115                 "movq    %%mm0, (%0)            \n\t"
116                 "movq    %%mm0, (%0, %2)        \n\t"
117                 "movq    %%mm0, (%0, %2, 2)     \n\t"
118                 "movq    %%mm0, (%0, %3)        \n\t"
119                 "add        $8, %0              \n\t"
120                 "cmp        %4, %0              \n\t"
121                 "jb         1b                  \n\t"
122                 : "+r" (ptr)
123                 : "r" ((x86_reg) buf - (x86_reg) ptr - w),
124                   "r" ((x86_reg) - wrap), "r" ((x86_reg) - wrap * 3),
125                   "r" (ptr + width + 2 * w));
126         }
127     }
128
129     if (sides & EDGE_BOTTOM) {
130         for (i = 0; i < h; i += 4) {
131             ptr = last_line + (i + 1) * wrap - w;
132             __asm__ volatile (
133                 "1:                             \n\t"
134                 "movq (%1, %0), %%mm0           \n\t"
135                 "movq    %%mm0, (%0)            \n\t"
136                 "movq    %%mm0, (%0, %2)        \n\t"
137                 "movq    %%mm0, (%0, %2, 2)     \n\t"
138                 "movq    %%mm0, (%0, %3)        \n\t"
139                 "add        $8, %0              \n\t"
140                 "cmp        %4, %0              \n\t"
141                 "jb         1b                  \n\t"
142                 : "+r" (ptr)
143                 : "r" ((x86_reg) last_line - (x86_reg) ptr - w),
144                   "r" ((x86_reg) wrap), "r" ((x86_reg) wrap * 3),
145                   "r" (ptr + width + 2 * w));
146         }
147     }
148 }
149
150 #endif /* HAVE_INLINE_ASM */