]> 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-2011 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
35 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
36     0x0103010301030103LL,
37     0x0200020002000200LL,};
38
39 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
40     0x0602060206020602LL,
41     0x0004000400040004LL,};
42
43 DECLARE_ASM_CONST(8, uint64_t, b16Mask)=   0x001F001F001F001FLL;
44 DECLARE_ASM_CONST(8, uint64_t, g16Mask)=   0x07E007E007E007E0LL;
45 DECLARE_ASM_CONST(8, uint64_t, r16Mask)=   0xF800F800F800F800LL;
46 DECLARE_ASM_CONST(8, uint64_t, b15Mask)=   0x001F001F001F001FLL;
47 DECLARE_ASM_CONST(8, uint64_t, g15Mask)=   0x03E003E003E003E0LL;
48 DECLARE_ASM_CONST(8, uint64_t, r15Mask)=   0x7C007C007C007C00LL;
49
50 DECLARE_ALIGNED(8, const uint64_t, ff_M24A)         = 0x00FF0000FF0000FFLL;
51 DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
52 DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
53
54 #ifdef FAST_BGR2YV12
55 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
56 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
57 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
58 #else
59 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
60 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
61 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
62 #endif /* FAST_BGR2YV12 */
63 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
64 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
65 DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
66
67
68 //MMX versions
69 #if HAVE_MMX
70 #undef RENAME
71 #define COMPILE_TEMPLATE_MMX2 0
72 #define RENAME(a) a ## _MMX
73 #include "swscale_template.c"
74 #endif
75
76 //MMX2 versions
77 #if HAVE_MMX2
78 #undef RENAME
79 #undef COMPILE_TEMPLATE_MMX2
80 #define COMPILE_TEMPLATE_MMX2 1
81 #define RENAME(a) a ## _MMX2
82 #include "swscale_template.c"
83 #endif
84
85 void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
86                            int lastInLumBuf, int lastInChrBuf)
87 {
88     const int dstH= c->dstH;
89     const int flags= c->flags;
90     int16_t **lumPixBuf= c->lumPixBuf;
91     int16_t **chrUPixBuf= c->chrUPixBuf;
92     int16_t **alpPixBuf= c->alpPixBuf;
93     const int vLumBufSize= c->vLumBufSize;
94     const int vChrBufSize= c->vChrBufSize;
95     int16_t *vLumFilterPos= c->vLumFilterPos;
96     int16_t *vChrFilterPos= c->vChrFilterPos;
97     int16_t *vLumFilter= c->vLumFilter;
98     int16_t *vChrFilter= c->vChrFilter;
99     int32_t *lumMmxFilter= c->lumMmxFilter;
100     int32_t *chrMmxFilter= c->chrMmxFilter;
101     int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
102     const int vLumFilterSize= c->vLumFilterSize;
103     const int vChrFilterSize= c->vChrFilterSize;
104     const int chrDstY= dstY>>c->chrDstVSubSample;
105     const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
106     const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
107
108     c->blueDither= ff_dither8[dstY&1];
109     if (c->dstFormat == PIX_FMT_RGB555 || c->dstFormat == PIX_FMT_BGR555)
110         c->greenDither= ff_dither8[dstY&1];
111     else
112         c->greenDither= ff_dither4[dstY&1];
113     c->redDither= ff_dither8[(dstY+1)&1];
114     if (dstY < dstH - 2) {
115         const int16_t **lumSrcPtr= (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
116         const int16_t **chrUSrcPtr= (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
117         const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
118         int i;
119         if (flags & SWS_ACCURATE_RND) {
120             int s= APCK_SIZE / 8;
121             for (i=0; i<vLumFilterSize; i+=2) {
122                 *(const void**)&lumMmxFilter[s*i              ]= lumSrcPtr[i  ];
123                 *(const void**)&lumMmxFilter[s*i+APCK_PTR2/4  ]= lumSrcPtr[i+(vLumFilterSize>1)];
124                 lumMmxFilter[s*i+APCK_COEF/4  ]=
125                 lumMmxFilter[s*i+APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i    ]
126                 + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1]<<16 : 0);
127                 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
128                     *(const void**)&alpMmxFilter[s*i              ]= alpSrcPtr[i  ];
129                     *(const void**)&alpMmxFilter[s*i+APCK_PTR2/4  ]= alpSrcPtr[i+(vLumFilterSize>1)];
130                     alpMmxFilter[s*i+APCK_COEF/4  ]=
131                     alpMmxFilter[s*i+APCK_COEF/4+1]= lumMmxFilter[s*i+APCK_COEF/4  ];
132                 }
133             }
134             for (i=0; i<vChrFilterSize; i+=2) {
135                 *(const void**)&chrMmxFilter[s*i              ]= chrUSrcPtr[i  ];
136                 *(const void**)&chrMmxFilter[s*i+APCK_PTR2/4  ]= chrUSrcPtr[i+(vChrFilterSize>1)];
137                 chrMmxFilter[s*i+APCK_COEF/4  ]=
138                 chrMmxFilter[s*i+APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i    ]
139                 + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1]<<16 : 0);
140             }
141         } else {
142             for (i=0; i<vLumFilterSize; i++) {
143                 *(const void**)&lumMmxFilter[4*i+0]= lumSrcPtr[i];
144                 lumMmxFilter[4*i+2]=
145                 lumMmxFilter[4*i+3]=
146                 ((uint16_t)vLumFilter[dstY*vLumFilterSize + i])*0x10001;
147                 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
148                     *(const void**)&alpMmxFilter[4*i+0]= alpSrcPtr[i];
149                     alpMmxFilter[4*i+2]=
150                     alpMmxFilter[4*i+3]= lumMmxFilter[4*i+2];
151                 }
152             }
153             for (i=0; i<vChrFilterSize; i++) {
154                 *(const void**)&chrMmxFilter[4*i+0]= chrUSrcPtr[i];
155                 chrMmxFilter[4*i+2]=
156                 chrMmxFilter[4*i+3]=
157                 ((uint16_t)vChrFilter[chrDstY*vChrFilterSize + i])*0x10001;
158             }
159         }
160     }
161 }
162
163 #if HAVE_MMX2
164 static void yuv2yuvX_sse3(const int16_t *filter, int filterSize,
165                            const int16_t **src, uint8_t *dest, int dstW,
166                            const uint8_t *dither, int offset)
167 {
168     if(((int)dest) & 15){
169         return yuv2yuvX_MMX2(filter, filterSize, src, dest, dstW, dither, offset);
170     }
171     if (offset) {
172         __asm__ volatile("movq       (%0), %%xmm3\n\t"
173                          "movdqa    %%xmm3, %%xmm4\n\t"
174                          "psrlq       $24, %%xmm3\n\t"
175                          "psllq       $40, %%xmm4\n\t"
176                          "por       %%xmm4, %%xmm3\n\t"
177                          :: "r"(dither)
178                          );
179     } else {
180         __asm__ volatile("movq       (%0), %%xmm3\n\t"
181                          :: "r"(dither)
182                          );
183     }
184     __asm__ volatile(
185         "pxor      %%xmm0, %%xmm0\n\t"
186         "punpcklbw %%xmm0, %%xmm3\n\t"
187         "psraw        $4, %%xmm3\n\t"
188         "movdqa    %%xmm3, %%xmm4\n\t"
189         "movdqa    %%xmm3, %%xmm7\n\t"
190         "movl %3, %%ecx\n\t"
191         "mov                                 %0, %%"REG_d"  \n\t"\
192         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
193         ".p2align                             4             \n\t" /* FIXME Unroll? */\
194         "1:                                                 \n\t"\
195         "movddup                  8(%%"REG_d"), %%xmm0      \n\t" /* filterCoeff */\
196         "movdqa              (%%"REG_S", %%"REG_c", 2), %%xmm2      \n\t" /* srcData */\
197         "movdqa            16(%%"REG_S", %%"REG_c", 2), %%xmm5      \n\t" /* srcData */\
198         "add                                $16, %%"REG_d"  \n\t"\
199         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
200         "test                         %%"REG_S", %%"REG_S"  \n\t"\
201         "pmulhw                           %%xmm0, %%xmm2      \n\t"\
202         "pmulhw                           %%xmm0, %%xmm5      \n\t"\
203         "paddw                            %%xmm2, %%xmm3      \n\t"\
204         "paddw                            %%xmm5, %%xmm4      \n\t"\
205         " jnz                                1b             \n\t"\
206         "psraw                               $3, %%xmm3      \n\t"\
207         "psraw                               $3, %%xmm4      \n\t"\
208         "packuswb                         %%xmm4, %%xmm3      \n\t"
209         "movntdq                          %%xmm3, (%1, %%"REG_c")\n\t"
210         "add                         $16, %%"REG_c"         \n\t"\
211         "cmp                          %2, %%"REG_c"         \n\t"\
212         "movdqa    %%xmm7, %%xmm3\n\t"
213         "movdqa    %%xmm7, %%xmm4\n\t"
214         "mov                                 %0, %%"REG_d"  \n\t"\
215         "mov                        (%%"REG_d"), %%"REG_S"  \n\t"\
216         "jb                                  1b             \n\t"\
217         :: "g" (filter),
218            "r" (dest-offset), "g" ((x86_reg)(dstW+offset)), "m" (offset)
219         : "%"REG_d, "%"REG_S, "%"REG_c
220     );
221 }
222 #endif
223
224 #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
225 extern void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
226                                                 SwsContext *c, int16_t *data, \
227                                                 int dstW, const uint8_t *src, \
228                                                 const int16_t *filter, \
229                                                 const int16_t *filterPos, int filterSize)
230
231 #define SCALE_FUNCS(filter_n, opt) \
232     SCALE_FUNC(filter_n,  8, 15, opt); \
233     SCALE_FUNC(filter_n,  9, 15, opt); \
234     SCALE_FUNC(filter_n, 10, 15, opt); \
235     SCALE_FUNC(filter_n, 14, 15, opt); \
236     SCALE_FUNC(filter_n, 16, 15, opt); \
237     SCALE_FUNC(filter_n,  8, 19, opt); \
238     SCALE_FUNC(filter_n,  9, 19, opt); \
239     SCALE_FUNC(filter_n, 10, 19, opt); \
240     SCALE_FUNC(filter_n, 14, 19, opt); \
241     SCALE_FUNC(filter_n, 16, 19, opt)
242
243 #define SCALE_FUNCS_MMX(opt) \
244     SCALE_FUNCS(4, opt); \
245     SCALE_FUNCS(8, opt); \
246     SCALE_FUNCS(X, opt)
247
248 #define SCALE_FUNCS_SSE(opt) \
249     SCALE_FUNCS(4, opt); \
250     SCALE_FUNCS(8, opt); \
251     SCALE_FUNCS(X4, opt); \
252     SCALE_FUNCS(X8, opt)
253
254 #if ARCH_X86_32
255 SCALE_FUNCS_MMX(mmx);
256 #endif
257 SCALE_FUNCS_SSE(sse2);
258 SCALE_FUNCS_SSE(ssse3);
259 SCALE_FUNCS_SSE(sse4);
260
261 #define VSCALEX_FUNC(size, opt) \
262 extern void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
263                                                const int16_t **src, uint8_t *dest, int dstW, \
264                                                const uint8_t *dither, int offset)
265 #define VSCALEX_FUNCS(opt) \
266     VSCALEX_FUNC(8,  opt); \
267     VSCALEX_FUNC(9,  opt); \
268     VSCALEX_FUNC(10, opt)
269
270 #if ARCH_X86_32
271 VSCALEX_FUNCS(mmx2);
272 #endif
273 VSCALEX_FUNCS(sse2);
274 VSCALEX_FUNCS(sse4);
275 VSCALEX_FUNC(16, sse4);
276 VSCALEX_FUNCS(avx);
277
278 #define VSCALE_FUNC(size, opt) \
279 extern void ff_yuv2plane1_ ## size ## _ ## opt(const int16_t *src, uint8_t *dst, int dstW, \
280                                                const uint8_t *dither, int offset)
281 #define VSCALE_FUNCS(opt1, opt2) \
282     VSCALE_FUNC(8,  opt1); \
283     VSCALE_FUNC(9,  opt2); \
284     VSCALE_FUNC(10, opt2); \
285     VSCALE_FUNC(16, opt1)
286
287 #if ARCH_X86_32
288 VSCALE_FUNCS(mmx, mmx2);
289 #endif
290 VSCALE_FUNCS(sse2, sse2);
291 VSCALE_FUNC(16, sse4);
292 VSCALE_FUNCS(avx, avx);
293
294 #define INPUT_Y_FUNC(fmt, opt) \
295 extern void ff_ ## fmt ## ToY_  ## opt(uint8_t *dst, const uint8_t *src, \
296                                        int w, uint32_t *unused)
297 #define INPUT_UV_FUNC(fmt, opt) \
298 extern void ff_ ## fmt ## ToUV_ ## opt(uint8_t *dstU, uint8_t *dstV, \
299                                        const uint8_t *src, const uint8_t *unused1, \
300                                        int w, uint32_t *unused2)
301 #define INPUT_FUNC(fmt, opt) \
302     INPUT_Y_FUNC(fmt, opt); \
303     INPUT_UV_FUNC(fmt, opt)
304 #define INPUT_FUNCS(opt) \
305     INPUT_FUNC(uyvy, opt); \
306     INPUT_FUNC(yuyv, opt); \
307     INPUT_UV_FUNC(nv12, opt); \
308     INPUT_UV_FUNC(nv21, opt); \
309     INPUT_FUNC(rgb24, opt); \
310     INPUT_FUNC(bgr24, opt)
311
312 #if ARCH_X86_32
313 INPUT_FUNCS(mmx);
314 #endif
315 INPUT_FUNCS(sse2);
316 INPUT_FUNCS(ssse3);
317 INPUT_FUNCS(avx);
318
319 void ff_sws_init_swScale_mmx(SwsContext *c)
320 {
321     int cpu_flags = av_get_cpu_flags();
322
323     if (cpu_flags & AV_CPU_FLAG_MMX)
324         sws_init_swScale_MMX(c);
325 #if HAVE_MMX2
326     if (cpu_flags & AV_CPU_FLAG_MMX2)
327         sws_init_swScale_MMX2(c);
328     if (cpu_flags & AV_CPU_FLAG_SSE3){
329         if(c->use_mmx_vfilter && !(c->flags & SWS_ACCURATE_RND))
330             c->yuv2planeX = yuv2yuvX_sse3;
331     }
332 #endif
333
334 #if HAVE_YASM
335 #define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt1, opt2) do { \
336     if (c->srcBpc == 8) { \
337         hscalefn = c->dstBpc <= 10 ? ff_hscale8to15_ ## filtersize ## _ ## opt2 : \
338                                      ff_hscale8to19_ ## filtersize ## _ ## opt1; \
339     } else if (c->srcBpc == 9) { \
340         hscalefn = c->dstBpc <= 10 ? ff_hscale9to15_ ## filtersize ## _ ## opt2 : \
341                                      ff_hscale9to19_ ## filtersize ## _ ## opt1; \
342     } else if (c->srcBpc == 10) { \
343         hscalefn = c->dstBpc <= 10 ? ff_hscale10to15_ ## filtersize ## _ ## opt2 : \
344                                      ff_hscale10to19_ ## filtersize ## _ ## opt1; \
345     } else if (c->srcBpc == 14 || ((c->srcFormat==PIX_FMT_PAL8||isAnyRGB(c->srcFormat)) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)) { \
346         hscalefn = c->dstBpc <= 10 ? ff_hscale14to15_ ## filtersize ## _ ## opt2 : \
347                                      ff_hscale14to19_ ## filtersize ## _ ## opt1; \
348     } else { /* c->srcBpc == 16 */ \
349         hscalefn = c->dstBpc <= 10 ? ff_hscale16to15_ ## filtersize ## _ ## opt2 : \
350                                      ff_hscale16to19_ ## filtersize ## _ ## opt1; \
351     } \
352 } while (0)
353 #define ASSIGN_MMX_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
354     switch (filtersize) { \
355     case 4:  ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
356     case 8:  ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
357     default: ASSIGN_SCALE_FUNC2(hscalefn, X, opt1, opt2); break; \
358     }
359 #define ASSIGN_VSCALEX_FUNC(vscalefn, opt, do_16_case) \
360 switch(c->dstBpc){ \
361     case 16:                          /*do_16_case;*/                          break; \
362     case 10: if (!isBE(c->dstFormat)) /*vscalefn = ff_yuv2planeX_10_ ## opt;*/ break; \
363     case 9:  if (!isBE(c->dstFormat)) /*vscalefn = ff_yuv2planeX_9_  ## opt;*/ break; \
364     default:                          /*vscalefn = ff_yuv2planeX_8_  ## opt;*/ break; \
365     }
366 #define ASSIGN_VSCALE_FUNC(vscalefn, opt1, opt2, opt2chk) \
367     switch(c->dstBpc){ \
368     case 16: if (!isBE(c->dstFormat))            vscalefn = ff_yuv2plane1_16_ ## opt1; break; \
369     case 10: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_10_ ## opt2; break; \
370     case 9:  if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_9_  ## opt2;  break; \
371     default:                                     vscalefn = ff_yuv2plane1_8_  ## opt1;  break; \
372     }
373 #define case_rgb(x, X, opt) \
374         case PIX_FMT_ ## X: \
375             c->lumToYV12 = ff_ ## x ## ToY_ ## opt; \
376             if (!c->chrSrcHSubSample) \
377                 c->chrToYV12 = ff_ ## x ## ToUV_ ## opt; \
378             break
379 #if ARCH_X86_32
380     if (cpu_flags & AV_CPU_FLAG_MMX) {
381         ASSIGN_MMX_SCALE_FUNC(c->hyScale, c->hLumFilterSize, mmx, mmx);
382         ASSIGN_MMX_SCALE_FUNC(c->hcScale, c->hChrFilterSize, mmx, mmx);
383         ASSIGN_VSCALE_FUNC(c->yuv2plane1, mmx, mmx2, cpu_flags & AV_CPU_FLAG_MMX2);
384
385         switch (c->srcFormat) {
386         case PIX_FMT_Y400A:
387             c->lumToYV12 = ff_yuyvToY_mmx;
388             if (c->alpPixBuf)
389                 c->alpToYV12 = ff_uyvyToY_mmx;
390             break;
391         case PIX_FMT_YUYV422:
392             c->lumToYV12 = ff_yuyvToY_mmx;
393             c->chrToYV12 = ff_yuyvToUV_mmx;
394             break;
395         case PIX_FMT_UYVY422:
396             c->lumToYV12 = ff_uyvyToY_mmx;
397             c->chrToYV12 = ff_uyvyToUV_mmx;
398             break;
399         case PIX_FMT_NV12:
400             c->chrToYV12 = ff_nv12ToUV_mmx;
401             break;
402         case PIX_FMT_NV21:
403             c->chrToYV12 = ff_nv21ToUV_mmx;
404             break;
405         case_rgb(rgb24, RGB24, mmx);
406         case_rgb(bgr24, BGR24, mmx);
407         default:
408             break;
409         }
410     }
411     if (cpu_flags & AV_CPU_FLAG_MMX2) {
412         ASSIGN_VSCALEX_FUNC(c->yuv2planeX, mmx2,);
413     }
414 #endif
415 #define ASSIGN_SSE_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
416     switch (filtersize) { \
417     case 4:  ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
418     case 8:  ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
419     default: if (filtersize & 4) ASSIGN_SCALE_FUNC2(hscalefn, X4, opt1, opt2); \
420              else                ASSIGN_SCALE_FUNC2(hscalefn, X8, opt1, opt2); \
421              break; \
422     }
423     if (cpu_flags & AV_CPU_FLAG_SSE2) {
424         ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse2, sse2);
425         ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse2, sse2);
426         ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse2,);
427         ASSIGN_VSCALE_FUNC(c->yuv2plane1, sse2, sse2, 1);
428
429         switch (c->srcFormat) {
430         case PIX_FMT_Y400A:
431             c->lumToYV12 = ff_yuyvToY_sse2;
432             if (c->alpPixBuf)
433                 c->alpToYV12 = ff_uyvyToY_sse2;
434             break;
435         case PIX_FMT_YUYV422:
436             c->lumToYV12 = ff_yuyvToY_sse2;
437             c->chrToYV12 = ff_yuyvToUV_sse2;
438             break;
439         case PIX_FMT_UYVY422:
440             c->lumToYV12 = ff_uyvyToY_sse2;
441             c->chrToYV12 = ff_uyvyToUV_sse2;
442             break;
443         case PIX_FMT_NV12:
444             c->chrToYV12 = ff_nv12ToUV_sse2;
445             break;
446         case PIX_FMT_NV21:
447             c->chrToYV12 = ff_nv21ToUV_sse2;
448             break;
449         case_rgb(rgb24, RGB24, sse2);
450         case_rgb(bgr24, BGR24, sse2);
451         default:
452             break;
453         }
454     }
455     if (cpu_flags & AV_CPU_FLAG_SSSE3) {
456         ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, ssse3, ssse3);
457         ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, ssse3, ssse3);
458         switch (c->srcFormat) {
459         case_rgb(rgb24, RGB24, ssse3);
460         case_rgb(bgr24, BGR24, ssse3);
461         default:
462             break;
463         }
464     }
465     if (cpu_flags & AV_CPU_FLAG_SSE4) {
466         /* Xto15 don't need special sse4 functions */
467         ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse4, ssse3);
468         ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse4, ssse3);
469         ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse4,
470                             if (!isBE(c->dstFormat)) c->yuv2planeX = ff_yuv2planeX_16_sse4);
471         if (c->dstBpc == 16 && !isBE(c->dstFormat))
472             c->yuv2plane1 = ff_yuv2plane1_16_sse4;
473     }
474
475     if (HAVE_AVX && cpu_flags & AV_CPU_FLAG_AVX) {
476         ASSIGN_VSCALEX_FUNC(c->yuv2planeX, avx,);
477         ASSIGN_VSCALE_FUNC(c->yuv2plane1, avx, avx, 1);
478
479         switch (c->srcFormat) {
480         case PIX_FMT_YUYV422:
481             c->chrToYV12 = ff_yuyvToUV_avx;
482             break;
483         case PIX_FMT_UYVY422:
484             c->chrToYV12 = ff_uyvyToUV_avx;
485             break;
486         case PIX_FMT_NV12:
487             c->chrToYV12 = ff_nv12ToUV_avx;
488             break;
489         case PIX_FMT_NV21:
490             c->chrToYV12 = ff_nv21ToUV_avx;
491             break;
492         case_rgb(rgb24, RGB24, avx);
493         case_rgb(bgr24, BGR24, avx);
494         default:
495             break;
496         }
497     }
498 #endif
499 }