]> git.sesse.net Git - ffmpeg/blob - libswscale/x86/swscale_mmx.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libswscale / x86 / swscale_mmx.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 <inttypes.h>
22 #include "config.h"
23 #include "libswscale/swscale.h"
24 #include "libswscale/swscale_internal.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/x86_cpu.h"
27 #include "libavutil/cpu.h"
28 #include "libavutil/pixdesc.h"
29
30 DECLARE_ASM_CONST(8, uint64_t, bF8)=       0xF8F8F8F8F8F8F8F8LL;
31 DECLARE_ASM_CONST(8, uint64_t, bFC)=       0xFCFCFCFCFCFCFCFCLL;
32 DECLARE_ASM_CONST(8, uint64_t, w10)=       0x0010001000100010LL;
33 DECLARE_ASM_CONST(8, uint64_t, w02)=       0x0002000200020002LL;
34 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
35 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
36 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
37 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
38
39 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
40     0x0103010301030103LL,
41     0x0200020002000200LL,};
42
43 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
44     0x0602060206020602LL,
45     0x0004000400040004LL,};
46
47 DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
48 DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
49 DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
50 DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
51 DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
52 DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
53
54 DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
55 DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
56 DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
57
58 #ifdef FAST_BGR2YV12
59 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
60 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
61 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
62 #else
63 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
64 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
65 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
66 #endif /* FAST_BGR2YV12 */
67 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
68 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
69 DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
70
71 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
72 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
73 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
74 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
75 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008010000080100ULL;
76
77 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
78     {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
79     {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
80 };
81
82 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040010000400100ULL;
83
84 //MMX versions
85 #if HAVE_MMX
86 #undef RENAME
87 #define COMPILE_TEMPLATE_MMX2 0
88 #define RENAME(a) a ## _MMX
89 #include "swscale_template.c"
90 #endif
91
92 //MMX2 versions
93 #if HAVE_MMX2
94 #undef RENAME
95 #undef COMPILE_TEMPLATE_MMX2
96 #define COMPILE_TEMPLATE_MMX2 1
97 #define RENAME(a) a ## _MMX2
98 #include "swscale_template.c"
99 #endif
100
101 void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
102                            int lastInLumBuf, int lastInChrBuf)
103 {
104     const int dstH= c->dstH;
105     const int flags= c->flags;
106     int16_t **lumPixBuf= c->lumPixBuf;
107     int16_t **chrUPixBuf= c->chrUPixBuf;
108     int16_t **alpPixBuf= c->alpPixBuf;
109     const int vLumBufSize= c->vLumBufSize;
110     const int vChrBufSize= c->vChrBufSize;
111     int16_t *vLumFilterPos= c->vLumFilterPos;
112     int16_t *vChrFilterPos= c->vChrFilterPos;
113     int16_t *vLumFilter= c->vLumFilter;
114     int16_t *vChrFilter= c->vChrFilter;
115     int32_t *lumMmxFilter= c->lumMmxFilter;
116     int32_t *chrMmxFilter= c->chrMmxFilter;
117     int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
118     const int vLumFilterSize= c->vLumFilterSize;
119     const int vChrFilterSize= c->vChrFilterSize;
120     const int chrDstY= dstY>>c->chrDstVSubSample;
121     const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
122     const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
123
124     c->blueDither= ff_dither8[dstY&1];
125     if (c->dstFormat == PIX_FMT_RGB555 || c->dstFormat == PIX_FMT_BGR555)
126         c->greenDither= ff_dither8[dstY&1];
127     else
128         c->greenDither= ff_dither4[dstY&1];
129     c->redDither= ff_dither8[(dstY+1)&1];
130     if (dstY < dstH - 2) {
131         const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
132         const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
133         const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
134         int i;
135         if (flags & SWS_ACCURATE_RND) {
136             int s= APCK_SIZE / 8;
137             for (i=0; i<vLumFilterSize; i+=2) {
138                 *(const void**)&lumMmxFilter[s*i              ]= lumSrcPtr[i  ];
139                 *(const void**)&lumMmxFilter[s*i+APCK_PTR2/4  ]= lumSrcPtr[i+(vLumFilterSize>1)];
140                 lumMmxFilter[s*i+APCK_COEF/4  ]=
141                 lumMmxFilter[s*i+APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i    ]
142                 + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1]<<16 : 0);
143                 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
144                     *(const void**)&alpMmxFilter[s*i              ]= alpSrcPtr[i  ];
145                     *(const void**)&alpMmxFilter[s*i+APCK_PTR2/4  ]= alpSrcPtr[i+(vLumFilterSize>1)];
146                     alpMmxFilter[s*i+APCK_COEF/4  ]=
147                     alpMmxFilter[s*i+APCK_COEF/4+1]= lumMmxFilter[s*i+APCK_COEF/4  ];
148                 }
149             }
150             for (i=0; i<vChrFilterSize; i+=2) {
151                 *(const void**)&chrMmxFilter[s*i              ]= chrUSrcPtr[i  ];
152                 *(const void**)&chrMmxFilter[s*i+APCK_PTR2/4  ]= chrUSrcPtr[i+(vChrFilterSize>1)];
153                 chrMmxFilter[s*i+APCK_COEF/4  ]=
154                 chrMmxFilter[s*i+APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i    ]
155                 + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1]<<16 : 0);
156             }
157         } else {
158             for (i=0; i<vLumFilterSize; i++) {
159                 *(const void**)&lumMmxFilter[4*i+0]= lumSrcPtr[i];
160                 lumMmxFilter[4*i+2]=
161                 lumMmxFilter[4*i+3]=
162                 ((uint16_t)vLumFilter[dstY*vLumFilterSize + i])*0x10001;
163                 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
164                     *(const void**)&alpMmxFilter[4*i+0]= alpSrcPtr[i];
165                     alpMmxFilter[4*i+2]=
166                     alpMmxFilter[4*i+3]= lumMmxFilter[4*i+2];
167                 }
168             }
169             for (i=0; i<vChrFilterSize; i++) {
170                 *(const void**)&chrMmxFilter[4*i+0]= chrUSrcPtr[i];
171                 chrMmxFilter[4*i+2]=
172                 chrMmxFilter[4*i+3]=
173                 ((uint16_t)vChrFilter[chrDstY*vChrFilterSize + i])*0x10001;
174             }
175         }
176     }
177 }
178
179 static void yuv2yuvX_sse3(const int16_t *filter, int filterSize,
180                            const int16_t **src, uint8_t *dest, int dstW,
181                            const uint8_t *dither, int offset)
182 {
183     if(((int)dest) & 15){
184         return yuv2yuvX_MMX2(filter, filterSize, src, dest, dstW, dither, offset);
185     }
186     if (offset) {
187         __asm__ volatile("movq       (%0), %%xmm3\n\t"
188                          "movdqa    %%xmm3, %%xmm4\n\t"
189                          "psrlq       $24, %%xmm3\n\t"
190                          "psllq       $40, %%xmm4\n\t"
191                          "por       %%xmm4, %%xmm3\n\t"
192                          :: "r"(dither)
193                          );
194     } else {
195         __asm__ volatile("movq       (%0), %%xmm3\n\t"
196                          :: "r"(dither)
197                          );
198     }
199     __asm__ volatile(
200         "pxor      %%xmm0, %%xmm0\n\t"
201         "punpcklbw %%xmm0, %%xmm3\n\t"
202         "psraw        $4, %%xmm3\n\t"
203         "movdqa    %%xmm3, %%xmm4\n\t"
204         "movdqa    %%xmm3, %%xmm7\n\t"
205         "movl %3, %%ecx\n\t"
206         "mov                                 %0, %%"REG_d"  \n\t"\
207         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
208         ".p2align                             4             \n\t" /* FIXME Unroll? */\
209         "1:                                                 \n\t"\
210         "movddup                  8(%%"REG_d"), %%xmm0      \n\t" /* filterCoeff */\
211         "movdqa              (%%"REG_S", %%"REG_c", 2), %%xmm2      \n\t" /* srcData */\
212         "movdqa            16(%%"REG_S", %%"REG_c", 2), %%xmm5      \n\t" /* srcData */\
213         "add                                $16, %%"REG_d"  \n\t"\
214         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
215         "test                         %%"REG_S", %%"REG_S"  \n\t"\
216         "pmulhw                           %%xmm0, %%xmm2      \n\t"\
217         "pmulhw                           %%xmm0, %%xmm5      \n\t"\
218         "paddw                            %%xmm2, %%xmm3      \n\t"\
219         "paddw                            %%xmm5, %%xmm4      \n\t"\
220         " jnz                                1b             \n\t"\
221         "psraw                               $3, %%xmm3      \n\t"\
222         "psraw                               $3, %%xmm4      \n\t"\
223         "packuswb                         %%xmm4, %%xmm3      \n\t"
224         "movntdq                          %%xmm3, (%1, %%"REG_c")\n\t"
225         "add                         $16, %%"REG_c"         \n\t"\
226         "cmp                          %2, %%"REG_c"         \n\t"\
227         "movdqa    %%xmm7, %%xmm3\n\t"
228         "movdqa    %%xmm7, %%xmm4\n\t"
229         "mov                                 %0, %%"REG_d"  \n\t"\
230         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
231         "jb                                  1b             \n\t"\
232         :: "g" (filter),
233            "r" (dest-offset), "g" ((x86_reg)(dstW+offset)), "m" (offset)
234         : "%"REG_d, "%"REG_S, "%"REG_c
235     );
236 }
237
238 #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
239 extern void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
240                                                 SwsContext *c, int16_t *data, \
241                                                 int dstW, const uint8_t *src, \
242                                                 const int16_t *filter, \
243                                                 const int16_t *filterPos, int filterSize);
244
245 #define SCALE_FUNCS(filter_n, opt) \
246     SCALE_FUNC(filter_n,  8, 15, opt); \
247     SCALE_FUNC(filter_n,  9, 15, opt); \
248     SCALE_FUNC(filter_n, 10, 15, opt); \
249     SCALE_FUNC(filter_n, 14, 15, opt); \
250     SCALE_FUNC(filter_n, 16, 15, opt); \
251     SCALE_FUNC(filter_n,  8, 19, opt); \
252     SCALE_FUNC(filter_n,  9, 19, opt); \
253     SCALE_FUNC(filter_n, 10, 19, opt); \
254     SCALE_FUNC(filter_n, 14, 19, opt); \
255     SCALE_FUNC(filter_n, 16, 19, opt)
256
257 #define SCALE_FUNCS_MMX(opt) \
258     SCALE_FUNCS(4, opt); \
259     SCALE_FUNCS(8, opt); \
260     SCALE_FUNCS(X, opt)
261
262 #define SCALE_FUNCS_SSE(opt) \
263     SCALE_FUNCS(4, opt); \
264     SCALE_FUNCS(8, opt); \
265     SCALE_FUNCS(X4, opt); \
266     SCALE_FUNCS(X8, opt)
267
268 #if ARCH_X86_32
269 SCALE_FUNCS_MMX(mmx);
270 #endif
271 SCALE_FUNCS_SSE(sse2);
272 SCALE_FUNCS_SSE(ssse3);
273 SCALE_FUNCS_SSE(sse4);
274
275 #define VSCALEX_FUNC(size, opt) \
276 extern void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
277                                                const int16_t **src, uint8_t *dest, int dstW, \
278                                                const uint8_t *dither, int offset)
279 #define VSCALEX_FUNCS(opt1, opt2) \
280     VSCALEX_FUNC(8,  opt1); \
281     VSCALEX_FUNC(9,  opt2); \
282     VSCALEX_FUNC(10, opt2)
283
284 #if ARCH_X86_32
285 VSCALEX_FUNCS(mmx,  mmx2);
286 #endif
287 VSCALEX_FUNCS(sse2, sse2);
288 VSCALEX_FUNCS(sse4, sse4);
289 VSCALEX_FUNC(16, sse4);
290 VSCALEX_FUNCS(avx,  avx);
291
292 void ff_sws_init_swScale_mmx(SwsContext *c)
293 {
294     int cpu_flags = av_get_cpu_flags();
295
296     if (cpu_flags & AV_CPU_FLAG_MMX)
297         sws_init_swScale_MMX(c);
298 #if HAVE_MMX2
299     if (cpu_flags & AV_CPU_FLAG_MMX2)
300         sws_init_swScale_MMX2(c);
301     if (cpu_flags & AV_CPU_FLAG_SSE3){
302         if(c->use_mmx_vfilter && !(c->flags & SWS_ACCURATE_RND))
303             c->yuv2planeX = yuv2yuvX_sse3;
304     }
305 #endif
306
307 #if HAVE_YASM
308 #define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt1, opt2) do { \
309     if (c->srcBpc == 8) { \
310         hscalefn = c->dstBpc <= 10 ? ff_hscale8to15_ ## filtersize ## _ ## opt2 : \
311                                      ff_hscale8to19_ ## filtersize ## _ ## opt1; \
312     } else if (c->srcBpc == 9) { \
313         hscalefn = c->dstBpc <= 10 ? ff_hscale9to15_ ## filtersize ## _ ## opt2 : \
314                                      ff_hscale9to19_ ## filtersize ## _ ## opt1; \
315     } else if (c->srcBpc == 10) { \
316         hscalefn = c->dstBpc <= 10 ? ff_hscale10to15_ ## filtersize ## _ ## opt2 : \
317                                      ff_hscale10to19_ ## filtersize ## _ ## opt1; \
318     } else if (c->srcBpc == 14 || ((c->srcFormat==PIX_FMT_PAL8||isAnyRGB(c->srcFormat)) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)) { \
319         hscalefn = c->dstBpc <= 10 ? ff_hscale14to15_ ## filtersize ## _ ## opt2 : \
320                                      ff_hscale14to19_ ## filtersize ## _ ## opt1; \
321     } else { /* c->srcBpc == 16 */ \
322         hscalefn = c->dstBpc <= 10 ? ff_hscale16to15_ ## filtersize ## _ ## opt2 : \
323                                      ff_hscale16to19_ ## filtersize ## _ ## opt1; \
324     } \
325 } while (0)
326 #define ASSIGN_MMX_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
327     switch (filtersize) { \
328     case 4:  ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
329     case 8:  ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
330     default: ASSIGN_SCALE_FUNC2(hscalefn, X, opt1, opt2); break; \
331     }
332 #define ASSIGN_VSCALEX_FUNC(vscalefn, opt1, opt2, opt2chk, do_16_case) \
333 switch(c->dstBpc){ \
334     case 16:                                     /*do_16_case;*/                           break; \
335     case 10: if (!isBE(c->dstFormat) && opt2chk) /*vscalefn = ff_yuv2planeX_10_ ## opt2;*/ break; \
336     case 9:  if (!isBE(c->dstFormat) && opt2chk) /*vscalefn = ff_yuv2planeX_9_  ## opt2;*/ break; \
337     default:                                     /*vscalefn = ff_yuv2planeX_8_  ## opt1;*/ break; \
338     }
339 #if ARCH_X86_32
340     if (cpu_flags & AV_CPU_FLAG_MMX) {
341         ASSIGN_MMX_SCALE_FUNC(c->hyScale, c->hLumFilterSize, mmx, mmx);
342         ASSIGN_MMX_SCALE_FUNC(c->hcScale, c->hChrFilterSize, mmx, mmx);
343         ASSIGN_VSCALEX_FUNC(c->yuv2planeX, mmx, mmx2, cpu_flags & AV_CPU_FLAG_MMX2,);
344     }
345 #endif
346 #define ASSIGN_SSE_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
347     switch (filtersize) { \
348     case 4:  ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
349     case 8:  ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
350     default: if (filtersize & 4) ASSIGN_SCALE_FUNC2(hscalefn, X4, opt1, opt2); \
351              else                ASSIGN_SCALE_FUNC2(hscalefn, X8, opt1, opt2); \
352              break; \
353     }
354     if (cpu_flags & AV_CPU_FLAG_SSE2) {
355         ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse2, sse2);
356         ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse2, sse2);
357         ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse2, sse2, 1,);
358     }
359     if (cpu_flags & AV_CPU_FLAG_SSSE3) {
360         ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, ssse3, ssse3);
361         ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, ssse3, ssse3);
362     }
363     if (cpu_flags & AV_CPU_FLAG_SSE4) {
364         /* Xto15 don't need special sse4 functions */
365         ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse4, ssse3);
366         ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse4, ssse3);
367         ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse4, sse4, 1,
368                             if (!isBE(c->dstFormat)) c->yuv2planeX = ff_yuv2planeX_16_sse4);
369     }
370
371     if (cpu_flags & AV_CPU_FLAG_AVX) {
372         ASSIGN_VSCALEX_FUNC(c->yuv2planeX, avx, avx, 1,);
373     }
374 #endif
375 }