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