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"
30 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
31 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
32 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
33 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
35 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
37 0x0200020002000200LL,};
39 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
41 0x0004000400040004LL,};
43 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
44 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
45 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
46 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
47 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
48 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
50 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
51 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
52 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
55 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
56 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
57 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
59 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
60 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
61 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
62 #endif /* FAST_BGR2YV12 */
63 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
64 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
65 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
70 #define COMPILE_TEMPLATE_MMX2 0
71 #define RENAME(a) a ## _MMX
72 #include "swscale_template.c"
78 #undef COMPILE_TEMPLATE_MMX2
79 #define COMPILE_TEMPLATE_MMX2 1
80 #define RENAME(a) a ## _MMX2
81 #include "swscale_template.c"
84 void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
85 int lastInLumBuf, int lastInChrBuf)
87 const int dstH= c->dstH;
88 const int flags= c->flags;
89 int16_t **lumPixBuf= c->lumPixBuf;
90 int16_t **chrUPixBuf= c->chrUPixBuf;
91 int16_t **alpPixBuf= c->alpPixBuf;
92 const int vLumBufSize= c->vLumBufSize;
93 const int vChrBufSize= c->vChrBufSize;
94 int16_t *vLumFilterPos= c->vLumFilterPos;
95 int16_t *vChrFilterPos= c->vChrFilterPos;
96 int16_t *vLumFilter= c->vLumFilter;
97 int16_t *vChrFilter= c->vChrFilter;
98 int32_t *lumMmxFilter= c->lumMmxFilter;
99 int32_t *chrMmxFilter= c->chrMmxFilter;
100 int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
101 const int vLumFilterSize= c->vLumFilterSize;
102 const int vChrFilterSize= c->vChrFilterSize;
103 const int chrDstY= dstY>>c->chrDstVSubSample;
104 const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
105 const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
107 c->blueDither= ff_dither8[dstY&1];
108 if (c->dstFormat == PIX_FMT_RGB555 || c->dstFormat == PIX_FMT_BGR555)
109 c->greenDither= ff_dither8[dstY&1];
111 c->greenDither= ff_dither4[dstY&1];
112 c->redDither= ff_dither8[(dstY+1)&1];
113 if (dstY < dstH - 2) {
114 const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
115 const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
116 const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
118 if (flags & SWS_ACCURATE_RND) {
119 int s= APCK_SIZE / 8;
120 for (i=0; i<vLumFilterSize; i+=2) {
121 *(const void**)&lumMmxFilter[s*i ]= lumSrcPtr[i ];
122 *(const void**)&lumMmxFilter[s*i+APCK_PTR2/4 ]= lumSrcPtr[i+(vLumFilterSize>1)];
123 lumMmxFilter[s*i+APCK_COEF/4 ]=
124 lumMmxFilter[s*i+APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i ]
125 + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1]<<16 : 0);
126 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
127 *(const void**)&alpMmxFilter[s*i ]= alpSrcPtr[i ];
128 *(const void**)&alpMmxFilter[s*i+APCK_PTR2/4 ]= alpSrcPtr[i+(vLumFilterSize>1)];
129 alpMmxFilter[s*i+APCK_COEF/4 ]=
130 alpMmxFilter[s*i+APCK_COEF/4+1]= lumMmxFilter[s*i+APCK_COEF/4 ];
133 for (i=0; i<vChrFilterSize; i+=2) {
134 *(const void**)&chrMmxFilter[s*i ]= chrUSrcPtr[i ];
135 *(const void**)&chrMmxFilter[s*i+APCK_PTR2/4 ]= chrUSrcPtr[i+(vChrFilterSize>1)];
136 chrMmxFilter[s*i+APCK_COEF/4 ]=
137 chrMmxFilter[s*i+APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i ]
138 + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1]<<16 : 0);
141 for (i=0; i<vLumFilterSize; i++) {
142 *(const void**)&lumMmxFilter[4*i+0]= lumSrcPtr[i];
145 ((uint16_t)vLumFilter[dstY*vLumFilterSize + i])*0x10001;
146 if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
147 *(const void**)&alpMmxFilter[4*i+0]= alpSrcPtr[i];
149 alpMmxFilter[4*i+3]= lumMmxFilter[4*i+2];
152 for (i=0; i<vChrFilterSize; i++) {
153 *(const void**)&chrMmxFilter[4*i+0]= chrUSrcPtr[i];
156 ((uint16_t)vChrFilter[chrDstY*vChrFilterSize + i])*0x10001;
162 #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
163 extern void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
164 SwsContext *c, int16_t *data, \
165 int dstW, const uint8_t *src, \
166 const int16_t *filter, \
167 const int16_t *filterPos, int filterSize)
169 #define SCALE_FUNCS(filter_n, opt) \
170 SCALE_FUNC(filter_n, 8, 15, opt); \
171 SCALE_FUNC(filter_n, 9, 15, opt); \
172 SCALE_FUNC(filter_n, 10, 15, opt); \
173 SCALE_FUNC(filter_n, 16, 15, opt); \
174 SCALE_FUNC(filter_n, 8, 19, opt); \
175 SCALE_FUNC(filter_n, 9, 19, opt); \
176 SCALE_FUNC(filter_n, 10, 19, opt); \
177 SCALE_FUNC(filter_n, 16, 19, opt)
179 #define SCALE_FUNCS_MMX(opt) \
180 SCALE_FUNCS(4, opt); \
181 SCALE_FUNCS(8, opt); \
184 #define SCALE_FUNCS_SSE(opt) \
185 SCALE_FUNCS(4, opt); \
186 SCALE_FUNCS(8, opt); \
187 SCALE_FUNCS(X4, opt); \
191 SCALE_FUNCS_MMX(mmx);
193 SCALE_FUNCS_SSE(sse2);
194 SCALE_FUNCS_SSE(ssse3);
195 SCALE_FUNCS_SSE(sse4);
197 #define VSCALEX_FUNC(size, opt) \
198 extern void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
199 const int16_t **src, uint8_t *dest, int dstW, \
200 const uint8_t *dither, int offset)
201 #define VSCALEX_FUNCS(opt) \
202 VSCALEX_FUNC(8, opt); \
203 VSCALEX_FUNC(9, opt); \
204 VSCALEX_FUNC(10, opt)
211 VSCALEX_FUNC(16, sse4);
214 #define VSCALE_FUNC(size, opt) \
215 extern void ff_yuv2plane1_ ## size ## _ ## opt(const int16_t *src, uint8_t *dst, int dstW, \
216 const uint8_t *dither, int offset)
217 #define VSCALE_FUNCS(opt1, opt2) \
218 VSCALE_FUNC(8, opt1); \
219 VSCALE_FUNC(9, opt2); \
220 VSCALE_FUNC(10, opt2); \
221 VSCALE_FUNC(16, opt1)
224 VSCALE_FUNCS(mmx, mmx2);
226 VSCALE_FUNCS(sse2, sse2);
227 VSCALE_FUNC(16, sse4);
228 VSCALE_FUNCS(avx, avx);
230 #define INPUT_Y_FUNC(fmt, opt) \
231 extern void ff_ ## fmt ## ToY_ ## opt(uint8_t *dst, const uint8_t *src, \
232 int w, uint32_t *unused)
233 #define INPUT_UV_FUNC(fmt, opt) \
234 extern void ff_ ## fmt ## ToUV_ ## opt(uint8_t *dstU, uint8_t *dstV, \
235 const uint8_t *src, const uint8_t *unused1, \
236 int w, uint32_t *unused2)
237 #define INPUT_FUNC(fmt, opt) \
238 INPUT_Y_FUNC(fmt, opt); \
239 INPUT_UV_FUNC(fmt, opt)
240 #define INPUT_FUNCS(opt) \
241 INPUT_FUNC(uyvy, opt); \
242 INPUT_FUNC(yuyv, opt); \
243 INPUT_UV_FUNC(nv12, opt); \
244 INPUT_UV_FUNC(nv21, opt); \
245 INPUT_FUNC(rgb24, opt); \
246 INPUT_FUNC(bgr24, opt)
255 void ff_sws_init_swScale_mmx(SwsContext *c)
257 int cpu_flags = av_get_cpu_flags();
259 if (cpu_flags & AV_CPU_FLAG_MMX)
260 sws_init_swScale_MMX(c);
262 if (cpu_flags & AV_CPU_FLAG_MMX2)
263 sws_init_swScale_MMX2(c);
267 #define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt1, opt2) do { \
268 if (c->srcBpc == 8) { \
269 hscalefn = c->dstBpc <= 10 ? ff_hscale8to15_ ## filtersize ## _ ## opt2 : \
270 ff_hscale8to19_ ## filtersize ## _ ## opt1; \
271 } else if (c->srcBpc == 9) { \
272 hscalefn = c->dstBpc <= 10 ? ff_hscale9to15_ ## filtersize ## _ ## opt2 : \
273 ff_hscale9to19_ ## filtersize ## _ ## opt1; \
274 } else if (c->srcBpc == 10) { \
275 hscalefn = c->dstBpc <= 10 ? ff_hscale10to15_ ## filtersize ## _ ## opt2 : \
276 ff_hscale10to19_ ## filtersize ## _ ## opt1; \
277 } else /* c->srcBpc == 16 */ { \
278 hscalefn = c->dstBpc <= 10 ? ff_hscale16to15_ ## filtersize ## _ ## opt2 : \
279 ff_hscale16to19_ ## filtersize ## _ ## opt1; \
282 #define ASSIGN_MMX_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
283 switch (filtersize) { \
284 case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
285 case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
286 default: ASSIGN_SCALE_FUNC2(hscalefn, X, opt1, opt2); break; \
288 #define ASSIGN_VSCALEX_FUNC(vscalefn, opt, do_16_case) \
290 case 16: do_16_case; break; \
291 case 10: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_10_ ## opt; break; \
292 case 9: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_9_ ## opt; break; \
293 default: vscalefn = ff_yuv2planeX_8_ ## opt; break; \
295 #define ASSIGN_VSCALE_FUNC(vscalefn, opt1, opt2, opt2chk) \
297 case 16: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2plane1_16_ ## opt1; break; \
298 case 10: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_10_ ## opt2; break; \
299 case 9: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_9_ ## opt2; break; \
300 default: vscalefn = ff_yuv2plane1_8_ ## opt1; break; \
302 #define case_rgb(x, X, opt) \
303 case PIX_FMT_ ## X: \
304 c->lumToYV12 = ff_ ## x ## ToY_ ## opt; \
305 if (!c->chrSrcHSubSample) \
306 c->chrToYV12 = ff_ ## x ## ToUV_ ## opt; \
309 if (cpu_flags & AV_CPU_FLAG_MMX) {
310 ASSIGN_MMX_SCALE_FUNC(c->hyScale, c->hLumFilterSize, mmx, mmx);
311 ASSIGN_MMX_SCALE_FUNC(c->hcScale, c->hChrFilterSize, mmx, mmx);
312 ASSIGN_VSCALE_FUNC(c->yuv2plane1, mmx, mmx2, cpu_flags & AV_CPU_FLAG_MMX2);
314 switch (c->srcFormat) {
316 c->lumToYV12 = ff_yuyvToY_mmx;
318 c->alpToYV12 = ff_uyvyToY_mmx;
320 case PIX_FMT_YUYV422:
321 c->lumToYV12 = ff_yuyvToY_mmx;
322 c->chrToYV12 = ff_yuyvToUV_mmx;
324 case PIX_FMT_UYVY422:
325 c->lumToYV12 = ff_uyvyToY_mmx;
326 c->chrToYV12 = ff_uyvyToUV_mmx;
329 c->chrToYV12 = ff_nv12ToUV_mmx;
332 c->chrToYV12 = ff_nv21ToUV_mmx;
334 case_rgb(rgb24, RGB24, mmx);
335 case_rgb(bgr24, BGR24, mmx);
340 if (cpu_flags & AV_CPU_FLAG_MMX2) {
341 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, mmx2,);
344 #define ASSIGN_SSE_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
345 switch (filtersize) { \
346 case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
347 case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
348 default: if (filtersize & 4) ASSIGN_SCALE_FUNC2(hscalefn, X4, opt1, opt2); \
349 else ASSIGN_SCALE_FUNC2(hscalefn, X8, opt1, opt2); \
352 if (cpu_flags & AV_CPU_FLAG_SSE2) {
353 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse2, sse2);
354 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse2, sse2);
355 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse2,);
356 ASSIGN_VSCALE_FUNC(c->yuv2plane1, sse2, sse2, 1);
358 switch (c->srcFormat) {
360 c->lumToYV12 = ff_yuyvToY_sse2;
362 c->alpToYV12 = ff_uyvyToY_sse2;
364 case PIX_FMT_YUYV422:
365 c->lumToYV12 = ff_yuyvToY_sse2;
366 c->chrToYV12 = ff_yuyvToUV_sse2;
368 case PIX_FMT_UYVY422:
369 c->lumToYV12 = ff_uyvyToY_sse2;
370 c->chrToYV12 = ff_uyvyToUV_sse2;
373 c->chrToYV12 = ff_nv12ToUV_sse2;
376 c->chrToYV12 = ff_nv21ToUV_sse2;
378 case_rgb(rgb24, RGB24, sse2);
379 case_rgb(bgr24, BGR24, sse2);
384 if (cpu_flags & AV_CPU_FLAG_SSSE3) {
385 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, ssse3, ssse3);
386 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, ssse3, ssse3);
387 switch (c->srcFormat) {
388 case_rgb(rgb24, RGB24, ssse3);
389 case_rgb(bgr24, BGR24, ssse3);
394 if (cpu_flags & AV_CPU_FLAG_SSE4) {
395 /* Xto15 don't need special sse4 functions */
396 ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse4, ssse3);
397 ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse4, ssse3);
398 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse4,
399 if (!isBE(c->dstFormat)) c->yuv2planeX = ff_yuv2planeX_16_sse4);
400 if (c->dstBpc == 16 && !isBE(c->dstFormat))
401 c->yuv2plane1 = ff_yuv2plane1_16_sse4;
404 if (cpu_flags & AV_CPU_FLAG_AVX) {
405 ASSIGN_VSCALEX_FUNC(c->yuv2planeX, avx,);
406 ASSIGN_VSCALE_FUNC(c->yuv2plane1, avx, avx, 1);
408 switch (c->srcFormat) {
409 case PIX_FMT_YUYV422:
410 c->chrToYV12 = ff_yuyvToUV_avx;
412 case PIX_FMT_UYVY422:
413 c->chrToYV12 = ff_uyvyToUV_avx;
416 c->chrToYV12 = ff_nv12ToUV_avx;
419 c->chrToYV12 = ff_nv21ToUV_avx;
421 case_rgb(rgb24, RGB24, avx);
422 case_rgb(bgr24, BGR24, avx);