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