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/cpu.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/mem_internal.h"
31 #include "libavutil/pixdesc.h"
32
33 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
34 0x0103010301030103LL,
35 0x0200020002000200LL,};
36
37 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
38 0x0602060206020602LL,
39 0x0004000400040004LL,};
40
41 #if HAVE_INLINE_ASM
42
43 #define DITHER1XBPP
44
45 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
46 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
47
48 DECLARE_ASM_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
49 DECLARE_ASM_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
50 DECLARE_ASM_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
51
52 DECLARE_ASM_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
53 DECLARE_ASM_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
54 DECLARE_ASM_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
55
56
57 // MMXEXT versions
58 #if HAVE_MMXEXT_INLINE
59 #undef RENAME
60 #undef COMPILE_TEMPLATE_MMXEXT
61 #define COMPILE_TEMPLATE_MMXEXT 1
62 #define RENAME(a) a ## _mmxext
63 #include "swscale_template.c"
64 #endif
65
ff_updateMMXDitherTables(SwsContext *c, int dstY)66 void ff_updateMMXDitherTables(SwsContext *c, int dstY)
67 {
68 const int dstH= c->dstH;
69 const int flags= c->flags;
70
71 SwsPlane *lumPlane = &c->slice[c->numSlice-2].plane[0];
72 SwsPlane *chrUPlane = &c->slice[c->numSlice-2].plane[1];
73 SwsPlane *alpPlane = &c->slice[c->numSlice-2].plane[3];
74
75 int hasAlpha = c->needAlpha;
76 int32_t *vLumFilterPos= c->vLumFilterPos;
77 int32_t *vChrFilterPos= c->vChrFilterPos;
78 int16_t *vLumFilter= c->vLumFilter;
79 int16_t *vChrFilter= c->vChrFilter;
80 int32_t *lumMmxFilter= c->lumMmxFilter;
81 int32_t *chrMmxFilter= c->chrMmxFilter;
82 int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
83 const int vLumFilterSize= c->vLumFilterSize;
84 const int vChrFilterSize= c->vChrFilterSize;
85 const int chrDstY= dstY>>c->chrDstVSubSample;
86 const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
87 const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
88
89 c->blueDither= ff_dither8[dstY&1];
90 if (c->dstFormat == AV_PIX_FMT_RGB555 || c->dstFormat == AV_PIX_FMT_BGR555)
91 c->greenDither= ff_dither8[dstY&1];
92 else
93 c->greenDither= ff_dither4[dstY&1];
94 c->redDither= ff_dither8[(dstY+1)&1];
95 if (dstY < dstH - 2) {
96 const int16_t **lumSrcPtr = (const int16_t **)(void*) lumPlane->line + firstLumSrcY - lumPlane->sliceY;
97 const int16_t **chrUSrcPtr = (const int16_t **)(void*) chrUPlane->line + firstChrSrcY - chrUPlane->sliceY;
98 const int16_t **alpSrcPtr = (CONFIG_SWSCALE_ALPHA && hasAlpha) ? (const int16_t **)(void*) alpPlane->line + firstLumSrcY - alpPlane->sliceY : NULL;
99
100 int i;
101 if (firstLumSrcY < 0 || firstLumSrcY + vLumFilterSize > c->srcH) {
102 const int16_t **tmpY = (const int16_t **) lumPlane->tmp;
103
104 int neg = -firstLumSrcY, i, end = FFMIN(c->srcH - firstLumSrcY, vLumFilterSize);
105 for (i = 0; i < neg; i++)
106 tmpY[i] = lumSrcPtr[neg];
107 for ( ; i < end; i++)
108 tmpY[i] = lumSrcPtr[i];
109 for ( ; i < vLumFilterSize; i++)
110 tmpY[i] = tmpY[i-1];
111 lumSrcPtr = tmpY;
112
113 if (alpSrcPtr) {
114 const int16_t **tmpA = (const int16_t **) alpPlane->tmp;
115 for (i = 0; i < neg; i++)
116 tmpA[i] = alpSrcPtr[neg];
117 for ( ; i < end; i++)
118 tmpA[i] = alpSrcPtr[i];
119 for ( ; i < vLumFilterSize; i++)
120 tmpA[i] = tmpA[i - 1];
121 alpSrcPtr = tmpA;
122 }
123 }
124 if (firstChrSrcY < 0 || firstChrSrcY + vChrFilterSize > c->chrSrcH) {
125 const int16_t **tmpU = (const int16_t **) chrUPlane->tmp;
126 int neg = -firstChrSrcY, i, end = FFMIN(c->chrSrcH - firstChrSrcY, vChrFilterSize);
127 for (i = 0; i < neg; i++) {
128 tmpU[i] = chrUSrcPtr[neg];
129 }
130 for ( ; i < end; i++) {
131 tmpU[i] = chrUSrcPtr[i];
132 }
133 for ( ; i < vChrFilterSize; i++) {
134 tmpU[i] = tmpU[i - 1];
135 }
136 chrUSrcPtr = tmpU;
137 }
138
139 if (flags & SWS_ACCURATE_RND) {
140 int s= APCK_SIZE / 8;
141 for (i=0; i<vLumFilterSize; i+=2) {
142 *(const void**)&lumMmxFilter[s*i ]= lumSrcPtr[i ];
143 *(const void**)&lumMmxFilter[s*i+APCK_PTR2/4 ]= lumSrcPtr[i+(vLumFilterSize>1)];
144 lumMmxFilter[s*i+APCK_COEF/4 ]=
145 lumMmxFilter[s*i+APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i ]
146 + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1] * (1 << 16) : 0);
147 if (CONFIG_SWSCALE_ALPHA && hasAlpha) {
148 *(const void**)&alpMmxFilter[s*i ]= alpSrcPtr[i ];
149 *(const void**)&alpMmxFilter[s*i+APCK_PTR2/4 ]= alpSrcPtr[i+(vLumFilterSize>1)];
150 alpMmxFilter[s*i+APCK_COEF/4 ]=
151 alpMmxFilter[s*i+APCK_COEF/4+1]= lumMmxFilter[s*i+APCK_COEF/4 ];
152 }
153 }
154 for (i=0; i<vChrFilterSize; i+=2) {
155 *(const void**)&chrMmxFilter[s*i ]= chrUSrcPtr[i ];
156 *(const void**)&chrMmxFilter[s*i+APCK_PTR2/4 ]= chrUSrcPtr[i+(vChrFilterSize>1)];
157 chrMmxFilter[s*i+APCK_COEF/4 ]=
158 chrMmxFilter[s*i+APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i ]
159 + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1] * (1 << 16) : 0);
160 }
161 } else {
162 for (i=0; i<vLumFilterSize; i++) {
163 *(const void**)&lumMmxFilter[4*i+0]= lumSrcPtr[i];
164 lumMmxFilter[4*i+2]=
165 lumMmxFilter[4*i+3]=
166 ((uint16_t)vLumFilter[dstY*vLumFilterSize + i])*0x10001U;
167 if (CONFIG_SWSCALE_ALPHA && hasAlpha) {
168 *(const void**)&alpMmxFilter[4*i+0]= alpSrcPtr[i];
169 alpMmxFilter[4*i+2]=
170 alpMmxFilter[4*i+3]= lumMmxFilter[4*i+2];
171 }
172 }
173 for (i=0; i<vChrFilterSize; i++) {
174 *(const void**)&chrMmxFilter[4*i+0]= chrUSrcPtr[i];
175 chrMmxFilter[4*i+2]=
176 chrMmxFilter[4*i+3]=
177 ((uint16_t)vChrFilter[chrDstY*vChrFilterSize + i])*0x10001U;
178 }
179 }
180 }
181 }
182 #endif /* HAVE_INLINE_ASM */
183
184 #define YUV2YUVX_FUNC_MMX(opt, step) \
185 void ff_yuv2yuvX_ ##opt(const int16_t *filter, int filterSize, int srcOffset, \
186 uint8_t *dest, int dstW, \
187 const uint8_t *dither, int offset); \
188 static void yuv2yuvX_ ##opt(const int16_t *filter, int filterSize, \
189 const int16_t **src, uint8_t *dest, int dstW, \
190 const uint8_t *dither, int offset) \
191 { \
192 if(dstW > 0) \
193 ff_yuv2yuvX_ ##opt(filter, filterSize - 1, 0, dest - offset, dstW + offset, dither, offset); \
194 return; \
195 }
196
197 #define YUV2YUVX_FUNC(opt, step) \
198 void ff_yuv2yuvX_ ##opt(const int16_t *filter, int filterSize, int srcOffset, \
199 uint8_t *dest, int dstW, \
200 const uint8_t *dither, int offset); \
201 static void yuv2yuvX_ ##opt(const int16_t *filter, int filterSize, \
202 const int16_t **src, uint8_t *dest, int dstW, \
203 const uint8_t *dither, int offset) \
204 { \
205 int remainder = (dstW % step); \
206 int pixelsProcessed = dstW - remainder; \
207 if(((uintptr_t)dest) & 15){ \
208 yuv2yuvX_mmx(filter, filterSize, src, dest, dstW, dither, offset); \
209 return; \
210 } \
211 if(pixelsProcessed > 0) \
212 ff_yuv2yuvX_ ##opt(filter, filterSize - 1, 0, dest - offset, pixelsProcessed + offset, dither, offset); \
213 if(remainder > 0){ \
214 ff_yuv2yuvX_mmx(filter, filterSize - 1, pixelsProcessed, dest - offset, pixelsProcessed + remainder + offset, dither, offset); \
215 } \
216 return; \
217 }
218
219 #if HAVE_MMX_EXTERNAL
220 YUV2YUVX_FUNC_MMX(mmx, 16)
221 #endif
222 #if HAVE_MMXEXT_EXTERNAL
223 YUV2YUVX_FUNC_MMX(mmxext, 16)
224 #endif
225 #if HAVE_SSE3_EXTERNAL
226 YUV2YUVX_FUNC(sse3, 32)
227 #endif
228 #if HAVE_AVX2_EXTERNAL
229 YUV2YUVX_FUNC(avx2, 64)
230 #endif
231
232 #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
233 void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
234 SwsContext *c, int16_t *data, \
235 int dstW, const uint8_t *src, \
236 const int16_t *filter, \
237 const int32_t *filterPos, int filterSize)
238
239 #define SCALE_FUNCS(filter_n, opt) \
240 SCALE_FUNC(filter_n, 8, 15, opt); \
241 SCALE_FUNC(filter_n, 9, 15, opt); \
242 SCALE_FUNC(filter_n, 10, 15, opt); \
243 SCALE_FUNC(filter_n, 12, 15, opt); \
244 SCALE_FUNC(filter_n, 14, 15, opt); \
245 SCALE_FUNC(filter_n, 16, 15, opt); \
246 SCALE_FUNC(filter_n, 8, 19, opt); \
247 SCALE_FUNC(filter_n, 9, 19, opt); \
248 SCALE_FUNC(filter_n, 10, 19, opt); \
249 SCALE_FUNC(filter_n, 12, 19, opt); \
250 SCALE_FUNC(filter_n, 14, 19, opt); \
251 SCALE_FUNC(filter_n, 16, 19, opt)
252
253 #define SCALE_FUNCS_MMX(opt) \
254 SCALE_FUNCS(4, opt); \
255 SCALE_FUNCS(8, opt); \
256 SCALE_FUNCS(X, opt)
257
258 #define SCALE_FUNCS_SSE(opt) \
259 SCALE_FUNCS(4, opt); \
260 SCALE_FUNCS(8, opt); \
261 SCALE_FUNCS(X4, opt); \
262 SCALE_FUNCS(X8, opt)
263
264 SCALE_FUNCS_SSE(sse2);
265 SCALE_FUNCS_SSE(ssse3);
266 SCALE_FUNCS_SSE(sse4);
267
268 SCALE_FUNC(4, 8, 15, avx2);
269 SCALE_FUNC(X4, 8, 15, avx2);
270
271 #define VSCALEX_FUNC(size, opt) \
272 void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
273 const int16_t **src, uint8_t *dest, int dstW, \
274 const uint8_t *dither, int offset)
275 #define VSCALEX_FUNCS(opt) \
276 VSCALEX_FUNC(8, opt); \
277 VSCALEX_FUNC(9, opt); \
278 VSCALEX_FUNC(10, opt)
279
280 VSCALEX_FUNC(8, mmxext);
281 VSCALEX_FUNCS(sse2);
282 VSCALEX_FUNCS(sse4);
283 VSCALEX_FUNC(16, sse4);
284 VSCALEX_FUNCS(avx);
285
286 #define VSCALE_FUNC(size, opt) \
287 void ff_yuv2plane1_ ## size ## _ ## opt(const int16_t *src, uint8_t *dst, int dstW, \
288 const uint8_t *dither, int offset)
289 #define VSCALE_FUNCS(opt1, opt2) \
290 VSCALE_FUNC(8, opt1); \
291 VSCALE_FUNC(9, opt2); \
292 VSCALE_FUNC(10, opt2); \
293 VSCALE_FUNC(16, opt1)
294
295 VSCALE_FUNCS(sse2, sse2);
296 VSCALE_FUNC(16, sse4);
297 VSCALE_FUNCS(avx, avx);
298
299 #define INPUT_Y_FUNC(fmt, opt) \
300 void ff_ ## fmt ## ToY_ ## opt(uint8_t *dst, const uint8_t *src, \
301 const uint8_t *unused1, const uint8_t *unused2, \
302 int w, uint32_t *unused)
303 #define INPUT_UV_FUNC(fmt, opt) \
304 void ff_ ## fmt ## ToUV_ ## opt(uint8_t *dstU, uint8_t *dstV, \
305 const uint8_t *unused0, \
306 const uint8_t *src1, \
307 const uint8_t *src2, \
308 int w, uint32_t *unused)
309 #define INPUT_FUNC(fmt, opt) \
310 INPUT_Y_FUNC(fmt, opt); \
311 INPUT_UV_FUNC(fmt, opt)
312 #define INPUT_FUNCS(opt) \
313 INPUT_FUNC(uyvy, opt); \
314 INPUT_FUNC(yuyv, opt); \
315 INPUT_UV_FUNC(nv12, opt); \
316 INPUT_UV_FUNC(nv21, opt); \
317 INPUT_FUNC(rgba, opt); \
318 INPUT_FUNC(bgra, opt); \
319 INPUT_FUNC(argb, opt); \
320 INPUT_FUNC(abgr, opt); \
321 INPUT_FUNC(rgb24, opt); \
322 INPUT_FUNC(bgr24, opt)
323
324 INPUT_FUNCS(sse2);
325 INPUT_FUNCS(ssse3);
326 INPUT_FUNCS(avx);
327
328 #if ARCH_X86_64
329 #define YUV2NV_DECL(fmt, opt) \
330 void ff_yuv2 ## fmt ## cX_ ## opt(enum AVPixelFormat format, const uint8_t *dither, \
331 const int16_t *filter, int filterSize, \
332 const int16_t **u, const int16_t **v, \
333 uint8_t *dst, int dstWidth)
334
335 YUV2NV_DECL(nv12, avx2);
336 YUV2NV_DECL(nv21, avx2);
337
338 #define YUV2GBRP_FN_DECL(fmt, opt) \
339 void ff_yuv2##fmt##_full_X_ ##opt(SwsContext *c, const int16_t *lumFilter, \
340 const int16_t **lumSrcx, int lumFilterSize, \
341 const int16_t *chrFilter, const int16_t **chrUSrcx, \
342 const int16_t **chrVSrcx, int chrFilterSize, \
343 const int16_t **alpSrcx, uint8_t **dest, \
344 int dstW, int y)
345
346 #define YUV2GBRP_DECL(opt) \
347 YUV2GBRP_FN_DECL(gbrp, opt); \
348 YUV2GBRP_FN_DECL(gbrap, opt); \
349 YUV2GBRP_FN_DECL(gbrp9le, opt); \
350 YUV2GBRP_FN_DECL(gbrp10le, opt); \
351 YUV2GBRP_FN_DECL(gbrap10le, opt); \
352 YUV2GBRP_FN_DECL(gbrp12le, opt); \
353 YUV2GBRP_FN_DECL(gbrap12le, opt); \
354 YUV2GBRP_FN_DECL(gbrp14le, opt); \
355 YUV2GBRP_FN_DECL(gbrp16le, opt); \
356 YUV2GBRP_FN_DECL(gbrap16le, opt); \
357 YUV2GBRP_FN_DECL(gbrpf32le, opt); \
358 YUV2GBRP_FN_DECL(gbrapf32le, opt); \
359 YUV2GBRP_FN_DECL(gbrp9be, opt); \
360 YUV2GBRP_FN_DECL(gbrp10be, opt); \
361 YUV2GBRP_FN_DECL(gbrap10be, opt); \
362 YUV2GBRP_FN_DECL(gbrp12be, opt); \
363 YUV2GBRP_FN_DECL(gbrap12be, opt); \
364 YUV2GBRP_FN_DECL(gbrp14be, opt); \
365 YUV2GBRP_FN_DECL(gbrp16be, opt); \
366 YUV2GBRP_FN_DECL(gbrap16be, opt); \
367 YUV2GBRP_FN_DECL(gbrpf32be, opt); \
368 YUV2GBRP_FN_DECL(gbrapf32be, opt)
369
370 YUV2GBRP_DECL(sse2);
371 YUV2GBRP_DECL(sse4);
372 YUV2GBRP_DECL(avx2);
373
374 #define INPUT_PLANAR_RGB_Y_FN_DECL(fmt, opt) \
375 void ff_planar_##fmt##_to_y_##opt(uint8_t *dst, \
376 const uint8_t *src[4], int w, int32_t *rgb2yuv)
377
378 #define INPUT_PLANAR_RGB_UV_FN_DECL(fmt, opt) \
379 void ff_planar_##fmt##_to_uv_##opt(uint8_t *dstU, uint8_t *dstV, \
380 const uint8_t *src[4], int w, int32_t *rgb2yuv)
381
382 #define INPUT_PLANAR_RGB_A_FN_DECL(fmt, opt) \
383 void ff_planar_##fmt##_to_a_##opt(uint8_t *dst, \
384 const uint8_t *src[4], int w, int32_t *rgb2yuv)
385
386
387 #define INPUT_PLANAR_RGBXX_A_DECL(fmt, opt) \
388 INPUT_PLANAR_RGB_A_FN_DECL(fmt##le, opt); \
389 INPUT_PLANAR_RGB_A_FN_DECL(fmt##be, opt)
390
391 #define INPUT_PLANAR_RGBXX_Y_DECL(fmt, opt) \
392 INPUT_PLANAR_RGB_Y_FN_DECL(fmt##le, opt); \
393 INPUT_PLANAR_RGB_Y_FN_DECL(fmt##be, opt)
394
395 #define INPUT_PLANAR_RGBXX_UV_DECL(fmt, opt) \
396 INPUT_PLANAR_RGB_UV_FN_DECL(fmt##le, opt); \
397 INPUT_PLANAR_RGB_UV_FN_DECL(fmt##be, opt)
398
399 #define INPUT_PLANAR_RGBXX_YUVA_DECL(fmt, opt) \
400 INPUT_PLANAR_RGBXX_Y_DECL(fmt, opt); \
401 INPUT_PLANAR_RGBXX_UV_DECL(fmt, opt); \
402 INPUT_PLANAR_RGBXX_A_DECL(fmt, opt)
403
404 #define INPUT_PLANAR_RGBXX_YUV_DECL(fmt, opt) \
405 INPUT_PLANAR_RGBXX_Y_DECL(fmt, opt); \
406 INPUT_PLANAR_RGBXX_UV_DECL(fmt, opt)
407
408 #define INPUT_PLANAR_RGBXX_UVA_DECL(fmt, opt) \
409 INPUT_PLANAR_RGBXX_UV_DECL(fmt, opt); \
410 INPUT_PLANAR_RGBXX_A_DECL(fmt, opt)
411
412 #define INPUT_PLANAR_RGB_A_ALL_DECL(opt) \
413 INPUT_PLANAR_RGB_A_FN_DECL(rgb, opt); \
414 INPUT_PLANAR_RGBXX_A_DECL(rgb10, opt); \
415 INPUT_PLANAR_RGBXX_A_DECL(rgb12, opt); \
416 INPUT_PLANAR_RGBXX_A_DECL(rgb16, opt); \
417 INPUT_PLANAR_RGBXX_A_DECL(rgbf32, opt)
418
419 #define INPUT_PLANAR_RGB_Y_ALL_DECL(opt) \
420 INPUT_PLANAR_RGB_Y_FN_DECL(rgb, opt); \
421 INPUT_PLANAR_RGBXX_Y_DECL(rgb9, opt); \
422 INPUT_PLANAR_RGBXX_Y_DECL(rgb10, opt); \
423 INPUT_PLANAR_RGBXX_Y_DECL(rgb12, opt); \
424 INPUT_PLANAR_RGBXX_Y_DECL(rgb14, opt); \
425 INPUT_PLANAR_RGBXX_Y_DECL(rgb16, opt); \
426 INPUT_PLANAR_RGBXX_Y_DECL(rgbf32, opt)
427
428 #define INPUT_PLANAR_RGB_UV_ALL_DECL(opt) \
429 INPUT_PLANAR_RGB_UV_FN_DECL(rgb, opt); \
430 INPUT_PLANAR_RGBXX_UV_DECL(rgb9, opt); \
431 INPUT_PLANAR_RGBXX_UV_DECL(rgb10, opt); \
432 INPUT_PLANAR_RGBXX_UV_DECL(rgb12, opt); \
433 INPUT_PLANAR_RGBXX_UV_DECL(rgb14, opt); \
434 INPUT_PLANAR_RGBXX_UV_DECL(rgb16, opt); \
435 INPUT_PLANAR_RGBXX_UV_DECL(rgbf32, opt)
436
437 INPUT_PLANAR_RGBXX_Y_DECL(rgbf32, sse2);
438 INPUT_PLANAR_RGB_UV_ALL_DECL(sse2);
439 INPUT_PLANAR_RGB_A_ALL_DECL(sse2);
440
441 INPUT_PLANAR_RGB_Y_ALL_DECL(sse4);
442 INPUT_PLANAR_RGB_UV_ALL_DECL(sse4);
443 INPUT_PLANAR_RGBXX_A_DECL(rgbf32, sse4);
444
445 INPUT_PLANAR_RGB_Y_ALL_DECL(avx2);
446 INPUT_PLANAR_RGB_UV_ALL_DECL(avx2);
447 INPUT_PLANAR_RGB_A_ALL_DECL(avx2);
448 #endif
449
ff_sws_init_swscale_x86(SwsContext *c)450 av_cold void ff_sws_init_swscale_x86(SwsContext *c)
451 {
452 int cpu_flags = av_get_cpu_flags();
453
454 #if HAVE_MMXEXT_INLINE
455 if (INLINE_MMXEXT(cpu_flags))
456 sws_init_swscale_mmxext(c);
457 #endif
458 if(c->use_mmx_vfilter && !(c->flags & SWS_ACCURATE_RND)) {
459 #if HAVE_MMXEXT_EXTERNAL
460 if (EXTERNAL_MMXEXT(cpu_flags))
461 c->yuv2planeX = yuv2yuvX_mmxext;
462 #endif
463 #if HAVE_SSE3_EXTERNAL
464 if (EXTERNAL_SSE3(cpu_flags))
465 c->yuv2planeX = yuv2yuvX_sse3;
466 #endif
467 #if HAVE_AVX2_EXTERNAL
468 if (EXTERNAL_AVX2_FAST(cpu_flags))
469 c->yuv2planeX = yuv2yuvX_avx2;
470 #endif
471 }
472 #if ARCH_X86_32 && !HAVE_ALIGNED_STACK
473 // The better yuv2planeX_8 functions need aligned stack on x86-32,
474 // so we use MMXEXT in this case if they are not available.
475 if (EXTERNAL_MMXEXT(cpu_flags)) {
476 if (c->dstBpc == 8 && !c->use_mmx_vfilter)
477 c->yuv2planeX = ff_yuv2planeX_8_mmxext;
478 }
479 #endif /* ARCH_X86_32 && !HAVE_ALIGNED_STACK */
480
481 #define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt1, opt2) do { \
482 if (c->srcBpc == 8) { \
483 hscalefn = c->dstBpc <= 14 ? ff_hscale8to15_ ## filtersize ## _ ## opt2 : \
484 ff_hscale8to19_ ## filtersize ## _ ## opt1; \
485 } else if (c->srcBpc == 9) { \
486 hscalefn = c->dstBpc <= 14 ? ff_hscale9to15_ ## filtersize ## _ ## opt2 : \
487 ff_hscale9to19_ ## filtersize ## _ ## opt1; \
488 } else if (c->srcBpc == 10) { \
489 hscalefn = c->dstBpc <= 14 ? ff_hscale10to15_ ## filtersize ## _ ## opt2 : \
490 ff_hscale10to19_ ## filtersize ## _ ## opt1; \
491 } else if (c->srcBpc == 12) { \
492 hscalefn = c->dstBpc <= 14 ? ff_hscale12to15_ ## filtersize ## _ ## opt2 : \
493 ff_hscale12to19_ ## filtersize ## _ ## opt1; \
494 } else if (c->srcBpc == 14 || ((c->srcFormat==AV_PIX_FMT_PAL8||isAnyRGB(c->srcFormat)) && av_pix_fmt_desc_get(c->srcFormat)->comp[0].depth<16)) { \
495 hscalefn = c->dstBpc <= 14 ? ff_hscale14to15_ ## filtersize ## _ ## opt2 : \
496 ff_hscale14to19_ ## filtersize ## _ ## opt1; \
497 } else { /* c->srcBpc == 16 */ \
498 av_assert0(c->srcBpc == 16);\
499 hscalefn = c->dstBpc <= 14 ? ff_hscale16to15_ ## filtersize ## _ ## opt2 : \
500 ff_hscale16to19_ ## filtersize ## _ ## opt1; \
501 } \
502 } while (0)
503 #define ASSIGN_VSCALEX_FUNC(vscalefn, opt, do_16_case, condition_8bit) \
504 switch(c->dstBpc){ \
505 case 16: do_16_case; break; \
506 case 10: if (!isBE(c->dstFormat) && !isSemiPlanarYUV(c->dstFormat)) vscalefn = ff_yuv2planeX_10_ ## opt; break; \
507 case 9: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_9_ ## opt; break; \
508 case 8: if ((condition_8bit) && !c->use_mmx_vfilter) vscalefn = ff_yuv2planeX_8_ ## opt; break; \
509 }
510 #define ASSIGN_VSCALE_FUNC(vscalefn, opt) \
511 switch(c->dstBpc){ \
512 case 16: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2plane1_16_ ## opt; break; \
513 case 10: if (!isBE(c->dstFormat) && !isSemiPlanarYUV(c->dstFormat)) vscalefn = ff_yuv2plane1_10_ ## opt; break; \
514 case 9: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2plane1_9_ ## opt; break; \
515 case 8: vscalefn = ff_yuv2plane1_8_ ## opt; break; \
516 default: av_assert0(c->dstBpc>8); \
517 }
518 #define case_rgb(x, X, opt) \
519 case AV_PIX_FMT_ ## X: \
520 c->lumToYV12 = ff_ ## x ## ToY_ ## opt; \
521 if (!c->chrSrcHSubSample) \
522 c->chrToYV12 = ff_ ## x ## ToUV_ ## opt; \
523 break
524 #define ASSIGN_SSE_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
525 switch (filtersize) { \
526 case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
527 case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
528 default: if (filtersize & 4) ASSIGN_SCALE_FUNC2(hscalefn, X4, opt1, opt2); \
529 else ASSIGN_SCALE_FUNC2(hscalefn, X8, opt1, opt2); \
530 break; \
531 }
532 if (EXTERNAL_SSE2(cpu_flags)) {
533 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse2, sse2);
534 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse2, sse2);
535 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse2, ,
536 HAVE_ALIGNED_STACK || ARCH_X86_64);
537 ASSIGN_VSCALE_FUNC(c->yuv2plane1, sse2);
538
539 switch (c->srcFormat) {
540 case AV_PIX_FMT_YA8:
541 c->lumToYV12 = ff_yuyvToY_sse2;
542 if (c->needAlpha)
543 c->alpToYV12 = ff_uyvyToY_sse2;
544 break;
545 case AV_PIX_FMT_YUYV422:
546 c->lumToYV12 = ff_yuyvToY_sse2;
547 c->chrToYV12 = ff_yuyvToUV_sse2;
548 break;
549 case AV_PIX_FMT_UYVY422:
550 c->lumToYV12 = ff_uyvyToY_sse2;
551 c->chrToYV12 = ff_uyvyToUV_sse2;
552 break;
553 case AV_PIX_FMT_NV12:
554 c->chrToYV12 = ff_nv12ToUV_sse2;
555 break;
556 case AV_PIX_FMT_NV21:
557 c->chrToYV12 = ff_nv21ToUV_sse2;
558 break;
559 case_rgb(rgb24, RGB24, sse2);
560 case_rgb(bgr24, BGR24, sse2);
561 case_rgb(bgra, BGRA, sse2);
562 case_rgb(rgba, RGBA, sse2);
563 case_rgb(abgr, ABGR, sse2);
564 case_rgb(argb, ARGB, sse2);
565 default:
566 break;
567 }
568 }
569 if (EXTERNAL_SSSE3(cpu_flags)) {
570 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, ssse3, ssse3);
571 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, ssse3, ssse3);
572 switch (c->srcFormat) {
573 case_rgb(rgb24, RGB24, ssse3);
574 case_rgb(bgr24, BGR24, ssse3);
575 default:
576 break;
577 }
578 }
579 if (EXTERNAL_SSE4(cpu_flags)) {
580 /* Xto15 don't need special sse4 functions */
581 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse4, ssse3);
582 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse4, ssse3);
583 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse4,
584 if (!isBE(c->dstFormat)) c->yuv2planeX = ff_yuv2planeX_16_sse4,
585 HAVE_ALIGNED_STACK || ARCH_X86_64);
586 if (c->dstBpc == 16 && !isBE(c->dstFormat))
587 c->yuv2plane1 = ff_yuv2plane1_16_sse4;
588 }
589
590 if (EXTERNAL_AVX(cpu_flags)) {
591 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, avx, ,
592 HAVE_ALIGNED_STACK || ARCH_X86_64);
593 ASSIGN_VSCALE_FUNC(c->yuv2plane1, avx);
594
595 switch (c->srcFormat) {
596 case AV_PIX_FMT_YUYV422:
597 c->chrToYV12 = ff_yuyvToUV_avx;
598 break;
599 case AV_PIX_FMT_UYVY422:
600 c->chrToYV12 = ff_uyvyToUV_avx;
601 break;
602 case AV_PIX_FMT_NV12:
603 c->chrToYV12 = ff_nv12ToUV_avx;
604 break;
605 case AV_PIX_FMT_NV21:
606 c->chrToYV12 = ff_nv21ToUV_avx;
607 break;
608 case_rgb(rgb24, RGB24, avx);
609 case_rgb(bgr24, BGR24, avx);
610 case_rgb(bgra, BGRA, avx);
611 case_rgb(rgba, RGBA, avx);
612 case_rgb(abgr, ABGR, avx);
613 case_rgb(argb, ARGB, avx);
614 default:
615 break;
616 }
617 }
618
619 #if ARCH_X86_64
620 #define ASSIGN_AVX2_SCALE_FUNC(hscalefn, filtersize) \
621 switch (filtersize) { \
622 case 4: hscalefn = ff_hscale8to15_4_avx2; break; \
623 default: hscalefn = ff_hscale8to15_X4_avx2; break; \
624 break; \
625 }
626
627 if (EXTERNAL_AVX2_FAST(cpu_flags) && !(cpu_flags & AV_CPU_FLAG_SLOW_GATHER)) {
628 if ((c->srcBpc == 8) && (c->dstBpc <= 14)) {
629 if (c->chrDstW % 16 == 0)
630 ASSIGN_AVX2_SCALE_FUNC(c->hcScale, c->hChrFilterSize);
631 if (c->dstW % 16 == 0)
632 ASSIGN_AVX2_SCALE_FUNC(c->hyScale, c->hLumFilterSize);
633 }
634 }
635
636 if (EXTERNAL_AVX2_FAST(cpu_flags)) {
637 switch (c->dstFormat) {
638 case AV_PIX_FMT_NV12:
639 case AV_PIX_FMT_NV24:
640 c->yuv2nv12cX = ff_yuv2nv12cX_avx2;
641 break;
642 case AV_PIX_FMT_NV21:
643 case AV_PIX_FMT_NV42:
644 c->yuv2nv12cX = ff_yuv2nv21cX_avx2;
645 break;
646 default:
647 break;
648 }
649 }
650
651
652 #define INPUT_PLANER_RGB_A_FUNC_CASE(fmt, name, opt) \
653 case fmt: \
654 c->readAlpPlanar = ff_planar_##name##_to_a_##opt;
655
656 #define INPUT_PLANER_RGBA_YUV_FUNC_CASE(rgb_fmt, rgba_fmt, name, opt) \
657 case rgba_fmt: \
658 case rgb_fmt: \
659 c->readLumPlanar = ff_planar_##name##_to_y_##opt; \
660 c->readChrPlanar = ff_planar_##name##_to_uv_##opt; \
661 break;
662
663 #define INPUT_PLANER_RGB_YUV_FUNC_CASE(fmt, name, opt) \
664 case fmt: \
665 c->readLumPlanar = ff_planar_##name##_to_y_##opt; \
666 c->readChrPlanar = ff_planar_##name##_to_uv_##opt; \
667 break;
668
669 #define INPUT_PLANER_RGB_UV_FUNC_CASE(fmt, name, opt) \
670 case fmt: \
671 c->readChrPlanar = ff_planar_##name##_to_uv_##opt; \
672 break;
673
674 #define INPUT_PLANER_RGBAXX_YUVA_FUNC_CASE(rgb_fmt, rgba_fmt, name, opt) \
675 INPUT_PLANER_RGB_A_FUNC_CASE(rgba_fmt##LE, name##le, opt) \
676 INPUT_PLANER_RGB_YUV_FUNC_CASE(rgb_fmt##LE, name##le, opt) \
677 INPUT_PLANER_RGB_A_FUNC_CASE(rgba_fmt##BE, name##be, opt) \
678 INPUT_PLANER_RGB_YUV_FUNC_CASE(rgb_fmt##BE, name##be, opt)
679
680 #define INPUT_PLANER_RGBAXX_UVA_FUNC_CASE(rgb_fmt, rgba_fmt, name, opt) \
681 INPUT_PLANER_RGB_A_FUNC_CASE(rgba_fmt##LE, name##le, opt) \
682 INPUT_PLANER_RGB_UV_FUNC_CASE(rgb_fmt##LE, name##le, opt) \
683 INPUT_PLANER_RGB_A_FUNC_CASE(rgba_fmt##BE, name##be, opt) \
684 INPUT_PLANER_RGB_UV_FUNC_CASE(rgb_fmt##BE, name##be, opt)
685
686 #define INPUT_PLANER_RGBAXX_YUV_FUNC_CASE(rgb_fmt, rgba_fmt, name, opt) \
687 INPUT_PLANER_RGBA_YUV_FUNC_CASE(rgb_fmt##LE, rgba_fmt##LE, name##le, opt) \
688 INPUT_PLANER_RGBA_YUV_FUNC_CASE(rgb_fmt##BE, rgba_fmt##BE, name##be, opt)
689
690 #define INPUT_PLANER_RGBXX_YUV_FUNC_CASE(rgb_fmt, name, opt) \
691 INPUT_PLANER_RGB_YUV_FUNC_CASE(rgb_fmt##LE, name##le, opt) \
692 INPUT_PLANER_RGB_YUV_FUNC_CASE(rgb_fmt##BE, name##be, opt)
693
694 #define INPUT_PLANER_RGBXX_UV_FUNC_CASE(rgb_fmt, name, opt) \
695 INPUT_PLANER_RGB_UV_FUNC_CASE(rgb_fmt##LE, name##le, opt) \
696 INPUT_PLANER_RGB_UV_FUNC_CASE(rgb_fmt##BE, name##be, opt)
697
698 #define INPUT_PLANER_RGB_YUVA_ALL_CASES(opt) \
699 INPUT_PLANER_RGB_A_FUNC_CASE( AV_PIX_FMT_GBRAP, rgb, opt) \
700 INPUT_PLANER_RGB_YUV_FUNC_CASE( AV_PIX_FMT_GBRP, rgb, opt) \
701 INPUT_PLANER_RGBXX_YUV_FUNC_CASE( AV_PIX_FMT_GBRP9, rgb9, opt) \
702 INPUT_PLANER_RGBAXX_YUVA_FUNC_CASE(AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, rgb10, opt) \
703 INPUT_PLANER_RGBAXX_YUVA_FUNC_CASE(AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, rgb12, opt) \
704 INPUT_PLANER_RGBXX_YUV_FUNC_CASE( AV_PIX_FMT_GBRP14, rgb14, opt) \
705 INPUT_PLANER_RGBAXX_YUVA_FUNC_CASE(AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, rgb16, opt) \
706 INPUT_PLANER_RGBAXX_YUVA_FUNC_CASE(AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32, rgbf32, opt)
707
708
709 if (EXTERNAL_SSE2(cpu_flags)) {
710 switch (c->srcFormat) {
711 INPUT_PLANER_RGB_A_FUNC_CASE( AV_PIX_FMT_GBRAP, rgb, sse2);
712 INPUT_PLANER_RGB_UV_FUNC_CASE( AV_PIX_FMT_GBRP, rgb, sse2);
713 INPUT_PLANER_RGBXX_UV_FUNC_CASE( AV_PIX_FMT_GBRP9, rgb9, sse2);
714 INPUT_PLANER_RGBAXX_UVA_FUNC_CASE( AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, rgb10, sse2);
715 INPUT_PLANER_RGBAXX_UVA_FUNC_CASE( AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, rgb12, sse2);
716 INPUT_PLANER_RGBXX_UV_FUNC_CASE( AV_PIX_FMT_GBRP14, rgb14, sse2);
717 INPUT_PLANER_RGBAXX_UVA_FUNC_CASE( AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, rgb16, sse2);
718 INPUT_PLANER_RGBAXX_YUVA_FUNC_CASE(AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32, rgbf32, sse2);
719 default:
720 break;
721 }
722 }
723
724 if (EXTERNAL_SSE4(cpu_flags)) {
725 switch (c->srcFormat) {
726 case AV_PIX_FMT_GBRAP:
727 INPUT_PLANER_RGB_YUV_FUNC_CASE( AV_PIX_FMT_GBRP, rgb, sse4);
728 INPUT_PLANER_RGBXX_YUV_FUNC_CASE( AV_PIX_FMT_GBRP9, rgb9, sse4);
729 INPUT_PLANER_RGBAXX_YUV_FUNC_CASE( AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, rgb10, sse4);
730 INPUT_PLANER_RGBAXX_YUV_FUNC_CASE( AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, rgb12, sse4);
731 INPUT_PLANER_RGBXX_YUV_FUNC_CASE( AV_PIX_FMT_GBRP14, rgb14, sse4);
732 INPUT_PLANER_RGBAXX_YUV_FUNC_CASE( AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, rgb16, sse4);
733 INPUT_PLANER_RGBAXX_YUVA_FUNC_CASE(AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32, rgbf32, sse4);
734 default:
735 break;
736 }
737 }
738
739 if (EXTERNAL_AVX2_FAST(cpu_flags)) {
740 switch (c->srcFormat) {
741 INPUT_PLANER_RGB_YUVA_ALL_CASES(avx2)
742 default:
743 break;
744 }
745 }
746
747 if(c->flags & SWS_FULL_CHR_H_INT) {
748
749 /* yuv2gbrp uses the SwsContext for yuv coefficients
750 if struct offsets change the asm needs to be updated too */
751 av_assert0(offsetof(SwsContext, yuv2rgb_y_offset) == 40292);
752
753 #define YUV2ANYX_FUNC_CASE(fmt, name, opt) \
754 case fmt: \
755 c->yuv2anyX = ff_yuv2##name##_full_X_##opt; \
756 break;
757
758 #define YUV2ANYX_GBRAP_CASES(opt) \
759 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP, gbrp, opt) \
760 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAP, gbrap, opt) \
761 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP9LE, gbrp9le, opt) \
762 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP10LE, gbrp10le, opt) \
763 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAP10LE, gbrap10le, opt) \
764 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP12LE, gbrp12le, opt) \
765 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAP12LE, gbrap12le, opt) \
766 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP14LE, gbrp14le, opt) \
767 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP16LE, gbrp16le, opt) \
768 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAP16LE, gbrap16le, opt) \
769 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRPF32LE, gbrpf32le, opt) \
770 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAPF32LE, gbrapf32le, opt) \
771 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP9BE, gbrp9be, opt) \
772 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP10BE, gbrp10be, opt) \
773 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAP10BE, gbrap10be, opt) \
774 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP12BE, gbrp12be, opt) \
775 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAP12BE, gbrap12be, opt) \
776 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP14BE, gbrp14be, opt) \
777 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRP16BE, gbrp16be, opt) \
778 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAP16BE, gbrap16be, opt) \
779 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRPF32BE, gbrpf32be, opt) \
780 YUV2ANYX_FUNC_CASE(AV_PIX_FMT_GBRAPF32BE, gbrapf32be, opt)
781
782 if (EXTERNAL_SSE2(cpu_flags)) {
783 switch (c->dstFormat) {
784 YUV2ANYX_GBRAP_CASES(sse2)
785 default:
786 break;
787 }
788 }
789
790 if (EXTERNAL_SSE4(cpu_flags)) {
791 switch (c->dstFormat) {
792 YUV2ANYX_GBRAP_CASES(sse4)
793 default:
794 break;
795 }
796 }
797
798 if (EXTERNAL_AVX2_FAST(cpu_flags)) {
799 switch (c->dstFormat) {
800 YUV2ANYX_GBRAP_CASES(avx2)
801 default:
802 break;
803 }
804 }
805 }
806
807 #endif
808 }
809