2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav 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.
11 * Libav 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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"
32 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
33 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
34 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
35 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
37 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
39 0x0200020002000200LL,};
41 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
43 0x0004000400040004LL,};
45 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
46 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
47 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
48 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
49 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
50 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
52 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
53 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
54 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
57 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
58 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
59 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
61 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
62 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
63 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
64 #endif /* FAST_BGR2YV12 */
65 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
66 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
67 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
72 #define COMPILE_TEMPLATE_MMX2 0
73 #define RENAME(a) a ## _MMX
74 #include "swscale_template.c"
80 #undef COMPILE_TEMPLATE_MMX2
81 #define COMPILE_TEMPLATE_MMX2 1
82 #define RENAME(a) a ## _MMX2
83 #include "swscale_template.c"
86 void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
87 int lastInLumBuf, int lastInChrBuf)
89 const int dstH= c->dstH;
90 const int flags= c->flags;
91 int16_t **lumPixBuf= c->lumPixBuf;
92 int16_t **chrUPixBuf= c->chrUPixBuf;
93 int16_t **alpPixBuf= c->alpPixBuf;
94 const int vLumBufSize= c->vLumBufSize;
95 const int vChrBufSize= c->vChrBufSize;
96 int16_t *vLumFilterPos= c->vLumFilterPos;
97 int16_t *vChrFilterPos= c->vChrFilterPos;
98 int16_t *vLumFilter= c->vLumFilter;
99 int16_t *vChrFilter= c->vChrFilter;
100 int32_t *lumMmxFilter= c->lumMmxFilter;
101 int32_t *chrMmxFilter= c->chrMmxFilter;
102 int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
103 const int vLumFilterSize= c->vLumFilterSize;
104 const int vChrFilterSize= c->vChrFilterSize;
105 const int chrDstY= dstY>>c->chrDstVSubSample;
106 const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
107 const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
109 c->blueDither= ff_dither8[dstY&1];
110 if (c->dstFormat == PIX_FMT_RGB555 || c->dstFormat == PIX_FMT_BGR555)
111 c->greenDither= ff_dither8[dstY&1];
113 c->greenDither= ff_dither4[dstY&1];
114 c->redDither= ff_dither8[(dstY+1)&1];
115 if (dstY < dstH - 2) {
116 const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
117 const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
118 const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
121 if (firstLumSrcY < 0 || firstLumSrcY + vLumFilterSize > c->srcH) {
122 const int16_t **tmpY = (const int16_t **) lumPixBuf + 2 * vLumBufSize;
123 int neg = -firstLumSrcY, i, end = FFMIN(c->srcH - firstLumSrcY, vLumFilterSize);
124 for (i = 0; i < neg; i++)
125 tmpY[i] = lumSrcPtr[neg];
126 for ( ; i < end; i++)
127 tmpY[i] = lumSrcPtr[i];
128 for ( ; i < vLumFilterSize; i++)
133 const int16_t **tmpA = (const int16_t **) alpPixBuf + 2 * vLumBufSize;
134 for (i = 0; i < neg; i++)
135 tmpA[i] = alpSrcPtr[neg];
136 for ( ; i < end; i++)
137 tmpA[i] = alpSrcPtr[i];
138 for ( ; i < vLumFilterSize; i++)
139 tmpA[i] = tmpA[i - 1];
143 if (firstChrSrcY < 0 || firstChrSrcY + vChrFilterSize > c->chrSrcH) {
144 const int16_t **tmpU = (const int16_t **) chrUPixBuf + 2 * vChrBufSize;
145 int neg = -firstChrSrcY, i, end = FFMIN(c->chrSrcH - firstChrSrcY, vChrFilterSize);
146 for (i = 0; i < neg; i++) {
147 tmpU[i] = chrUSrcPtr[neg];
149 for ( ; i < end; i++) {
150 tmpU[i] = chrUSrcPtr[i];
152 for ( ; i < vChrFilterSize; i++) {
153 tmpU[i] = tmpU[i - 1];
158 if (flags & SWS_ACCURATE_RND) {
159 int s= APCK_SIZE / 8;
160 for (i=0; i<vLumFilterSize; i+=2) {
161 *(const void**)&lumMmxFilter[s*i ]= lumSrcPtr[i ];
162 *(const void**)&lumMmxFilter[s*i+APCK_PTR2/4 ]= lumSrcPtr[i+(vLumFilterSize>1)];
163 lumMmxFilter[s*i+APCK_COEF/4 ]=
164 lumMmxFilter[s*i+APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i ]
165 + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1]<<16 : 0);
166 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
167 *(const void**)&alpMmxFilter[s*i ]= alpSrcPtr[i ];
168 *(const void**)&alpMmxFilter[s*i+APCK_PTR2/4 ]= alpSrcPtr[i+(vLumFilterSize>1)];
169 alpMmxFilter[s*i+APCK_COEF/4 ]=
170 alpMmxFilter[s*i+APCK_COEF/4+1]= lumMmxFilter[s*i+APCK_COEF/4 ];
173 for (i=0; i<vChrFilterSize; i+=2) {
174 *(const void**)&chrMmxFilter[s*i ]= chrUSrcPtr[i ];
175 *(const void**)&chrMmxFilter[s*i+APCK_PTR2/4 ]= chrUSrcPtr[i+(vChrFilterSize>1)];
176 chrMmxFilter[s*i+APCK_COEF/4 ]=
177 chrMmxFilter[s*i+APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i ]
178 + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1]<<16 : 0);
181 for (i=0; i<vLumFilterSize; i++) {
182 *(const void**)&lumMmxFilter[4*i+0]= lumSrcPtr[i];
185 ((uint16_t)vLumFilter[dstY*vLumFilterSize + i])*0x10001;
186 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
187 *(const void**)&alpMmxFilter[4*i+0]= alpSrcPtr[i];
189 alpMmxFilter[4*i+3]= lumMmxFilter[4*i+2];
192 for (i=0; i<vChrFilterSize; i++) {
193 *(const void**)&chrMmxFilter[4*i+0]= chrUSrcPtr[i];
196 ((uint16_t)vChrFilter[chrDstY*vChrFilterSize + i])*0x10001;
202 #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
203 extern void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
204 SwsContext *c, int16_t *data, \
205 int dstW, const uint8_t *src, \
206 const int16_t *filter, \
207 const int16_t *filterPos, int filterSize)
209 #define SCALE_FUNCS(filter_n, opt) \
210 SCALE_FUNC(filter_n, 8, 15, opt); \
211 SCALE_FUNC(filter_n, 9, 15, opt); \
212 SCALE_FUNC(filter_n, 10, 15, opt); \
213 SCALE_FUNC(filter_n, 16, 15, opt); \
214 SCALE_FUNC(filter_n, 8, 19, opt); \
215 SCALE_FUNC(filter_n, 9, 19, opt); \
216 SCALE_FUNC(filter_n, 10, 19, opt); \
217 SCALE_FUNC(filter_n, 16, 19, opt)
219 #define SCALE_FUNCS_MMX(opt) \
220 SCALE_FUNCS(4, opt); \
221 SCALE_FUNCS(8, opt); \
224 #define SCALE_FUNCS_SSE(opt) \
225 SCALE_FUNCS(4, opt); \
226 SCALE_FUNCS(8, opt); \
227 SCALE_FUNCS(X4, opt); \
231 SCALE_FUNCS_MMX(mmx);
233 SCALE_FUNCS_SSE(sse2);
234 SCALE_FUNCS_SSE(ssse3);
235 SCALE_FUNCS_SSE(sse4);
237 #define VSCALEX_FUNC(size, opt) \
238 extern void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
239 const int16_t **src, uint8_t *dest, int dstW, \
240 const uint8_t *dither, int offset)
241 #define VSCALEX_FUNCS(opt) \
242 VSCALEX_FUNC(8, opt); \
243 VSCALEX_FUNC(9, opt); \
244 VSCALEX_FUNC(10, opt)
251 VSCALEX_FUNC(16, sse4);
254 #define VSCALE_FUNC(size, opt) \
255 extern void ff_yuv2plane1_ ## size ## _ ## opt(const int16_t *src, uint8_t *dst, int dstW, \
256 const uint8_t *dither, int offset)
257 #define VSCALE_FUNCS(opt1, opt2) \
258 VSCALE_FUNC(8, opt1); \
259 VSCALE_FUNC(9, opt2); \
260 VSCALE_FUNC(10, opt2); \
261 VSCALE_FUNC(16, opt1)
264 VSCALE_FUNCS(mmx, mmx2);
266 VSCALE_FUNCS(sse2, sse2);
267 VSCALE_FUNC(16, sse4);
268 VSCALE_FUNCS(avx, avx);
270 #define INPUT_Y_FUNC(fmt, opt) \
271 extern void ff_ ## fmt ## ToY_ ## opt(uint8_t *dst, const uint8_t *src, \
272 int w, uint32_t *unused)
273 #define INPUT_UV_FUNC(fmt, opt) \
274 extern void ff_ ## fmt ## ToUV_ ## opt(uint8_t *dstU, uint8_t *dstV, \
275 const uint8_t *src, const uint8_t *unused1, \
276 int w, uint32_t *unused2)
277 #define INPUT_FUNC(fmt, opt) \
278 INPUT_Y_FUNC(fmt, opt); \
279 INPUT_UV_FUNC(fmt, opt)
280 #define INPUT_FUNCS(opt) \
281 INPUT_FUNC(uyvy, opt); \
282 INPUT_FUNC(yuyv, opt); \
283 INPUT_UV_FUNC(nv12, opt); \
284 INPUT_UV_FUNC(nv21, opt); \
285 INPUT_FUNC(rgba, opt); \
286 INPUT_FUNC(bgra, opt); \
287 INPUT_FUNC(argb, opt); \
288 INPUT_FUNC(abgr, opt); \
289 INPUT_FUNC(rgb24, opt); \
290 INPUT_FUNC(bgr24, opt)
299 void ff_sws_init_swScale_mmx(SwsContext *c)
301 int cpu_flags = av_get_cpu_flags();
303 if (cpu_flags & AV_CPU_FLAG_MMX)
304 sws_init_swScale_MMX(c);
306 if (cpu_flags & AV_CPU_FLAG_MMX2)
307 sws_init_swScale_MMX2(c);
311 #define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt1, opt2) do { \
312 if (c->srcBpc == 8) { \
313 hscalefn = c->dstBpc <= 10 ? ff_hscale8to15_ ## filtersize ## _ ## opt2 : \
314 ff_hscale8to19_ ## filtersize ## _ ## opt1; \
315 } else if (c->srcBpc == 9) { \
316 hscalefn = c->dstBpc <= 10 ? ff_hscale9to15_ ## filtersize ## _ ## opt2 : \
317 ff_hscale9to19_ ## filtersize ## _ ## opt1; \
318 } else if (c->srcBpc == 10) { \
319 hscalefn = c->dstBpc <= 10 ? ff_hscale10to15_ ## filtersize ## _ ## opt2 : \
320 ff_hscale10to19_ ## filtersize ## _ ## opt1; \
321 } else /* c->srcBpc == 16 */ { \
322 hscalefn = c->dstBpc <= 10 ? ff_hscale16to15_ ## filtersize ## _ ## opt2 : \
323 ff_hscale16to19_ ## filtersize ## _ ## opt1; \
326 #define ASSIGN_MMX_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
327 switch (filtersize) { \
328 case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
329 case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
330 default: ASSIGN_SCALE_FUNC2(hscalefn, X, opt1, opt2); break; \
332 #define ASSIGN_VSCALEX_FUNC(vscalefn, opt, do_16_case) \
334 case 16: do_16_case; break; \
335 case 10: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_10_ ## opt; break; \
336 case 9: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_9_ ## opt; break; \
337 default: vscalefn = ff_yuv2planeX_8_ ## opt; break; \
339 #define ASSIGN_VSCALE_FUNC(vscalefn, opt1, opt2, opt2chk) \
341 case 16: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2plane1_16_ ## opt1; break; \
342 case 10: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_10_ ## opt2; break; \
343 case 9: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_9_ ## opt2; break; \
344 default: vscalefn = ff_yuv2plane1_8_ ## opt1; break; \
346 #define case_rgb(x, X, opt) \
347 case PIX_FMT_ ## X: \
348 c->lumToYV12 = ff_ ## x ## ToY_ ## opt; \
349 if (!c->chrSrcHSubSample) \
350 c->chrToYV12 = ff_ ## x ## ToUV_ ## opt; \
353 if (cpu_flags & AV_CPU_FLAG_MMX) {
354 ASSIGN_MMX_SCALE_FUNC(c->hyScale, c->hLumFilterSize, mmx, mmx);
355 ASSIGN_MMX_SCALE_FUNC(c->hcScale, c->hChrFilterSize, mmx, mmx);
356 ASSIGN_VSCALE_FUNC(c->yuv2plane1, mmx, mmx2, cpu_flags & AV_CPU_FLAG_MMX2);
358 switch (c->srcFormat) {
360 c->lumToYV12 = ff_yuyvToY_mmx;
362 c->alpToYV12 = ff_uyvyToY_mmx;
364 case PIX_FMT_YUYV422:
365 c->lumToYV12 = ff_yuyvToY_mmx;
366 c->chrToYV12 = ff_yuyvToUV_mmx;
368 case PIX_FMT_UYVY422:
369 c->lumToYV12 = ff_uyvyToY_mmx;
370 c->chrToYV12 = ff_uyvyToUV_mmx;
373 c->chrToYV12 = ff_nv12ToUV_mmx;
376 c->chrToYV12 = ff_nv21ToUV_mmx;
378 case_rgb(rgb24, RGB24, mmx);
379 case_rgb(bgr24, BGR24, mmx);
380 case_rgb(bgra, BGRA, mmx);
381 case_rgb(rgba, RGBA, mmx);
382 case_rgb(abgr, ABGR, mmx);
383 case_rgb(argb, ARGB, mmx);
388 if (cpu_flags & AV_CPU_FLAG_MMX2) {
389 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, mmx2,);
392 #define ASSIGN_SSE_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
393 switch (filtersize) { \
394 case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
395 case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
396 default: if (filtersize & 4) ASSIGN_SCALE_FUNC2(hscalefn, X4, opt1, opt2); \
397 else ASSIGN_SCALE_FUNC2(hscalefn, X8, opt1, opt2); \
400 if (cpu_flags & AV_CPU_FLAG_SSE2) {
401 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse2, sse2);
402 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse2, sse2);
403 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse2,);
404 ASSIGN_VSCALE_FUNC(c->yuv2plane1, sse2, sse2, 1);
406 switch (c->srcFormat) {
408 c->lumToYV12 = ff_yuyvToY_sse2;
410 c->alpToYV12 = ff_uyvyToY_sse2;
412 case PIX_FMT_YUYV422:
413 c->lumToYV12 = ff_yuyvToY_sse2;
414 c->chrToYV12 = ff_yuyvToUV_sse2;
416 case PIX_FMT_UYVY422:
417 c->lumToYV12 = ff_uyvyToY_sse2;
418 c->chrToYV12 = ff_uyvyToUV_sse2;
421 c->chrToYV12 = ff_nv12ToUV_sse2;
424 c->chrToYV12 = ff_nv21ToUV_sse2;
426 case_rgb(rgb24, RGB24, sse2);
427 case_rgb(bgr24, BGR24, sse2);
428 case_rgb(bgra, BGRA, sse2);
429 case_rgb(rgba, RGBA, sse2);
430 case_rgb(abgr, ABGR, sse2);
431 case_rgb(argb, ARGB, sse2);
436 if (cpu_flags & AV_CPU_FLAG_SSSE3) {
437 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, ssse3, ssse3);
438 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, ssse3, ssse3);
439 switch (c->srcFormat) {
440 case_rgb(rgb24, RGB24, ssse3);
441 case_rgb(bgr24, BGR24, ssse3);
446 if (cpu_flags & AV_CPU_FLAG_SSE4) {
447 /* Xto15 don't need special sse4 functions */
448 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse4, ssse3);
449 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse4, ssse3);
450 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse4,
451 if (!isBE(c->dstFormat)) c->yuv2planeX = ff_yuv2planeX_16_sse4);
452 if (c->dstBpc == 16 && !isBE(c->dstFormat))
453 c->yuv2plane1 = ff_yuv2plane1_16_sse4;
456 if (cpu_flags & AV_CPU_FLAG_AVX) {
457 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, avx,);
458 ASSIGN_VSCALE_FUNC(c->yuv2plane1, avx, avx, 1);
460 switch (c->srcFormat) {
461 case PIX_FMT_YUYV422:
462 c->chrToYV12 = ff_yuyvToUV_avx;
464 case PIX_FMT_UYVY422:
465 c->chrToYV12 = ff_uyvyToUV_avx;
468 c->chrToYV12 = ff_nv12ToUV_avx;
471 c->chrToYV12 = ff_nv21ToUV_avx;
473 case_rgb(rgb24, RGB24, avx);
474 case_rgb(bgr24, BGR24, avx);
475 case_rgb(bgra, BGRA, avx);
476 case_rgb(rgba, RGBA, avx);
477 case_rgb(abgr, ABGR, avx);
478 case_rgb(argb, ARGB, avx);