]> git.sesse.net Git - ffmpeg/blob - libswscale/x86/hscale_fast_bilinear_simd.c
sws/x86: split mmxext fast bilinear scaler out
[ffmpeg] / libswscale / x86 / hscale_fast_bilinear_simd.c
1 /*
2  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
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 "../swscale_internal.h"
22 #include "libavutil/x86/asm.h"
23 #include "libavutil/x86/cpu.h"
24
25 #define RET 0xC3 // near return opcode for x86
26 #define PREFETCH "prefetchnta"
27
28 av_cold int ff_init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
29                                        int16_t *filter, int32_t *filterPos,
30                                        int numSplits)
31 {
32     uint8_t *fragmentA;
33     x86_reg imm8OfPShufW1A;
34     x86_reg imm8OfPShufW2A;
35     x86_reg fragmentLengthA;
36     uint8_t *fragmentB;
37     x86_reg imm8OfPShufW1B;
38     x86_reg imm8OfPShufW2B;
39     x86_reg fragmentLengthB;
40     int fragmentPos;
41
42     int xpos, i;
43
44     // create an optimized horizontal scaling routine
45     /* This scaler is made of runtime-generated MMXEXT code using specially tuned
46      * pshufw instructions. For every four output pixels, if four input pixels
47      * are enough for the fast bilinear scaling, then a chunk of fragmentB is
48      * used. If five input pixels are needed, then a chunk of fragmentA is used.
49      */
50
51     // code fragment
52
53     __asm__ volatile (
54         "jmp                         9f                 \n\t"
55         // Begin
56         "0:                                             \n\t"
57         "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
58         "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
59         "movd   1(%%"REG_c", %%"REG_S"), %%mm1          \n\t"
60         "punpcklbw                %%mm7, %%mm1          \n\t"
61         "punpcklbw                %%mm7, %%mm0          \n\t"
62         "pshufw                   $0xFF, %%mm1, %%mm1   \n\t"
63         "1:                                             \n\t"
64         "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
65         "2:                                             \n\t"
66         "psubw                    %%mm1, %%mm0          \n\t"
67         "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
68         "pmullw                   %%mm3, %%mm0          \n\t"
69         "psllw                       $7, %%mm1          \n\t"
70         "paddw                    %%mm1, %%mm0          \n\t"
71
72         "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
73
74         "add                         $8, %%"REG_a"      \n\t"
75         // End
76         "9:                                             \n\t"
77         // "int $3                                         \n\t"
78         "lea       " LOCAL_MANGLE(0b) ", %0             \n\t"
79         "lea       " LOCAL_MANGLE(1b) ", %1             \n\t"
80         "lea       " LOCAL_MANGLE(2b) ", %2             \n\t"
81         "dec                         %1                 \n\t"
82         "dec                         %2                 \n\t"
83         "sub                         %0, %1             \n\t"
84         "sub                         %0, %2             \n\t"
85         "lea       " LOCAL_MANGLE(9b) ", %3             \n\t"
86         "sub                         %0, %3             \n\t"
87
88
89         : "=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
90           "=r" (fragmentLengthA)
91         );
92
93     __asm__ volatile (
94         "jmp                         9f                 \n\t"
95         // Begin
96         "0:                                             \n\t"
97         "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
98         "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
99         "punpcklbw                %%mm7, %%mm0          \n\t"
100         "pshufw                   $0xFF, %%mm0, %%mm1   \n\t"
101         "1:                                             \n\t"
102         "pshufw                   $0xFF, %%mm0, %%mm0   \n\t"
103         "2:                                             \n\t"
104         "psubw                    %%mm1, %%mm0          \n\t"
105         "movl   8(%%"REG_b", %%"REG_a"), %%esi          \n\t"
106         "pmullw                   %%mm3, %%mm0          \n\t"
107         "psllw                       $7, %%mm1          \n\t"
108         "paddw                    %%mm1, %%mm0          \n\t"
109
110         "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
111
112         "add                         $8, %%"REG_a"      \n\t"
113         // End
114         "9:                                             \n\t"
115         // "int                       $3                   \n\t"
116         "lea       " LOCAL_MANGLE(0b) ", %0             \n\t"
117         "lea       " LOCAL_MANGLE(1b) ", %1             \n\t"
118         "lea       " LOCAL_MANGLE(2b) ", %2             \n\t"
119         "dec                         %1                 \n\t"
120         "dec                         %2                 \n\t"
121         "sub                         %0, %1             \n\t"
122         "sub                         %0, %2             \n\t"
123         "lea       " LOCAL_MANGLE(9b) ", %3             \n\t"
124         "sub                         %0, %3             \n\t"
125
126
127         : "=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
128           "=r" (fragmentLengthB)
129         );
130
131     xpos        = 0; // lumXInc/2 - 0x8000; // difference between pixel centers
132     fragmentPos = 0;
133
134     for (i = 0; i < dstW / numSplits; i++) {
135         int xx = xpos >> 16;
136
137         if ((i & 3) == 0) {
138             int a                  = 0;
139             int b                  = ((xpos + xInc) >> 16) - xx;
140             int c                  = ((xpos + xInc * 2) >> 16) - xx;
141             int d                  = ((xpos + xInc * 3) >> 16) - xx;
142             int inc                = (d + 1 < 4);
143             uint8_t *fragment      = inc ? fragmentB : fragmentA;
144             x86_reg imm8OfPShufW1  = inc ? imm8OfPShufW1B : imm8OfPShufW1A;
145             x86_reg imm8OfPShufW2  = inc ? imm8OfPShufW2B : imm8OfPShufW2A;
146             x86_reg fragmentLength = inc ? fragmentLengthB : fragmentLengthA;
147             int maxShift           = 3 - (d + inc);
148             int shift              = 0;
149
150             if (filterCode) {
151                 filter[i]        = ((xpos              & 0xFFFF) ^ 0xFFFF) >> 9;
152                 filter[i + 1]    = (((xpos + xInc)     & 0xFFFF) ^ 0xFFFF) >> 9;
153                 filter[i + 2]    = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
154                 filter[i + 3]    = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
155                 filterPos[i / 2] = xx;
156
157                 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
158
159                 filterCode[fragmentPos + imm8OfPShufW1] =  (a + inc)       |
160                                                           ((b + inc) << 2) |
161                                                           ((c + inc) << 4) |
162                                                           ((d + inc) << 6);
163                 filterCode[fragmentPos + imm8OfPShufW2] =  a | (b << 2) |
164                                                                (c << 4) |
165                                                                (d << 6);
166
167                 if (i + 4 - inc >= dstW)
168                     shift = maxShift;               // avoid overread
169                 else if ((filterPos[i / 2] & 3) <= maxShift)
170                     shift = filterPos[i / 2] & 3;   // align
171
172                 if (shift && i >= shift) {
173                     filterCode[fragmentPos + imm8OfPShufW1] += 0x55 * shift;
174                     filterCode[fragmentPos + imm8OfPShufW2] += 0x55 * shift;
175                     filterPos[i / 2]                        -= shift;
176                 }
177             }
178
179             fragmentPos += fragmentLength;
180
181             if (filterCode)
182                 filterCode[fragmentPos] = RET;
183         }
184         xpos += xInc;
185     }
186     if (filterCode)
187         filterPos[((i / 2) + 1) & (~1)] = xpos >> 16;  // needed to jump to the next part
188
189     return fragmentPos + 1;
190 }
191
192 void ff_hyscale_fast_mmxext(SwsContext *c, int16_t *dst,
193                                  int dstWidth, const uint8_t *src,
194                                  int srcW, int xInc)
195 {
196     int32_t *filterPos = c->hLumFilterPos;
197     int16_t *filter    = c->hLumFilter;
198     void    *mmxextFilterCode = c->lumMmxextFilterCode;
199     int i;
200 #if defined(PIC)
201     uint64_t ebxsave;
202 #endif
203 #if ARCH_X86_64
204     uint64_t retsave;
205 #endif
206
207     __asm__ volatile(
208 #if defined(PIC)
209         "mov               %%"REG_b", %5        \n\t"
210 #if ARCH_X86_64
211         "mov               -8(%%rsp), %%"REG_a" \n\t"
212         "mov               %%"REG_a", %6        \n\t"
213 #endif
214 #else
215 #if ARCH_X86_64
216         "mov               -8(%%rsp), %%"REG_a" \n\t"
217         "mov               %%"REG_a", %5        \n\t"
218 #endif
219 #endif
220         "pxor                  %%mm7, %%mm7     \n\t"
221         "mov                      %0, %%"REG_c" \n\t"
222         "mov                      %1, %%"REG_D" \n\t"
223         "mov                      %2, %%"REG_d" \n\t"
224         "mov                      %3, %%"REG_b" \n\t"
225         "xor               %%"REG_a", %%"REG_a" \n\t" // i
226         PREFETCH"        (%%"REG_c")            \n\t"
227         PREFETCH"      32(%%"REG_c")            \n\t"
228         PREFETCH"      64(%%"REG_c")            \n\t"
229
230 #if ARCH_X86_64
231 #define CALL_MMXEXT_FILTER_CODE \
232         "movl            (%%"REG_b"), %%esi     \n\t"\
233         "call                    *%4            \n\t"\
234         "movl (%%"REG_b", %%"REG_a"), %%esi     \n\t"\
235         "add               %%"REG_S", %%"REG_c" \n\t"\
236         "add               %%"REG_a", %%"REG_D" \n\t"\
237         "xor               %%"REG_a", %%"REG_a" \n\t"\
238
239 #else
240 #define CALL_MMXEXT_FILTER_CODE \
241         "movl (%%"REG_b"), %%esi        \n\t"\
242         "call         *%4                       \n\t"\
243         "addl (%%"REG_b", %%"REG_a"), %%"REG_c" \n\t"\
244         "add               %%"REG_a", %%"REG_D" \n\t"\
245         "xor               %%"REG_a", %%"REG_a" \n\t"\
246
247 #endif /* ARCH_X86_64 */
248
249         CALL_MMXEXT_FILTER_CODE
250         CALL_MMXEXT_FILTER_CODE
251         CALL_MMXEXT_FILTER_CODE
252         CALL_MMXEXT_FILTER_CODE
253         CALL_MMXEXT_FILTER_CODE
254         CALL_MMXEXT_FILTER_CODE
255         CALL_MMXEXT_FILTER_CODE
256         CALL_MMXEXT_FILTER_CODE
257
258 #if defined(PIC)
259         "mov                      %5, %%"REG_b" \n\t"
260 #if ARCH_X86_64
261         "mov                      %6, %%"REG_a" \n\t"
262         "mov               %%"REG_a", -8(%%rsp) \n\t"
263 #endif
264 #else
265 #if ARCH_X86_64
266         "mov                      %5, %%"REG_a" \n\t"
267         "mov               %%"REG_a", -8(%%rsp) \n\t"
268 #endif
269 #endif
270         :: "m" (src), "m" (dst), "m" (filter), "m" (filterPos),
271            "m" (mmxextFilterCode)
272 #if defined(PIC)
273           ,"m" (ebxsave)
274 #endif
275 #if ARCH_X86_64
276           ,"m"(retsave)
277 #endif
278         : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D
279 #if !defined(PIC)
280          ,"%"REG_b
281 #endif
282     );
283
284     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
285         dst[i] = src[srcW-1]*128;
286 }
287
288 void ff_hcscale_fast_mmxext(SwsContext *c, int16_t *dst1, int16_t *dst2,
289                                  int dstWidth, const uint8_t *src1,
290                                  const uint8_t *src2, int srcW, int xInc)
291 {
292     int32_t *filterPos = c->hChrFilterPos;
293     int16_t *filter    = c->hChrFilter;
294     void    *mmxextFilterCode = c->chrMmxextFilterCode;
295     int i;
296 #if defined(PIC)
297     DECLARE_ALIGNED(8, uint64_t, ebxsave);
298 #endif
299 #if ARCH_X86_64
300     DECLARE_ALIGNED(8, uint64_t, retsave);
301 #endif
302
303     __asm__ volatile(
304 #if defined(PIC)
305         "mov          %%"REG_b", %7         \n\t"
306 #if ARCH_X86_64
307         "mov          -8(%%rsp), %%"REG_a"  \n\t"
308         "mov          %%"REG_a", %8         \n\t"
309 #endif
310 #else
311 #if ARCH_X86_64
312         "mov          -8(%%rsp), %%"REG_a"  \n\t"
313         "mov          %%"REG_a", %7         \n\t"
314 #endif
315 #endif
316         "pxor             %%mm7, %%mm7      \n\t"
317         "mov                 %0, %%"REG_c"  \n\t"
318         "mov                 %1, %%"REG_D"  \n\t"
319         "mov                 %2, %%"REG_d"  \n\t"
320         "mov                 %3, %%"REG_b"  \n\t"
321         "xor          %%"REG_a", %%"REG_a"  \n\t" // i
322         PREFETCH"   (%%"REG_c")             \n\t"
323         PREFETCH" 32(%%"REG_c")             \n\t"
324         PREFETCH" 64(%%"REG_c")             \n\t"
325
326         CALL_MMXEXT_FILTER_CODE
327         CALL_MMXEXT_FILTER_CODE
328         CALL_MMXEXT_FILTER_CODE
329         CALL_MMXEXT_FILTER_CODE
330         "xor          %%"REG_a", %%"REG_a"  \n\t" // i
331         "mov                 %5, %%"REG_c"  \n\t" // src
332         "mov                 %6, %%"REG_D"  \n\t" // buf2
333         PREFETCH"   (%%"REG_c")             \n\t"
334         PREFETCH" 32(%%"REG_c")             \n\t"
335         PREFETCH" 64(%%"REG_c")             \n\t"
336
337         CALL_MMXEXT_FILTER_CODE
338         CALL_MMXEXT_FILTER_CODE
339         CALL_MMXEXT_FILTER_CODE
340         CALL_MMXEXT_FILTER_CODE
341
342 #if defined(PIC)
343         "mov %7, %%"REG_b"    \n\t"
344 #if ARCH_X86_64
345         "mov                 %8, %%"REG_a"  \n\t"
346         "mov          %%"REG_a", -8(%%rsp)  \n\t"
347 #endif
348 #else
349 #if ARCH_X86_64
350         "mov                 %7, %%"REG_a"  \n\t"
351         "mov          %%"REG_a", -8(%%rsp)  \n\t"
352 #endif
353 #endif
354         :: "m" (src1), "m" (dst1), "m" (filter), "m" (filterPos),
355            "m" (mmxextFilterCode), "m" (src2), "m"(dst2)
356 #if defined(PIC)
357           ,"m" (ebxsave)
358 #endif
359 #if ARCH_X86_64
360           ,"m"(retsave)
361 #endif
362         : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D
363 #if !defined(PIC)
364          ,"%"REG_b
365 #endif
366     );
367
368     for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
369         dst1[i] = src1[srcW-1]*128;
370         dst2[i] = src2[srcW-1]*128;
371     }
372 }