2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
22 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
23 supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
24 {BGR,RGB}{1,4,8,15,16} support dithering
26 unscaled special converters (YV12=I420=IYUV, Y800=Y8)
27 YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
32 BGR24 -> BGR32 & RGB24 -> RGB32
33 BGR32 -> BGR24 & RGB32 -> RGB24
38 tested special converters (most are tested actually, but I did not write it down ...)
45 untested special converters
46 YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
47 YV12/I420 -> YV12/I420
48 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
49 BGR24 -> BGR32 & RGB24 -> RGB32
50 BGR32 -> BGR24 & RGB32 -> RGB24
61 #include "swscale_internal.h"
63 #include "libavutil/intreadwrite.h"
64 #include "libavutil/x86_cpu.h"
65 #include "libavutil/avutil.h"
66 #include "libavutil/bswap.h"
67 #include "libavutil/pixdesc.h"
73 //#define HAVE_AMD3DNOW
78 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
83 #define PI 3.14159265358979323846
86 #define isPacked(x) ( \
88 || (x)==PIX_FMT_YUYV422 \
89 || (x)==PIX_FMT_UYVY422 \
93 #define RGB2YUV_SHIFT 15
94 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
95 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
96 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
97 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
98 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
99 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
100 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
101 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
102 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
104 static const double rgb2yuv_table[8][9]={
105 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
106 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
107 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
108 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
109 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
110 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
111 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
112 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
117 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
120 more intelligent misalignment avoidance for the horizontal scaler
121 write special vertical cubic upscale version
122 optimize C code (YV12 / minmax)
123 add support for packed pixel YUV input & output
124 add support for Y8 output
125 optimize BGR24 & BGR32
126 add BGR4 output support
127 write special BGR->BGR scaler
131 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
132 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
133 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
134 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
135 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
136 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
137 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
138 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
140 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
141 0x0103010301030103LL,
142 0x0200020002000200LL,};
144 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
145 0x0602060206020602LL,
146 0x0004000400040004LL,};
148 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
149 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
150 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
151 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
152 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
153 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
155 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
156 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
157 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
160 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
161 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
162 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
164 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
165 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
166 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
167 #endif /* FAST_BGR2YV12 */
168 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
169 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
170 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
172 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
173 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
174 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
175 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
176 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
178 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
179 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
180 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
183 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
185 #endif /* ARCH_X86 */
187 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
188 { 1, 3, 1, 3, 1, 3, 1, 3, },
189 { 2, 0, 2, 0, 2, 0, 2, 0, },
192 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
193 { 6, 2, 6, 2, 6, 2, 6, 2, },
194 { 0, 4, 0, 4, 0, 4, 0, 4, },
197 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
198 { 8, 4, 11, 7, 8, 4, 11, 7, },
199 { 2, 14, 1, 13, 2, 14, 1, 13, },
200 { 10, 6, 9, 5, 10, 6, 9, 5, },
201 { 0, 12, 3, 15, 0, 12, 3, 15, },
204 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
205 { 17, 9, 23, 15, 16, 8, 22, 14, },
206 { 5, 29, 3, 27, 4, 28, 2, 26, },
207 { 21, 13, 19, 11, 20, 12, 18, 10, },
208 { 0, 24, 6, 30, 1, 25, 7, 31, },
209 { 16, 8, 22, 14, 17, 9, 23, 15, },
210 { 4, 28, 2, 26, 5, 29, 3, 27, },
211 { 20, 12, 18, 10, 21, 13, 19, 11, },
212 { 1, 25, 7, 31, 0, 24, 6, 30, },
215 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
216 { 0, 55, 14, 68, 3, 58, 17, 72, },
217 { 37, 18, 50, 32, 40, 22, 54, 35, },
218 { 9, 64, 5, 59, 13, 67, 8, 63, },
219 { 46, 27, 41, 23, 49, 31, 44, 26, },
220 { 2, 57, 16, 71, 1, 56, 15, 70, },
221 { 39, 21, 52, 34, 38, 19, 51, 33, },
222 { 11, 66, 7, 62, 10, 65, 6, 60, },
223 { 48, 30, 43, 25, 47, 29, 42, 24, },
227 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
228 {117, 62, 158, 103, 113, 58, 155, 100, },
229 { 34, 199, 21, 186, 31, 196, 17, 182, },
230 {144, 89, 131, 76, 141, 86, 127, 72, },
231 { 0, 165, 41, 206, 10, 175, 52, 217, },
232 {110, 55, 151, 96, 120, 65, 162, 107, },
233 { 28, 193, 14, 179, 38, 203, 24, 189, },
234 {138, 83, 124, 69, 148, 93, 134, 79, },
235 { 7, 172, 48, 213, 3, 168, 45, 210, },
238 // tries to correct a gamma of 1.5
239 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
240 { 0, 143, 18, 200, 2, 156, 25, 215, },
241 { 78, 28, 125, 64, 89, 36, 138, 74, },
242 { 10, 180, 3, 161, 16, 195, 8, 175, },
243 {109, 51, 93, 38, 121, 60, 105, 47, },
244 { 1, 152, 23, 210, 0, 147, 20, 205, },
245 { 85, 33, 134, 71, 81, 30, 130, 67, },
246 { 14, 190, 6, 171, 12, 185, 5, 166, },
247 {117, 57, 101, 44, 113, 54, 97, 41, },
250 // tries to correct a gamma of 2.0
251 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
252 { 0, 124, 8, 193, 0, 140, 12, 213, },
253 { 55, 14, 104, 42, 66, 19, 119, 52, },
254 { 3, 168, 1, 145, 6, 187, 3, 162, },
255 { 86, 31, 70, 21, 99, 39, 82, 28, },
256 { 0, 134, 11, 206, 0, 129, 9, 200, },
257 { 62, 17, 114, 48, 58, 16, 109, 45, },
258 { 5, 181, 2, 157, 4, 175, 1, 151, },
259 { 95, 36, 78, 26, 90, 34, 74, 24, },
262 // tries to correct a gamma of 2.5
263 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
264 { 0, 107, 3, 187, 0, 125, 6, 212, },
265 { 39, 7, 86, 28, 49, 11, 102, 36, },
266 { 1, 158, 0, 131, 3, 180, 1, 151, },
267 { 68, 19, 52, 12, 81, 25, 64, 17, },
268 { 0, 119, 5, 203, 0, 113, 4, 195, },
269 { 45, 9, 96, 33, 42, 8, 91, 30, },
270 { 2, 172, 1, 144, 2, 165, 0, 137, },
271 { 77, 23, 60, 15, 72, 21, 56, 14, },
275 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
276 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
277 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
278 int dstW, int chrDstW, int big_endian)
280 //FIXME Optimize (just quickly written not optimized..)
283 for (i = 0; i < dstW; i++) {
287 for (j = 0; j < lumFilterSize; j++)
288 val += lumSrc[j][i] * lumFilter[j];
291 AV_WB16(&dest[i], av_clip_uint16(val >> 11));
293 AV_WL16(&dest[i], av_clip_uint16(val >> 11));
298 for (i = 0; i < chrDstW; i++) {
303 for (j = 0; j < chrFilterSize; j++) {
304 u += chrSrc[j][i ] * chrFilter[j];
305 v += chrSrc[j][i + VOFW] * chrFilter[j];
309 AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
310 AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
312 AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
313 AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
318 if (CONFIG_SWSCALE_ALPHA && aDest) {
319 for (i = 0; i < dstW; i++) {
323 for (j = 0; j < lumFilterSize; j++)
324 val += alpSrc[j][i] * lumFilter[j];
327 AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
329 AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
335 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
336 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
337 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
338 enum PixelFormat dstFormat)
340 if (isBE(dstFormat)) {
341 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
342 chrFilter, chrSrc, chrFilterSize,
344 dest, uDest, vDest, aDest,
347 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
348 chrFilter, chrSrc, chrFilterSize,
350 dest, uDest, vDest, aDest,
355 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
356 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
357 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
359 //FIXME Optimize (just quickly written not optimized..)
361 for (i=0; i<dstW; i++) {
364 for (j=0; j<lumFilterSize; j++)
365 val += lumSrc[j][i] * lumFilter[j];
367 dest[i]= av_clip_uint8(val>>19);
371 for (i=0; i<chrDstW; i++) {
375 for (j=0; j<chrFilterSize; j++) {
376 u += chrSrc[j][i] * chrFilter[j];
377 v += chrSrc[j][i + VOFW] * chrFilter[j];
380 uDest[i]= av_clip_uint8(u>>19);
381 vDest[i]= av_clip_uint8(v>>19);
384 if (CONFIG_SWSCALE_ALPHA && aDest)
385 for (i=0; i<dstW; i++) {
388 for (j=0; j<lumFilterSize; j++)
389 val += alpSrc[j][i] * lumFilter[j];
391 aDest[i]= av_clip_uint8(val>>19);
396 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
397 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
398 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
400 //FIXME Optimize (just quickly written not optimized..)
402 for (i=0; i<dstW; i++) {
405 for (j=0; j<lumFilterSize; j++)
406 val += lumSrc[j][i] * lumFilter[j];
408 dest[i]= av_clip_uint8(val>>19);
414 if (dstFormat == PIX_FMT_NV12)
415 for (i=0; i<chrDstW; i++) {
419 for (j=0; j<chrFilterSize; j++) {
420 u += chrSrc[j][i] * chrFilter[j];
421 v += chrSrc[j][i + VOFW] * chrFilter[j];
424 uDest[2*i]= av_clip_uint8(u>>19);
425 uDest[2*i+1]= av_clip_uint8(v>>19);
428 for (i=0; i<chrDstW; i++) {
432 for (j=0; j<chrFilterSize; j++) {
433 u += chrSrc[j][i] * chrFilter[j];
434 v += chrSrc[j][i + VOFW] * chrFilter[j];
437 uDest[2*i]= av_clip_uint8(v>>19);
438 uDest[2*i+1]= av_clip_uint8(u>>19);
442 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
443 for (i=0; i<(dstW>>1); i++) {\
449 int av_unused A1, A2;\
450 type av_unused *r, *b, *g;\
453 for (j=0; j<lumFilterSize; j++) {\
454 Y1 += lumSrc[j][i2] * lumFilter[j];\
455 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
457 for (j=0; j<chrFilterSize; j++) {\
458 U += chrSrc[j][i] * chrFilter[j];\
459 V += chrSrc[j][i+VOFW] * chrFilter[j];\
468 for (j=0; j<lumFilterSize; j++) {\
469 A1 += alpSrc[j][i2 ] * lumFilter[j];\
470 A2 += alpSrc[j][i2+1] * lumFilter[j];\
476 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
477 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
478 if ((Y1|Y2|U|V)&256) {\
479 if (Y1>255) Y1=255; \
480 else if (Y1<0)Y1=0; \
481 if (Y2>255) Y2=255; \
482 else if (Y2<0)Y2=0; \
488 if (alpha && ((A1|A2)&256)) {\
489 A1=av_clip_uint8(A1);\
490 A2=av_clip_uint8(A2);\
493 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
494 for (i=0; i<dstW; i++) {\
502 for (j=0; j<lumFilterSize; j++) {\
503 Y += lumSrc[j][i ] * lumFilter[j];\
505 for (j=0; j<chrFilterSize; j++) {\
506 U += chrSrc[j][i ] * chrFilter[j];\
507 V += chrSrc[j][i+VOFW] * chrFilter[j];\
514 for (j=0; j<lumFilterSize; j++)\
515 A += alpSrc[j][i ] * lumFilter[j];\
518 A = av_clip_uint8(A);\
521 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
522 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
523 Y-= c->yuv2rgb_y_offset;\
524 Y*= c->yuv2rgb_y_coeff;\
526 R= Y + V*c->yuv2rgb_v2r_coeff;\
527 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
528 B= Y + U*c->yuv2rgb_u2b_coeff;\
529 if ((R|G|B)&(0xC0000000)) {\
530 if (R>=(256<<22)) R=(256<<22)-1; \
532 if (G>=(256<<22)) G=(256<<22)-1; \
534 if (B>=(256<<22)) B=(256<<22)-1; \
538 #define YSCALE_YUV_2_GRAY16_C \
539 for (i=0; i<(dstW>>1); i++) {\
548 for (j=0; j<lumFilterSize; j++) {\
549 Y1 += lumSrc[j][i2] * lumFilter[j];\
550 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
554 if ((Y1|Y2|U|V)&65536) {\
555 if (Y1>65535) Y1=65535; \
556 else if (Y1<0)Y1=0; \
557 if (Y2>65535) Y2=65535; \
558 else if (Y2<0)Y2=0; \
561 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
562 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
563 r = (type *)c->table_rV[V]; \
564 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
565 b = (type *)c->table_bU[U];
567 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
568 for (i=0; i<(dstW>>1); i++) { \
570 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
571 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
572 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
573 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
574 type av_unused *r, *b, *g; \
575 int av_unused A1, A2; \
577 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
578 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
581 #define YSCALE_YUV_2_GRAY16_2_C \
582 for (i=0; i<(dstW>>1); i++) { \
584 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
585 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
587 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
588 YSCALE_YUV_2_PACKED2_C(type,alpha)\
589 r = (type *)c->table_rV[V];\
590 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
591 b = (type *)c->table_bU[U];
593 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
594 for (i=0; i<(dstW>>1); i++) {\
596 int Y1= buf0[i2 ]>>7;\
597 int Y2= buf0[i2+1]>>7;\
598 int U= (uvbuf1[i ])>>7;\
599 int V= (uvbuf1[i+VOFW])>>7;\
600 type av_unused *r, *b, *g;\
601 int av_unused A1, A2;\
607 #define YSCALE_YUV_2_GRAY16_1_C \
608 for (i=0; i<(dstW>>1); i++) {\
610 int Y1= buf0[i2 ]<<1;\
611 int Y2= buf0[i2+1]<<1;
613 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
614 YSCALE_YUV_2_PACKED1_C(type,alpha)\
615 r = (type *)c->table_rV[V];\
616 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
617 b = (type *)c->table_bU[U];
619 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
620 for (i=0; i<(dstW>>1); i++) {\
622 int Y1= buf0[i2 ]>>7;\
623 int Y2= buf0[i2+1]>>7;\
624 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
625 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
626 type av_unused *r, *b, *g;\
627 int av_unused A1, A2;\
633 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
634 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
635 r = (type *)c->table_rV[V];\
636 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
637 b = (type *)c->table_bU[U];
639 #define YSCALE_YUV_2_MONO2_C \
640 const uint8_t * const d128=dither_8x8_220[y&7];\
641 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
642 for (i=0; i<dstW-7; i+=8) {\
644 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
645 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
646 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
647 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
648 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
649 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
650 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
651 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
652 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
656 #define YSCALE_YUV_2_MONOX_C \
657 const uint8_t * const d128=dither_8x8_220[y&7];\
658 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
660 for (i=0; i<dstW-1; i+=2) {\
665 for (j=0; j<lumFilterSize; j++) {\
666 Y1 += lumSrc[j][i] * lumFilter[j];\
667 Y2 += lumSrc[j][i+1] * lumFilter[j];\
677 acc+= acc + g[Y1+d128[(i+0)&7]];\
678 acc+= acc + g[Y2+d128[(i+1)&7]];\
680 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
685 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
686 switch(c->dstFormat) {\
687 case PIX_FMT_RGB48BE:\
688 case PIX_FMT_RGB48LE:\
690 ((uint8_t*)dest)[ 0]= r[Y1];\
691 ((uint8_t*)dest)[ 1]= r[Y1];\
692 ((uint8_t*)dest)[ 2]= g[Y1];\
693 ((uint8_t*)dest)[ 3]= g[Y1];\
694 ((uint8_t*)dest)[ 4]= b[Y1];\
695 ((uint8_t*)dest)[ 5]= b[Y1];\
696 ((uint8_t*)dest)[ 6]= r[Y2];\
697 ((uint8_t*)dest)[ 7]= r[Y2];\
698 ((uint8_t*)dest)[ 8]= g[Y2];\
699 ((uint8_t*)dest)[ 9]= g[Y2];\
700 ((uint8_t*)dest)[10]= b[Y2];\
701 ((uint8_t*)dest)[11]= b[Y2];\
708 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
709 func(uint32_t,needAlpha)\
710 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
711 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
714 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
716 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
717 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
721 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
722 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
730 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
731 func(uint32_t,needAlpha)\
732 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
733 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
736 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
738 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
739 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
743 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
744 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
751 ((uint8_t*)dest)[0]= r[Y1];\
752 ((uint8_t*)dest)[1]= g[Y1];\
753 ((uint8_t*)dest)[2]= b[Y1];\
754 ((uint8_t*)dest)[3]= r[Y2];\
755 ((uint8_t*)dest)[4]= g[Y2];\
756 ((uint8_t*)dest)[5]= b[Y2];\
762 ((uint8_t*)dest)[0]= b[Y1];\
763 ((uint8_t*)dest)[1]= g[Y1];\
764 ((uint8_t*)dest)[2]= r[Y1];\
765 ((uint8_t*)dest)[3]= b[Y2];\
766 ((uint8_t*)dest)[4]= g[Y2];\
767 ((uint8_t*)dest)[5]= r[Y2];\
771 case PIX_FMT_RGB565BE:\
772 case PIX_FMT_RGB565LE:\
773 case PIX_FMT_BGR565BE:\
774 case PIX_FMT_BGR565LE:\
776 const int dr1= dither_2x2_8[y&1 ][0];\
777 const int dg1= dither_2x2_4[y&1 ][0];\
778 const int db1= dither_2x2_8[(y&1)^1][0];\
779 const int dr2= dither_2x2_8[y&1 ][1];\
780 const int dg2= dither_2x2_4[y&1 ][1];\
781 const int db2= dither_2x2_8[(y&1)^1][1];\
783 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
784 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
788 case PIX_FMT_RGB555BE:\
789 case PIX_FMT_RGB555LE:\
790 case PIX_FMT_BGR555BE:\
791 case PIX_FMT_BGR555LE:\
793 const int dr1= dither_2x2_8[y&1 ][0];\
794 const int dg1= dither_2x2_8[y&1 ][1];\
795 const int db1= dither_2x2_8[(y&1)^1][0];\
796 const int dr2= dither_2x2_8[y&1 ][1];\
797 const int dg2= dither_2x2_8[y&1 ][0];\
798 const int db2= dither_2x2_8[(y&1)^1][1];\
800 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
801 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
805 case PIX_FMT_RGB444BE:\
806 case PIX_FMT_RGB444LE:\
807 case PIX_FMT_BGR444BE:\
808 case PIX_FMT_BGR444LE:\
810 const int dr1= dither_4x4_16[y&3 ][0];\
811 const int dg1= dither_4x4_16[y&3 ][1];\
812 const int db1= dither_4x4_16[(y&3)^3][0];\
813 const int dr2= dither_4x4_16[y&3 ][1];\
814 const int dg2= dither_4x4_16[y&3 ][0];\
815 const int db2= dither_4x4_16[(y&3)^3][1];\
817 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
818 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
825 const uint8_t * const d64= dither_8x8_73[y&7];\
826 const uint8_t * const d32= dither_8x8_32[y&7];\
828 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
829 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
836 const uint8_t * const d64= dither_8x8_73 [y&7];\
837 const uint8_t * const d128=dither_8x8_220[y&7];\
839 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
840 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
844 case PIX_FMT_RGB4_BYTE:\
845 case PIX_FMT_BGR4_BYTE:\
847 const uint8_t * const d64= dither_8x8_73 [y&7];\
848 const uint8_t * const d128=dither_8x8_220[y&7];\
850 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
851 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
855 case PIX_FMT_MONOBLACK:\
856 case PIX_FMT_MONOWHITE:\
861 case PIX_FMT_YUYV422:\
863 ((uint8_t*)dest)[2*i2+0]= Y1;\
864 ((uint8_t*)dest)[2*i2+1]= U;\
865 ((uint8_t*)dest)[2*i2+2]= Y2;\
866 ((uint8_t*)dest)[2*i2+3]= V;\
869 case PIX_FMT_UYVY422:\
871 ((uint8_t*)dest)[2*i2+0]= U;\
872 ((uint8_t*)dest)[2*i2+1]= Y1;\
873 ((uint8_t*)dest)[2*i2+2]= V;\
874 ((uint8_t*)dest)[2*i2+3]= Y2;\
877 case PIX_FMT_GRAY16BE:\
879 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
880 ((uint8_t*)dest)[2*i2+1]= Y1;\
881 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
882 ((uint8_t*)dest)[2*i2+3]= Y2;\
885 case PIX_FMT_GRAY16LE:\
887 ((uint8_t*)dest)[2*i2+0]= Y1;\
888 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
889 ((uint8_t*)dest)[2*i2+2]= Y2;\
890 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
895 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
896 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
897 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
900 YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
903 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
904 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
905 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
908 int step= c->dstFormatBpp/8;
911 switch(c->dstFormat) {
919 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
920 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
921 dest[aidx]= needAlpha ? A : 255;
928 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
929 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
937 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
954 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
955 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
956 dest[aidx]= needAlpha ? A : 255;
963 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
964 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
972 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
987 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
990 uint8_t *ptr = plane + stride*y;
991 for (i=0; i<height; i++) {
992 memset(ptr, val, width);
997 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width,
1001 for (i = 0; i < width; i++) {
1006 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1010 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1011 const uint8_t *src1, const uint8_t *src2,
1012 int width, uint32_t *unused)
1016 for (i = 0; i < width; i++) {
1017 int r = src1[6*i + 0];
1018 int g = src1[6*i + 2];
1019 int b = src1[6*i + 4];
1021 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1022 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1026 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1027 const uint8_t *src1, const uint8_t *src2,
1028 int width, uint32_t *unused)
1032 for (i = 0; i < width; i++) {
1033 int r= src1[12*i + 0] + src1[12*i + 6];
1034 int g= src1[12*i + 2] + src1[12*i + 8];
1035 int b= src1[12*i + 4] + src1[12*i + 10];
1037 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1038 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1042 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1043 static inline void name(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)\
1046 for (i=0; i<width; i++) {\
1047 int b= (((const type*)src)[i]>>shb)&maskb;\
1048 int g= (((const type*)src)[i]>>shg)&maskg;\
1049 int r= (((const type*)src)[i]>>shr)&maskr;\
1051 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1055 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1056 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1057 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1058 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1059 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1060 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1062 static inline void abgrToA(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1065 for (i=0; i<width; i++) {
1070 #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
1071 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, int width, uint32_t *unused)\
1074 for (i=0; i<width; i++) {\
1075 int b= (((const type*)src)[i]&maskb)>>shb;\
1076 int g= (((const type*)src)[i]&maskg)>>shg;\
1077 int r= (((const type*)src)[i]&maskr)>>shr;\
1079 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1080 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1083 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, int width, uint32_t *unused)\
1086 for (i=0; i<width; i++) {\
1087 int pix0= ((const type*)src)[2*i+0];\
1088 int pix1= ((const type*)src)[2*i+1];\
1089 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1090 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1091 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1092 g&= maskg|(2*maskg);\
1096 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1097 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1101 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1102 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1103 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1104 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1105 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1106 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1108 static inline void palToY(uint8_t *dst, const uint8_t *src, int width, uint32_t *pal)
1111 for (i=0; i<width; i++) {
1114 dst[i]= pal[d] & 0xFF;
1118 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1119 const uint8_t *src1, const uint8_t *src2,
1120 int width, uint32_t *pal)
1123 assert(src1 == src2);
1124 for (i=0; i<width; i++) {
1125 int p= pal[src1[i]];
1132 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1135 for (i=0; i<width/8; i++) {
1138 dst[8*i+j]= ((d>>(7-j))&1)*255;
1142 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, int width, uint32_t *unused)
1145 for (i=0; i<width/8; i++) {
1148 dst[8*i+j]= ((d>>(7-j))&1)*255;
1152 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1154 #if (!HAVE_MMX && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
1160 #define COMPILE_ALTIVEC
1166 #if (HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT
1170 #if HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT
1171 #define COMPILE_MMX2
1174 #if (HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT
1175 #define COMPILE_3DNOW
1179 #define COMPILE_TEMPLATE_MMX 0
1180 #define COMPILE_TEMPLATE_MMX2 0
1181 #define COMPILE_TEMPLATE_AMD3DNOW 0
1182 #define COMPILE_TEMPLATE_ALTIVEC 0
1185 #define RENAME(a) a ## _C
1186 #include "swscale_template.c"
1189 #ifdef COMPILE_ALTIVEC
1191 #undef COMPILE_TEMPLATE_ALTIVEC
1192 #define COMPILE_TEMPLATE_ALTIVEC 1
1193 #define RENAME(a) a ## _altivec
1194 #include "swscale_template.c"
1202 #undef COMPILE_TEMPLATE_MMX
1203 #undef COMPILE_TEMPLATE_MMX2
1204 #undef COMPILE_TEMPLATE_AMD3DNOW
1205 #define COMPILE_TEMPLATE_MMX 1
1206 #define COMPILE_TEMPLATE_MMX2 0
1207 #define COMPILE_TEMPLATE_AMD3DNOW 0
1208 #define RENAME(a) a ## _MMX
1209 #include "swscale_template.c"
1215 #undef COMPILE_TEMPLATE_MMX
1216 #undef COMPILE_TEMPLATE_MMX2
1217 #undef COMPILE_TEMPLATE_AMD3DNOW
1218 #define COMPILE_TEMPLATE_MMX 1
1219 #define COMPILE_TEMPLATE_MMX2 1
1220 #define COMPILE_TEMPLATE_AMD3DNOW 0
1221 #define RENAME(a) a ## _MMX2
1222 #include "swscale_template.c"
1226 #ifdef COMPILE_3DNOW
1228 #undef COMPILE_TEMPLATE_MMX
1229 #undef COMPILE_TEMPLATE_MMX2
1230 #undef COMPILE_TEMPLATE_AMD3DNOW
1231 #define COMPILE_TEMPLATE_MMX 1
1232 #define COMPILE_TEMPLATE_MMX2 0
1233 #define COMPILE_TEMPLATE_AMD3DNOW 1
1234 #define RENAME(a) a ## _3DNow
1235 #include "swscale_template.c"
1240 SwsFunc ff_getSwsFunc(SwsContext *c)
1242 #if CONFIG_RUNTIME_CPUDETECT
1243 int flags = c->flags;
1246 // ordered per speed fastest first
1247 if (flags & SWS_CPU_CAPS_MMX2) {
1248 sws_init_swScale_MMX2(c);
1249 return swScale_MMX2;
1250 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1251 sws_init_swScale_3DNow(c);
1252 return swScale_3DNow;
1253 } else if (flags & SWS_CPU_CAPS_MMX) {
1254 sws_init_swScale_MMX(c);
1257 sws_init_swScale_C(c);
1262 #ifdef COMPILE_ALTIVEC
1263 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1264 sws_init_swScale_altivec(c);
1265 return swScale_altivec;
1267 sws_init_swScale_C(c);
1271 sws_init_swScale_C(c);
1273 #endif /* ARCH_X86 */
1274 #else //CONFIG_RUNTIME_CPUDETECT
1275 #if COMPILE_TEMPLATE_MMX2
1276 sws_init_swScale_MMX2(c);
1277 return swScale_MMX2;
1278 #elif COMPILE_TEMPLATE_AMD3DNOW
1279 sws_init_swScale_3DNow(c);
1280 return swScale_3DNow;
1281 #elif COMPILE_TEMPLATE_MMX
1282 sws_init_swScale_MMX(c);
1284 #elif COMPILE_TEMPLATE_ALTIVEC
1285 sws_init_swScale_altivec(c);
1286 return swScale_altivec;
1288 sws_init_swScale_C(c);
1291 #endif //!CONFIG_RUNTIME_CPUDETECT
1294 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1295 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1297 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1299 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1300 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1303 const uint8_t *srcPtr= src[0];
1304 uint8_t *dstPtr= dst;
1305 for (i=0; i<srcSliceH; i++) {
1306 memcpy(dstPtr, srcPtr, c->srcW);
1307 srcPtr+= srcStride[0];
1308 dstPtr+= dstStride[0];
1311 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1312 if (c->dstFormat == PIX_FMT_NV12)
1313 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1315 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1320 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1321 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1323 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1325 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1330 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1331 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1333 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1335 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1340 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1341 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1343 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1345 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1350 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1351 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1353 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1355 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1360 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1361 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1363 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1364 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1365 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1367 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1370 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1375 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1376 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1378 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1379 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1380 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1382 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1387 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1388 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1390 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1391 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1392 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1394 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1397 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1402 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1403 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1405 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1406 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1407 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1409 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1414 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1415 int srcSliceH, uint8_t* dst[], int dstStride[])
1417 const enum PixelFormat srcFormat= c->srcFormat;
1418 const enum PixelFormat dstFormat= c->dstFormat;
1419 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1420 const uint8_t *palette)=NULL;
1422 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1423 const uint8_t *srcPtr= src[0];
1425 if (usePal(srcFormat)) {
1426 switch (dstFormat) {
1427 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
1428 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
1429 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1430 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1431 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
1432 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
1437 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1438 sws_format_name(srcFormat), sws_format_name(dstFormat));
1440 for (i=0; i<srcSliceH; i++) {
1441 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1442 srcPtr+= srcStride[0];
1443 dstPtr+= dstStride[0];
1450 #define isRGBA32(x) ( \
1451 (x) == PIX_FMT_ARGB \
1452 || (x) == PIX_FMT_RGBA \
1453 || (x) == PIX_FMT_BGRA \
1454 || (x) == PIX_FMT_ABGR \
1457 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1458 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1459 int srcSliceH, uint8_t* dst[], int dstStride[])
1461 const enum PixelFormat srcFormat= c->srcFormat;
1462 const enum PixelFormat dstFormat= c->dstFormat;
1463 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1464 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1465 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1466 const int dstId= c->dstFormatBpp >> 2;
1467 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1469 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1471 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1472 if ( CONV_IS(ABGR, RGBA)
1473 || CONV_IS(ARGB, BGRA)
1474 || CONV_IS(BGRA, ARGB)
1475 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1476 else if (CONV_IS(ABGR, ARGB)
1477 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1478 else if (CONV_IS(ABGR, BGRA)
1479 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1480 else if (CONV_IS(BGRA, RGBA)
1481 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1482 else if (CONV_IS(BGRA, ABGR)
1483 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1486 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1487 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1488 switch(srcId | (dstId<<4)) {
1489 case 0x34: conv= rgb16to15; break;
1490 case 0x36: conv= rgb24to15; break;
1491 case 0x38: conv= rgb32to15; break;
1492 case 0x43: conv= rgb15to16; break;
1493 case 0x46: conv= rgb24to16; break;
1494 case 0x48: conv= rgb32to16; break;
1495 case 0x63: conv= rgb15to24; break;
1496 case 0x64: conv= rgb16to24; break;
1497 case 0x68: conv= rgb32to24; break;
1498 case 0x83: conv= rgb15to32; break;
1499 case 0x84: conv= rgb16to32; break;
1500 case 0x86: conv= rgb24to32; break;
1502 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1503 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1504 switch(srcId | (dstId<<4)) {
1505 case 0x33: conv= rgb15tobgr15; break;
1506 case 0x34: conv= rgb16tobgr15; break;
1507 case 0x36: conv= rgb24tobgr15; break;
1508 case 0x38: conv= rgb32tobgr15; break;
1509 case 0x43: conv= rgb15tobgr16; break;
1510 case 0x44: conv= rgb16tobgr16; break;
1511 case 0x46: conv= rgb24tobgr16; break;
1512 case 0x48: conv= rgb32tobgr16; break;
1513 case 0x63: conv= rgb15tobgr24; break;
1514 case 0x64: conv= rgb16tobgr24; break;
1515 case 0x66: conv= rgb24tobgr24; break;
1516 case 0x68: conv= rgb32tobgr24; break;
1517 case 0x83: conv= rgb15tobgr32; break;
1518 case 0x84: conv= rgb16tobgr32; break;
1519 case 0x86: conv= rgb24tobgr32; break;
1524 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1525 sws_format_name(srcFormat), sws_format_name(dstFormat));
1527 const uint8_t *srcPtr= src[0];
1528 uint8_t *dstPtr= dst[0];
1529 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1530 srcPtr += ALT32_CORR;
1532 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1533 dstPtr += ALT32_CORR;
1535 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1536 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1539 dstPtr += dstStride[0]*srcSliceY;
1541 for (i=0; i<srcSliceH; i++) {
1542 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1543 srcPtr+= srcStride[0];
1544 dstPtr+= dstStride[0];
1551 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1552 int srcSliceH, uint8_t* dst[], int dstStride[])
1556 dst[0]+ srcSliceY *dstStride[0],
1557 dst[1]+(srcSliceY>>1)*dstStride[1],
1558 dst[2]+(srcSliceY>>1)*dstStride[2],
1560 dstStride[0], dstStride[1], srcStride[0]);
1562 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1566 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1567 int srcSliceH, uint8_t* dst[], int dstStride[])
1572 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1573 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1575 const uint8_t *srcPtr= src[0];
1576 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1578 for (i=0; i<srcSliceH; i++) {
1579 memcpy(dstPtr, srcPtr, c->srcW);
1580 srcPtr+= srcStride[0];
1581 dstPtr+= dstStride[0];
1585 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
1586 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1587 srcSliceH >> 2, srcStride[1], dstStride[1]);
1588 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1589 srcSliceH >> 2, srcStride[2], dstStride[2]);
1591 planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1592 srcSliceH >> 2, srcStride[1], dstStride[2]);
1593 planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1594 srcSliceH >> 2, srcStride[2], dstStride[1]);
1597 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1601 /* unscaled copy like stuff (assumes nearly identical formats) */
1602 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1603 int srcSliceH, uint8_t* dst[], int dstStride[])
1605 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1606 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1609 const uint8_t *srcPtr= src[0];
1610 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1613 /* universal length finder */
1614 while(length+c->srcW <= FFABS(dstStride[0])
1615 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1618 for (i=0; i<srcSliceH; i++) {
1619 memcpy(dstPtr, srcPtr, length);
1620 srcPtr+= srcStride[0];
1621 dstPtr+= dstStride[0];
1627 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1628 int srcSliceH, uint8_t* dst[], int dstStride[])
1631 for (plane=0; plane<4; plane++) {
1632 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1633 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1634 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1635 const uint8_t *srcPtr= src[plane];
1636 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1638 if (!dst[plane]) continue;
1639 // ignore palette for GRAY8
1640 if (plane == 1 && !dst[2]) continue;
1641 if (!src[plane] || (plane == 1 && !src[2])) {
1642 if(is16BPS(c->dstFormat))
1644 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1646 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1647 if (!isBE(c->srcFormat)) srcPtr++;
1648 for (i=0; i<height; i++) {
1649 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1650 srcPtr+= srcStride[plane];
1651 dstPtr+= dstStride[plane];
1653 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1654 for (i=0; i<height; i++) {
1655 for (j=0; j<length; j++) {
1656 dstPtr[ j<<1 ] = srcPtr[j];
1657 dstPtr[(j<<1)+1] = srcPtr[j];
1659 srcPtr+= srcStride[plane];
1660 dstPtr+= dstStride[plane];
1662 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1663 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1665 for (i=0; i<height; i++) {
1666 for (j=0; j<length; j++)
1667 ((uint16_t*)dstPtr)[j] = bswap_16(((const uint16_t*)srcPtr)[j]);
1668 srcPtr+= srcStride[plane];
1669 dstPtr+= dstStride[plane];
1671 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1672 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1674 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1676 for (i=0; i<height; i++) {
1677 memcpy(dstPtr, srcPtr, length);
1678 srcPtr+= srcStride[plane];
1679 dstPtr+= dstStride[plane];
1687 int ff_hardcodedcpuflags(void)
1690 #if COMPILE_TEMPLATE_MMX2
1691 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1692 #elif COMPILE_TEMPLATE_AMD3DNOW
1693 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1694 #elif COMPILE_TEMPLATE_MMX
1695 flags |= SWS_CPU_CAPS_MMX;
1696 #elif COMPILE_TEMPLATE_ALTIVEC
1697 flags |= SWS_CPU_CAPS_ALTIVEC;
1699 flags |= SWS_CPU_CAPS_BFIN;
1704 void ff_get_unscaled_swscale(SwsContext *c)
1706 const enum PixelFormat srcFormat = c->srcFormat;
1707 const enum PixelFormat dstFormat = c->dstFormat;
1708 const int flags = c->flags;
1709 const int dstH = c->dstH;
1712 needsDither= isAnyRGB(dstFormat)
1713 && c->dstFormatBpp < 24
1714 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1717 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1718 c->swScale= planarToNv12Wrapper;
1721 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1722 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1723 c->swScale= ff_yuv2rgb_get_func_ptr(c);
1726 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1727 c->swScale= yvu9ToYv12Wrapper;
1731 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1732 c->swScale= bgr24ToYv12Wrapper;
1734 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1735 if ( isAnyRGB(srcFormat)
1736 && isAnyRGB(dstFormat)
1737 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
1738 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
1739 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
1740 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
1741 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1742 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1743 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1744 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1745 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
1746 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
1747 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1748 c->swScale= rgbToRgbWrapper;
1750 if ((usePal(srcFormat) && (
1751 dstFormat == PIX_FMT_RGB32 ||
1752 dstFormat == PIX_FMT_RGB32_1 ||
1753 dstFormat == PIX_FMT_RGB24 ||
1754 dstFormat == PIX_FMT_BGR32 ||
1755 dstFormat == PIX_FMT_BGR32_1 ||
1756 dstFormat == PIX_FMT_BGR24)))
1757 c->swScale= palToRgbWrapper;
1759 if (srcFormat == PIX_FMT_YUV422P) {
1760 if (dstFormat == PIX_FMT_YUYV422)
1761 c->swScale= yuv422pToYuy2Wrapper;
1762 else if (dstFormat == PIX_FMT_UYVY422)
1763 c->swScale= yuv422pToUyvyWrapper;
1766 /* LQ converters if -sws 0 or -sws 4*/
1767 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1769 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1770 if (dstFormat == PIX_FMT_YUYV422)
1771 c->swScale= planarToYuy2Wrapper;
1772 else if (dstFormat == PIX_FMT_UYVY422)
1773 c->swScale= planarToUyvyWrapper;
1776 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1777 c->swScale= yuyvToYuv420Wrapper;
1778 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1779 c->swScale= uyvyToYuv420Wrapper;
1780 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1781 c->swScale= yuyvToYuv422Wrapper;
1782 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1783 c->swScale= uyvyToYuv422Wrapper;
1785 #ifdef COMPILE_ALTIVEC
1786 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1787 !(c->flags & SWS_BITEXACT) &&
1788 srcFormat == PIX_FMT_YUV420P) {
1789 // unscaled YV12 -> packed YUV, we want speed
1790 if (dstFormat == PIX_FMT_YUYV422)
1791 c->swScale= yv12toyuy2_unscaled_altivec;
1792 else if (dstFormat == PIX_FMT_UYVY422)
1793 c->swScale= yv12touyvy_unscaled_altivec;
1798 if ( srcFormat == dstFormat
1799 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1800 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1801 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1802 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1803 || (isGray(dstFormat) && isGray(srcFormat))
1804 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1805 && c->chrDstHSubSample == c->chrSrcHSubSample
1806 && c->chrDstVSubSample == c->chrSrcVSubSample
1807 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1808 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1810 if (isPacked(c->srcFormat))
1811 c->swScale= packedCopyWrapper;
1812 else /* Planar YUV or gray */
1813 c->swScale= planarCopyWrapper;
1816 if (flags & SWS_CPU_CAPS_BFIN)
1817 ff_bfin_get_unscaled_swscale (c);
1821 static void reset_ptr(const uint8_t* src[], int format)
1823 if(!isALPHA(format))
1825 if(!isPlanarYUV(format)) {
1828 if (!usePal(format))
1834 * swscale wrapper, so we don't need to export the SwsContext.
1835 * Assumes planar YUV to be in YUV order instead of YVU.
1837 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1838 int srcSliceH, uint8_t* const dst[], const int dstStride[])
1841 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1842 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1844 // do not mess up sliceDir if we have a "trailing" 0-size slice
1848 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1849 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1852 if (c->sliceDir == 0) {
1853 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1856 if (usePal(c->srcFormat)) {
1857 for (i=0; i<256; i++) {
1858 int p, r, g, b,y,u,v;
1859 if(c->srcFormat == PIX_FMT_PAL8) {
1860 p=((const uint32_t*)(src[1]))[i];
1864 } else if(c->srcFormat == PIX_FMT_RGB8) {
1868 } else if(c->srcFormat == PIX_FMT_BGR8) {
1872 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1876 } else if(c->srcFormat == PIX_FMT_GRAY8) {
1879 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
1884 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1885 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1886 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1887 c->pal_yuv[i]= y + (u<<8) + (v<<16);
1889 switch(c->dstFormat) {
1894 c->pal_rgb[i]= r + (g<<8) + (b<<16);
1896 case PIX_FMT_BGR32_1:
1900 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
1902 case PIX_FMT_RGB32_1:
1906 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
1913 c->pal_rgb[i]= b + (g<<8) + (r<<16);
1918 // copy strides, so they can safely be modified
1919 if (c->sliceDir == 1) {
1920 // slices go from top to bottom
1921 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
1922 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
1924 reset_ptr(src2, c->srcFormat);
1925 reset_ptr((const uint8_t**)dst2, c->dstFormat);
1927 /* reset slice direction at end of frame */
1928 if (srcSliceY + srcSliceH == c->srcH)
1931 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
1933 // slices go from bottom to top => we flip the image internally
1934 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
1935 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
1937 src2[0] += (srcSliceH-1)*srcStride[0];
1938 if (!usePal(c->srcFormat))
1939 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
1940 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
1941 src2[3] += (srcSliceH-1)*srcStride[3];
1942 dst2[0] += ( c->dstH -1)*dstStride[0];
1943 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
1944 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
1945 dst2[3] += ( c->dstH -1)*dstStride[3];
1947 reset_ptr(src2, c->srcFormat);
1948 reset_ptr((const uint8_t**)dst2, c->dstFormat);
1950 /* reset slice direction at end of frame */
1954 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
1958 #if LIBSWSCALE_VERSION_MAJOR < 1
1959 int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
1960 int srcSliceH, uint8_t* dst[], int dstStride[])
1962 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);