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
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/mathematics.h"
67 #include "libavutil/bswap.h"
68 #include "libavutil/pixdesc.h"
74 //#define HAVE_AMD3DNOW
79 #define isPacked(x) ( \
81 || (x)==PIX_FMT_YUYV422 \
82 || (x)==PIX_FMT_UYVY422 \
83 || (x)==PIX_FMT_Y400A \
87 #define RGB2YUV_SHIFT 15
88 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
89 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
90 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
91 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
92 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
93 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
94 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
95 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
96 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
98 static const double rgb2yuv_table[8][9]={
99 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
100 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
101 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
102 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
103 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
104 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
105 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
106 {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
111 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
114 more intelligent misalignment avoidance for the horizontal scaler
115 write special vertical cubic upscale version
116 optimize C code (YV12 / minmax)
117 add support for packed pixel YUV input & output
118 add support for Y8 output
119 optimize BGR24 & BGR32
120 add BGR4 output support
121 write special BGR->BGR scaler
125 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
126 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
127 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
128 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
129 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
130 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
131 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
132 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
134 const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
135 0x0103010301030103LL,
136 0x0200020002000200LL,};
138 const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
139 0x0602060206020602LL,
140 0x0004000400040004LL,};
142 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
143 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
144 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
145 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
146 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
147 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
149 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
150 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
151 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
154 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
155 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
156 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
158 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
159 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
160 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
161 #endif /* FAST_BGR2YV12 */
162 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
163 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
164 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
166 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
167 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
168 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
169 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
170 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
172 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
173 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
174 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
177 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
179 #endif /* ARCH_X86 */
181 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
182 { 1, 3, 1, 3, 1, 3, 1, 3, },
183 { 2, 0, 2, 0, 2, 0, 2, 0, },
186 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
187 { 6, 2, 6, 2, 6, 2, 6, 2, },
188 { 0, 4, 0, 4, 0, 4, 0, 4, },
191 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
192 { 8, 4, 11, 7, 8, 4, 11, 7, },
193 { 2, 14, 1, 13, 2, 14, 1, 13, },
194 { 10, 6, 9, 5, 10, 6, 9, 5, },
195 { 0, 12, 3, 15, 0, 12, 3, 15, },
198 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
199 { 17, 9, 23, 15, 16, 8, 22, 14, },
200 { 5, 29, 3, 27, 4, 28, 2, 26, },
201 { 21, 13, 19, 11, 20, 12, 18, 10, },
202 { 0, 24, 6, 30, 1, 25, 7, 31, },
203 { 16, 8, 22, 14, 17, 9, 23, 15, },
204 { 4, 28, 2, 26, 5, 29, 3, 27, },
205 { 20, 12, 18, 10, 21, 13, 19, 11, },
206 { 1, 25, 7, 31, 0, 24, 6, 30, },
209 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
210 { 0, 55, 14, 68, 3, 58, 17, 72, },
211 { 37, 18, 50, 32, 40, 22, 54, 35, },
212 { 9, 64, 5, 59, 13, 67, 8, 63, },
213 { 46, 27, 41, 23, 49, 31, 44, 26, },
214 { 2, 57, 16, 71, 1, 56, 15, 70, },
215 { 39, 21, 52, 34, 38, 19, 51, 33, },
216 { 11, 66, 7, 62, 10, 65, 6, 60, },
217 { 48, 30, 43, 25, 47, 29, 42, 24, },
221 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
222 {117, 62, 158, 103, 113, 58, 155, 100, },
223 { 34, 199, 21, 186, 31, 196, 17, 182, },
224 {144, 89, 131, 76, 141, 86, 127, 72, },
225 { 0, 165, 41, 206, 10, 175, 52, 217, },
226 {110, 55, 151, 96, 120, 65, 162, 107, },
227 { 28, 193, 14, 179, 38, 203, 24, 189, },
228 {138, 83, 124, 69, 148, 93, 134, 79, },
229 { 7, 172, 48, 213, 3, 168, 45, 210, },
232 // tries to correct a gamma of 1.5
233 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
234 { 0, 143, 18, 200, 2, 156, 25, 215, },
235 { 78, 28, 125, 64, 89, 36, 138, 74, },
236 { 10, 180, 3, 161, 16, 195, 8, 175, },
237 {109, 51, 93, 38, 121, 60, 105, 47, },
238 { 1, 152, 23, 210, 0, 147, 20, 205, },
239 { 85, 33, 134, 71, 81, 30, 130, 67, },
240 { 14, 190, 6, 171, 12, 185, 5, 166, },
241 {117, 57, 101, 44, 113, 54, 97, 41, },
244 // tries to correct a gamma of 2.0
245 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
246 { 0, 124, 8, 193, 0, 140, 12, 213, },
247 { 55, 14, 104, 42, 66, 19, 119, 52, },
248 { 3, 168, 1, 145, 6, 187, 3, 162, },
249 { 86, 31, 70, 21, 99, 39, 82, 28, },
250 { 0, 134, 11, 206, 0, 129, 9, 200, },
251 { 62, 17, 114, 48, 58, 16, 109, 45, },
252 { 5, 181, 2, 157, 4, 175, 1, 151, },
253 { 95, 36, 78, 26, 90, 34, 74, 24, },
256 // tries to correct a gamma of 2.5
257 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
258 { 0, 107, 3, 187, 0, 125, 6, 212, },
259 { 39, 7, 86, 28, 49, 11, 102, 36, },
260 { 1, 158, 0, 131, 3, 180, 1, 151, },
261 { 68, 19, 52, 12, 81, 25, 64, 17, },
262 { 0, 119, 5, 203, 0, 113, 4, 195, },
263 { 45, 9, 96, 33, 42, 8, 91, 30, },
264 { 2, 172, 1, 144, 2, 165, 0, 137, },
265 { 77, 23, 60, 15, 72, 21, 56, 14, },
269 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
270 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
271 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
272 int dstW, int chrDstW, int big_endian)
274 //FIXME Optimize (just quickly written not optimized..)
277 for (i = 0; i < dstW; i++) {
281 for (j = 0; j < lumFilterSize; j++)
282 val += lumSrc[j][i] * lumFilter[j];
285 AV_WB16(&dest[i], av_clip_uint16(val >> 11));
287 AV_WL16(&dest[i], av_clip_uint16(val >> 11));
292 for (i = 0; i < chrDstW; i++) {
297 for (j = 0; j < chrFilterSize; j++) {
298 u += chrSrc[j][i ] * chrFilter[j];
299 v += chrSrc[j][i + VOFW] * chrFilter[j];
303 AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
304 AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
306 AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
307 AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
312 if (CONFIG_SWSCALE_ALPHA && aDest) {
313 for (i = 0; i < dstW; i++) {
317 for (j = 0; j < lumFilterSize; j++)
318 val += alpSrc[j][i] * lumFilter[j];
321 AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
323 AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
329 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
330 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
331 const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
332 enum PixelFormat dstFormat)
334 if (isBE(dstFormat)) {
335 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
336 chrFilter, chrSrc, chrFilterSize,
338 dest, uDest, vDest, aDest,
341 yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
342 chrFilter, chrSrc, chrFilterSize,
344 dest, uDest, vDest, aDest,
349 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
350 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
351 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
353 //FIXME Optimize (just quickly written not optimized..)
355 for (i=0; i<dstW; i++) {
358 for (j=0; j<lumFilterSize; j++)
359 val += lumSrc[j][i] * lumFilter[j];
361 dest[i]= av_clip_uint8(val>>19);
365 for (i=0; i<chrDstW; i++) {
369 for (j=0; j<chrFilterSize; j++) {
370 u += chrSrc[j][i] * chrFilter[j];
371 v += chrSrc[j][i + VOFW] * chrFilter[j];
374 uDest[i]= av_clip_uint8(u>>19);
375 vDest[i]= av_clip_uint8(v>>19);
378 if (CONFIG_SWSCALE_ALPHA && aDest)
379 for (i=0; i<dstW; i++) {
382 for (j=0; j<lumFilterSize; j++)
383 val += alpSrc[j][i] * lumFilter[j];
385 aDest[i]= av_clip_uint8(val>>19);
390 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
391 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
392 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
394 //FIXME Optimize (just quickly written not optimized..)
396 for (i=0; i<dstW; i++) {
399 for (j=0; j<lumFilterSize; j++)
400 val += lumSrc[j][i] * lumFilter[j];
402 dest[i]= av_clip_uint8(val>>19);
408 if (dstFormat == PIX_FMT_NV12)
409 for (i=0; i<chrDstW; i++) {
413 for (j=0; j<chrFilterSize; j++) {
414 u += chrSrc[j][i] * chrFilter[j];
415 v += chrSrc[j][i + VOFW] * chrFilter[j];
418 uDest[2*i]= av_clip_uint8(u>>19);
419 uDest[2*i+1]= av_clip_uint8(v>>19);
422 for (i=0; i<chrDstW; i++) {
426 for (j=0; j<chrFilterSize; j++) {
427 u += chrSrc[j][i] * chrFilter[j];
428 v += chrSrc[j][i + VOFW] * chrFilter[j];
431 uDest[2*i]= av_clip_uint8(v>>19);
432 uDest[2*i+1]= av_clip_uint8(u>>19);
436 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
437 for (i=0; i<(dstW>>1); i++) {\
443 int av_unused A1, A2;\
444 type av_unused *r, *b, *g;\
447 for (j=0; j<lumFilterSize; j++) {\
448 Y1 += lumSrc[j][i2] * lumFilter[j];\
449 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
451 for (j=0; j<chrFilterSize; j++) {\
452 U += chrSrc[j][i] * chrFilter[j];\
453 V += chrSrc[j][i+VOFW] * chrFilter[j];\
462 for (j=0; j<lumFilterSize; j++) {\
463 A1 += alpSrc[j][i2 ] * lumFilter[j];\
464 A2 += alpSrc[j][i2+1] * lumFilter[j];\
470 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
471 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
472 if ((Y1|Y2|U|V)&256) {\
473 if (Y1>255) Y1=255; \
474 else if (Y1<0)Y1=0; \
475 if (Y2>255) Y2=255; \
476 else if (Y2<0)Y2=0; \
482 if (alpha && ((A1|A2)&256)) {\
483 A1=av_clip_uint8(A1);\
484 A2=av_clip_uint8(A2);\
487 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
488 for (i=0; i<dstW; i++) {\
496 for (j=0; j<lumFilterSize; j++) {\
497 Y += lumSrc[j][i ] * lumFilter[j];\
499 for (j=0; j<chrFilterSize; j++) {\
500 U += chrSrc[j][i ] * chrFilter[j];\
501 V += chrSrc[j][i+VOFW] * chrFilter[j];\
508 for (j=0; j<lumFilterSize; j++)\
509 A += alpSrc[j][i ] * lumFilter[j];\
512 A = av_clip_uint8(A);\
515 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
516 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
517 Y-= c->yuv2rgb_y_offset;\
518 Y*= c->yuv2rgb_y_coeff;\
520 R= Y + V*c->yuv2rgb_v2r_coeff;\
521 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
522 B= Y + U*c->yuv2rgb_u2b_coeff;\
523 if ((R|G|B)&(0xC0000000)) {\
524 if (R>=(256<<22)) R=(256<<22)-1; \
526 if (G>=(256<<22)) G=(256<<22)-1; \
528 if (B>=(256<<22)) B=(256<<22)-1; \
532 #define YSCALE_YUV_2_GRAY16_C \
533 for (i=0; i<(dstW>>1); i++) {\
542 for (j=0; j<lumFilterSize; j++) {\
543 Y1 += lumSrc[j][i2] * lumFilter[j];\
544 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
548 if ((Y1|Y2|U|V)&65536) {\
549 if (Y1>65535) Y1=65535; \
550 else if (Y1<0)Y1=0; \
551 if (Y2>65535) Y2=65535; \
552 else if (Y2<0)Y2=0; \
555 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
556 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
557 r = (type *)c->table_rV[V]; \
558 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
559 b = (type *)c->table_bU[U];
561 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
562 for (i=0; i<(dstW>>1); i++) { \
564 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
565 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
566 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
567 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
568 type av_unused *r, *b, *g; \
569 int av_unused A1, A2; \
571 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
572 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
575 #define YSCALE_YUV_2_GRAY16_2_C \
576 for (i=0; i<(dstW>>1); i++) { \
578 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
579 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
581 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
582 YSCALE_YUV_2_PACKED2_C(type,alpha)\
583 r = (type *)c->table_rV[V];\
584 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
585 b = (type *)c->table_bU[U];
587 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
588 for (i=0; i<(dstW>>1); i++) {\
590 int Y1= buf0[i2 ]>>7;\
591 int Y2= buf0[i2+1]>>7;\
592 int U= (uvbuf1[i ])>>7;\
593 int V= (uvbuf1[i+VOFW])>>7;\
594 type av_unused *r, *b, *g;\
595 int av_unused A1, A2;\
601 #define YSCALE_YUV_2_GRAY16_1_C \
602 for (i=0; i<(dstW>>1); i++) {\
604 int Y1= buf0[i2 ]<<1;\
605 int Y2= buf0[i2+1]<<1;
607 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
608 YSCALE_YUV_2_PACKED1_C(type,alpha)\
609 r = (type *)c->table_rV[V];\
610 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
611 b = (type *)c->table_bU[U];
613 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
614 for (i=0; i<(dstW>>1); i++) {\
616 int Y1= buf0[i2 ]>>7;\
617 int Y2= buf0[i2+1]>>7;\
618 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
619 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
620 type av_unused *r, *b, *g;\
621 int av_unused A1, A2;\
627 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
628 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
629 r = (type *)c->table_rV[V];\
630 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
631 b = (type *)c->table_bU[U];
633 #define YSCALE_YUV_2_MONO2_C \
634 const uint8_t * const d128=dither_8x8_220[y&7];\
635 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
636 for (i=0; i<dstW-7; i+=8) {\
638 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
639 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
640 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
641 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
642 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
643 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
644 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
645 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
646 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
650 #define YSCALE_YUV_2_MONOX_C \
651 const uint8_t * const d128=dither_8x8_220[y&7];\
652 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
654 for (i=0; i<dstW-1; i+=2) {\
659 for (j=0; j<lumFilterSize; j++) {\
660 Y1 += lumSrc[j][i] * lumFilter[j];\
661 Y2 += lumSrc[j][i+1] * lumFilter[j];\
671 acc+= acc + g[Y1+d128[(i+0)&7]];\
672 acc+= acc + g[Y2+d128[(i+1)&7]];\
674 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
679 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
680 switch(c->dstFormat) {\
681 case PIX_FMT_RGB48BE:\
682 case PIX_FMT_RGB48LE:\
684 ((uint8_t*)dest)[ 0]= r[Y1];\
685 ((uint8_t*)dest)[ 1]= r[Y1];\
686 ((uint8_t*)dest)[ 2]= g[Y1];\
687 ((uint8_t*)dest)[ 3]= g[Y1];\
688 ((uint8_t*)dest)[ 4]= b[Y1];\
689 ((uint8_t*)dest)[ 5]= b[Y1];\
690 ((uint8_t*)dest)[ 6]= r[Y2];\
691 ((uint8_t*)dest)[ 7]= r[Y2];\
692 ((uint8_t*)dest)[ 8]= g[Y2];\
693 ((uint8_t*)dest)[ 9]= g[Y2];\
694 ((uint8_t*)dest)[10]= b[Y2];\
695 ((uint8_t*)dest)[11]= b[Y2];\
702 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
703 func(uint32_t,needAlpha)\
704 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
705 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
708 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
710 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
711 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
715 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
716 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
724 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
725 func(uint32_t,needAlpha)\
726 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
727 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
730 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
732 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
733 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
737 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
738 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
745 ((uint8_t*)dest)[0]= r[Y1];\
746 ((uint8_t*)dest)[1]= g[Y1];\
747 ((uint8_t*)dest)[2]= b[Y1];\
748 ((uint8_t*)dest)[3]= r[Y2];\
749 ((uint8_t*)dest)[4]= g[Y2];\
750 ((uint8_t*)dest)[5]= b[Y2];\
756 ((uint8_t*)dest)[0]= b[Y1];\
757 ((uint8_t*)dest)[1]= g[Y1];\
758 ((uint8_t*)dest)[2]= r[Y1];\
759 ((uint8_t*)dest)[3]= b[Y2];\
760 ((uint8_t*)dest)[4]= g[Y2];\
761 ((uint8_t*)dest)[5]= r[Y2];\
765 case PIX_FMT_RGB565BE:\
766 case PIX_FMT_RGB565LE:\
767 case PIX_FMT_BGR565BE:\
768 case PIX_FMT_BGR565LE:\
770 const int dr1= dither_2x2_8[y&1 ][0];\
771 const int dg1= dither_2x2_4[y&1 ][0];\
772 const int db1= dither_2x2_8[(y&1)^1][0];\
773 const int dr2= dither_2x2_8[y&1 ][1];\
774 const int dg2= dither_2x2_4[y&1 ][1];\
775 const int db2= dither_2x2_8[(y&1)^1][1];\
777 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
778 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
782 case PIX_FMT_RGB555BE:\
783 case PIX_FMT_RGB555LE:\
784 case PIX_FMT_BGR555BE:\
785 case PIX_FMT_BGR555LE:\
787 const int dr1= dither_2x2_8[y&1 ][0];\
788 const int dg1= dither_2x2_8[y&1 ][1];\
789 const int db1= dither_2x2_8[(y&1)^1][0];\
790 const int dr2= dither_2x2_8[y&1 ][1];\
791 const int dg2= dither_2x2_8[y&1 ][0];\
792 const int db2= dither_2x2_8[(y&1)^1][1];\
794 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
795 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
799 case PIX_FMT_RGB444BE:\
800 case PIX_FMT_RGB444LE:\
801 case PIX_FMT_BGR444BE:\
802 case PIX_FMT_BGR444LE:\
804 const int dr1= dither_4x4_16[y&3 ][0];\
805 const int dg1= dither_4x4_16[y&3 ][1];\
806 const int db1= dither_4x4_16[(y&3)^3][0];\
807 const int dr2= dither_4x4_16[y&3 ][1];\
808 const int dg2= dither_4x4_16[y&3 ][0];\
809 const int db2= dither_4x4_16[(y&3)^3][1];\
811 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
812 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
819 const uint8_t * const d64= dither_8x8_73[y&7];\
820 const uint8_t * const d32= dither_8x8_32[y&7];\
822 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
823 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
830 const uint8_t * const d64= dither_8x8_73 [y&7];\
831 const uint8_t * const d128=dither_8x8_220[y&7];\
833 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
834 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
838 case PIX_FMT_RGB4_BYTE:\
839 case PIX_FMT_BGR4_BYTE:\
841 const uint8_t * const d64= dither_8x8_73 [y&7];\
842 const uint8_t * const d128=dither_8x8_220[y&7];\
844 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
845 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
849 case PIX_FMT_MONOBLACK:\
850 case PIX_FMT_MONOWHITE:\
855 case PIX_FMT_YUYV422:\
857 ((uint8_t*)dest)[2*i2+0]= Y1;\
858 ((uint8_t*)dest)[2*i2+1]= U;\
859 ((uint8_t*)dest)[2*i2+2]= Y2;\
860 ((uint8_t*)dest)[2*i2+3]= V;\
863 case PIX_FMT_UYVY422:\
865 ((uint8_t*)dest)[2*i2+0]= U;\
866 ((uint8_t*)dest)[2*i2+1]= Y1;\
867 ((uint8_t*)dest)[2*i2+2]= V;\
868 ((uint8_t*)dest)[2*i2+3]= Y2;\
871 case PIX_FMT_GRAY16BE:\
873 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
874 ((uint8_t*)dest)[2*i2+1]= Y1;\
875 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
876 ((uint8_t*)dest)[2*i2+3]= Y2;\
879 case PIX_FMT_GRAY16LE:\
881 ((uint8_t*)dest)[2*i2+0]= Y1;\
882 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
883 ((uint8_t*)dest)[2*i2+2]= Y2;\
884 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
889 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
890 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
891 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
894 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)
897 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
898 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
899 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
902 int step= c->dstFormatBpp/8;
905 switch(c->dstFormat) {
913 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
914 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
915 dest[aidx]= needAlpha ? A : 255;
922 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
923 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
931 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
948 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
949 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
950 dest[aidx]= needAlpha ? A : 255;
957 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
958 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
966 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
981 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
984 uint8_t *ptr = plane + stride*y;
985 for (i=0; i<height; i++) {
986 memset(ptr, val, width);
991 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
995 for (i = 0; i < width; i++) {
1000 dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1004 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
1005 const uint8_t *src1, const uint8_t *src2,
1006 long width, uint32_t *unused)
1010 for (i = 0; i < width; i++) {
1011 int r = src1[6*i + 0];
1012 int g = src1[6*i + 2];
1013 int b = src1[6*i + 4];
1015 dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1016 dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1020 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1021 const uint8_t *src1, const uint8_t *src2,
1022 long width, uint32_t *unused)
1026 for (i = 0; i < width; i++) {
1027 int r= src1[12*i + 0] + src1[12*i + 6];
1028 int g= src1[12*i + 2] + src1[12*i + 8];
1029 int b= src1[12*i + 4] + src1[12*i + 10];
1031 dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1032 dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1036 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1037 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1040 for (i=0; i<width; i++) {\
1041 int b= (((const type*)src)[i]>>shb)&maskb;\
1042 int g= (((const type*)src)[i]>>shg)&maskg;\
1043 int r= (((const type*)src)[i]>>shr)&maskr;\
1045 dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1049 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1050 BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
1051 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
1052 BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY , GY<<8, BY , RGB2YUV_SHIFT+8)
1053 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
1054 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
1055 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1056 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1058 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1061 for (i=0; i<width; i++) {
1066 #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1067 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1070 for (i=0; i<width; i++) {\
1071 int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1072 int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1073 int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1075 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1076 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1079 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1082 for (i=0; i<width; i++) {\
1083 int pix0= ((const type*)src)[2*i+0]>>shp;\
1084 int pix1= ((const type*)src)[2*i+1]>>shp;\
1085 int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1086 int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1087 int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1088 g&= maskg|(2*maskg);\
1092 dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1093 dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1097 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1098 BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1099 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1100 BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
1101 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
1102 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
1103 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1104 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1106 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1109 for (i=0; i<width; i++) {
1112 dst[i]= pal[d] & 0xFF;
1116 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1117 const uint8_t *src1, const uint8_t *src2,
1118 long width, uint32_t *pal)
1121 assert(src1 == src2);
1122 for (i=0; i<width; i++) {
1123 int p= pal[src1[i]];
1130 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1133 for (i=0; i<width/8; i++) {
1136 dst[8*i+j]= ((d>>(7-j))&1)*255;
1140 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1143 for (i=0; i<width/8; i++) {
1146 dst[8*i+j]= ((d>>(7-j))&1)*255;
1150 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1152 #if CONFIG_RUNTIME_CPUDETECT
1153 # define COMPILE_C 1
1155 # define COMPILE_MMX HAVE_MMX
1156 # define COMPILE_MMX2 HAVE_MMX2
1157 # define COMPILE_3DNOW HAVE_AMD3DNOW
1159 # define COMPILE_ALTIVEC HAVE_ALTIVEC
1161 #else /* CONFIG_RUNTIME_CPUDETECT */
1164 # define COMPILE_MMX2 1
1165 # elif HAVE_AMD3DNOW
1166 # define COMPILE_3DNOW 1
1168 # define COMPILE_MMX 1
1170 # define COMPILE_C 1
1172 # elif ARCH_PPC && HAVE_ALTIVEC
1173 # define COMPILE_ALTIVEC 1
1175 # define COMPILE_C 1
1180 # define COMPILE_C 0
1183 # define COMPILE_MMX 0
1185 #ifndef COMPILE_MMX2
1186 # define COMPILE_MMX2 0
1188 #ifndef COMPILE_3DNOW
1189 # define COMPILE_3DNOW 0
1191 #ifndef COMPILE_ALTIVEC
1192 # define COMPILE_ALTIVEC 0
1195 #define COMPILE_TEMPLATE_MMX 0
1196 #define COMPILE_TEMPLATE_MMX2 0
1197 #define COMPILE_TEMPLATE_AMD3DNOW 0
1198 #define COMPILE_TEMPLATE_ALTIVEC 0
1201 #define RENAME(a) a ## _C
1202 #include "swscale_template.c"
1207 #undef COMPILE_TEMPLATE_ALTIVEC
1208 #define COMPILE_TEMPLATE_ALTIVEC 1
1209 #define RENAME(a) a ## _altivec
1210 #include "swscale_template.c"
1218 #undef COMPILE_TEMPLATE_MMX
1219 #undef COMPILE_TEMPLATE_MMX2
1220 #undef COMPILE_TEMPLATE_AMD3DNOW
1221 #define COMPILE_TEMPLATE_MMX 1
1222 #define COMPILE_TEMPLATE_MMX2 0
1223 #define COMPILE_TEMPLATE_AMD3DNOW 0
1224 #define RENAME(a) a ## _MMX
1225 #include "swscale_template.c"
1231 #undef COMPILE_TEMPLATE_MMX
1232 #undef COMPILE_TEMPLATE_MMX2
1233 #undef COMPILE_TEMPLATE_AMD3DNOW
1234 #define COMPILE_TEMPLATE_MMX 1
1235 #define COMPILE_TEMPLATE_MMX2 1
1236 #define COMPILE_TEMPLATE_AMD3DNOW 0
1237 #define RENAME(a) a ## _MMX2
1238 #include "swscale_template.c"
1244 #undef COMPILE_TEMPLATE_MMX
1245 #undef COMPILE_TEMPLATE_MMX2
1246 #undef COMPILE_TEMPLATE_AMD3DNOW
1247 #define COMPILE_TEMPLATE_MMX 1
1248 #define COMPILE_TEMPLATE_MMX2 0
1249 #define COMPILE_TEMPLATE_AMD3DNOW 1
1250 #define RENAME(a) a ## _3DNow
1251 #include "swscale_template.c"
1256 SwsFunc ff_getSwsFunc(SwsContext *c)
1258 #if CONFIG_RUNTIME_CPUDETECT
1259 int flags = c->flags;
1262 // ordered per speed fastest first
1263 if (flags & SWS_CPU_CAPS_MMX2) {
1264 sws_init_swScale_MMX2(c);
1265 return swScale_MMX2;
1266 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1267 sws_init_swScale_3DNow(c);
1268 return swScale_3DNow;
1269 } else if (flags & SWS_CPU_CAPS_MMX) {
1270 sws_init_swScale_MMX(c);
1273 sws_init_swScale_C(c);
1279 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1280 sws_init_swScale_altivec(c);
1281 return swScale_altivec;
1283 sws_init_swScale_C(c);
1287 sws_init_swScale_C(c);
1289 #endif /* ARCH_X86 */
1290 #else //CONFIG_RUNTIME_CPUDETECT
1291 #if COMPILE_TEMPLATE_MMX2
1292 sws_init_swScale_MMX2(c);
1293 return swScale_MMX2;
1294 #elif COMPILE_TEMPLATE_AMD3DNOW
1295 sws_init_swScale_3DNow(c);
1296 return swScale_3DNow;
1297 #elif COMPILE_TEMPLATE_MMX
1298 sws_init_swScale_MMX(c);
1300 #elif COMPILE_TEMPLATE_ALTIVEC
1301 sws_init_swScale_altivec(c);
1302 return swScale_altivec;
1304 sws_init_swScale_C(c);
1307 #endif //!CONFIG_RUNTIME_CPUDETECT
1310 static void copyPlane(const uint8_t *src, int srcStride,
1311 int srcSliceY, int srcSliceH, int width,
1312 uint8_t *dst, int dstStride)
1314 dst += dstStride * srcSliceY;
1315 if (dstStride == srcStride && srcStride > 0) {
1316 memcpy(dst, src, srcSliceH * dstStride);
1319 for (i=0; i<srcSliceH; i++) {
1320 memcpy(dst, src, width);
1327 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1328 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1330 uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1332 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1333 dstParam[0], dstStride[0]);
1335 if (c->dstFormat == PIX_FMT_NV12)
1336 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1338 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1343 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1344 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1346 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1348 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1353 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1354 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1356 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1358 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1363 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1364 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1366 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1368 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1373 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1374 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1376 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1378 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1383 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1384 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1386 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1387 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1388 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1390 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1393 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1398 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1399 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1401 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1402 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1403 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1405 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1410 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1411 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1413 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1414 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1415 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1417 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1420 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1425 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1426 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1428 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1429 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1430 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1432 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1437 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1440 for (i=0; i<num_pixels; i++)
1441 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1444 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1448 for (i=0; i<num_pixels; i++)
1449 ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1452 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1456 for (i=0; i<num_pixels; i++) {
1458 dst[0]= palette[src[i<<1]*4+0];
1459 dst[1]= palette[src[i<<1]*4+1];
1460 dst[2]= palette[src[i<<1]*4+2];
1465 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1466 int srcSliceH, uint8_t* dst[], int dstStride[])
1468 const enum PixelFormat srcFormat= c->srcFormat;
1469 const enum PixelFormat dstFormat= c->dstFormat;
1470 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1471 const uint8_t *palette)=NULL;
1473 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1474 const uint8_t *srcPtr= src[0];
1476 if (srcFormat == PIX_FMT_Y400A) {
1477 switch (dstFormat) {
1478 case PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
1479 case PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
1480 case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1481 case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1482 case PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
1483 case PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
1485 } else if (usePal(srcFormat)) {
1486 switch (dstFormat) {
1487 case PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
1488 case PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
1489 case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1490 case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1491 case PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
1492 case PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
1497 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1498 sws_format_name(srcFormat), sws_format_name(dstFormat));
1500 for (i=0; i<srcSliceH; i++) {
1501 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1502 srcPtr+= srcStride[0];
1503 dstPtr+= dstStride[0];
1510 #define isRGBA32(x) ( \
1511 (x) == PIX_FMT_ARGB \
1512 || (x) == PIX_FMT_RGBA \
1513 || (x) == PIX_FMT_BGRA \
1514 || (x) == PIX_FMT_ABGR \
1517 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1518 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1519 int srcSliceH, uint8_t* dst[], int dstStride[])
1521 const enum PixelFormat srcFormat= c->srcFormat;
1522 const enum PixelFormat dstFormat= c->dstFormat;
1523 const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1524 const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1525 const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1526 const int dstId= c->dstFormatBpp >> 2;
1527 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1529 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1531 if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1532 if ( CONV_IS(ABGR, RGBA)
1533 || CONV_IS(ARGB, BGRA)
1534 || CONV_IS(BGRA, ARGB)
1535 || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1536 else if (CONV_IS(ABGR, ARGB)
1537 || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1538 else if (CONV_IS(ABGR, BGRA)
1539 || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1540 else if (CONV_IS(BGRA, RGBA)
1541 || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1542 else if (CONV_IS(BGRA, ABGR)
1543 || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1546 if ( (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1547 || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1548 switch(srcId | (dstId<<4)) {
1549 case 0x34: conv= rgb16to15; break;
1550 case 0x36: conv= rgb24to15; break;
1551 case 0x38: conv= rgb32to15; break;
1552 case 0x43: conv= rgb15to16; break;
1553 case 0x46: conv= rgb24to16; break;
1554 case 0x48: conv= rgb32to16; break;
1555 case 0x63: conv= rgb15to24; break;
1556 case 0x64: conv= rgb16to24; break;
1557 case 0x68: conv= rgb32to24; break;
1558 case 0x83: conv= rgb15to32; break;
1559 case 0x84: conv= rgb16to32; break;
1560 case 0x86: conv= rgb24to32; break;
1562 } else if ( (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1563 || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1564 switch(srcId | (dstId<<4)) {
1565 case 0x33: conv= rgb15tobgr15; break;
1566 case 0x34: conv= rgb16tobgr15; break;
1567 case 0x36: conv= rgb24tobgr15; break;
1568 case 0x38: conv= rgb32tobgr15; break;
1569 case 0x43: conv= rgb15tobgr16; break;
1570 case 0x44: conv= rgb16tobgr16; break;
1571 case 0x46: conv= rgb24tobgr16; break;
1572 case 0x48: conv= rgb32tobgr16; break;
1573 case 0x63: conv= rgb15tobgr24; break;
1574 case 0x64: conv= rgb16tobgr24; break;
1575 case 0x66: conv= rgb24tobgr24; break;
1576 case 0x68: conv= rgb32tobgr24; break;
1577 case 0x83: conv= rgb15tobgr32; break;
1578 case 0x84: conv= rgb16tobgr32; break;
1579 case 0x86: conv= rgb24tobgr32; break;
1584 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1585 sws_format_name(srcFormat), sws_format_name(dstFormat));
1587 const uint8_t *srcPtr= src[0];
1588 uint8_t *dstPtr= dst[0];
1589 if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1590 srcPtr += ALT32_CORR;
1592 if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1593 dstPtr += ALT32_CORR;
1595 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1596 conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1599 dstPtr += dstStride[0]*srcSliceY;
1601 for (i=0; i<srcSliceH; i++) {
1602 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1603 srcPtr+= srcStride[0];
1604 dstPtr+= dstStride[0];
1611 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1612 int srcSliceH, uint8_t* dst[], int dstStride[])
1616 dst[0]+ srcSliceY *dstStride[0],
1617 dst[1]+(srcSliceY>>1)*dstStride[1],
1618 dst[2]+(srcSliceY>>1)*dstStride[2],
1620 dstStride[0], dstStride[1], srcStride[0]);
1622 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1626 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1627 int srcSliceH, uint8_t* dst[], int dstStride[])
1629 copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1630 dst[0], dstStride[0]);
1632 planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1633 srcSliceH >> 2, srcStride[1], dstStride[1]);
1634 planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1635 srcSliceH >> 2, srcStride[2], dstStride[2]);
1637 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1641 /* unscaled copy like stuff (assumes nearly identical formats) */
1642 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1643 int srcSliceH, uint8_t* dst[], int dstStride[])
1645 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1646 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1649 const uint8_t *srcPtr= src[0];
1650 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1653 /* universal length finder */
1654 while(length+c->srcW <= FFABS(dstStride[0])
1655 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1658 for (i=0; i<srcSliceH; i++) {
1659 memcpy(dstPtr, srcPtr, length);
1660 srcPtr+= srcStride[0];
1661 dstPtr+= dstStride[0];
1667 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1668 int srcSliceH, uint8_t* dst[], int dstStride[])
1671 for (plane=0; plane<4; plane++) {
1672 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1673 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1674 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1675 const uint8_t *srcPtr= src[plane];
1676 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1678 if (!dst[plane]) continue;
1679 // ignore palette for GRAY8
1680 if (plane == 1 && !dst[2]) continue;
1681 if (!src[plane] || (plane == 1 && !src[2])) {
1682 if(is16BPS(c->dstFormat))
1684 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1686 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1687 if (!isBE(c->srcFormat)) srcPtr++;
1688 for (i=0; i<height; i++) {
1689 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1690 srcPtr+= srcStride[plane];
1691 dstPtr+= dstStride[plane];
1693 } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1694 for (i=0; i<height; i++) {
1695 for (j=0; j<length; j++) {
1696 dstPtr[ j<<1 ] = srcPtr[j];
1697 dstPtr[(j<<1)+1] = srcPtr[j];
1699 srcPtr+= srcStride[plane];
1700 dstPtr+= dstStride[plane];
1702 } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1703 && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1705 for (i=0; i<height; i++) {
1706 for (j=0; j<length; j++)
1707 ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1708 srcPtr+= srcStride[plane];
1709 dstPtr+= dstStride[plane];
1711 } else if (dstStride[plane] == srcStride[plane] &&
1712 srcStride[plane] > 0 && srcStride[plane] == length) {
1713 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1714 height*dstStride[plane]);
1716 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1718 for (i=0; i<height; i++) {
1719 memcpy(dstPtr, srcPtr, length);
1720 srcPtr+= srcStride[plane];
1721 dstPtr+= dstStride[plane];
1729 int ff_hardcodedcpuflags(void)
1732 #if COMPILE_TEMPLATE_MMX2
1733 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1734 #elif COMPILE_TEMPLATE_AMD3DNOW
1735 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
1736 #elif COMPILE_TEMPLATE_MMX
1737 flags |= SWS_CPU_CAPS_MMX;
1738 #elif COMPILE_TEMPLATE_ALTIVEC
1739 flags |= SWS_CPU_CAPS_ALTIVEC;
1741 flags |= SWS_CPU_CAPS_BFIN;
1746 void ff_get_unscaled_swscale(SwsContext *c)
1748 const enum PixelFormat srcFormat = c->srcFormat;
1749 const enum PixelFormat dstFormat = c->dstFormat;
1750 const int flags = c->flags;
1751 const int dstH = c->dstH;
1754 needsDither= isAnyRGB(dstFormat)
1755 && c->dstFormatBpp < 24
1756 && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1759 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1760 c->swScale= planarToNv12Wrapper;
1763 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1764 && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1765 c->swScale= ff_yuv2rgb_get_func_ptr(c);
1768 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1769 c->swScale= yvu9ToYv12Wrapper;
1773 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1774 c->swScale= bgr24ToYv12Wrapper;
1776 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1777 if ( isAnyRGB(srcFormat)
1778 && isAnyRGB(dstFormat)
1779 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
1780 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
1781 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
1782 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
1783 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1784 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1785 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1786 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1787 && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
1788 && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
1789 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1790 c->swScale= rgbToRgbWrapper;
1792 if ((usePal(srcFormat) && (
1793 dstFormat == PIX_FMT_RGB32 ||
1794 dstFormat == PIX_FMT_RGB32_1 ||
1795 dstFormat == PIX_FMT_RGB24 ||
1796 dstFormat == PIX_FMT_BGR32 ||
1797 dstFormat == PIX_FMT_BGR32_1 ||
1798 dstFormat == PIX_FMT_BGR24)))
1799 c->swScale= palToRgbWrapper;
1801 if (srcFormat == PIX_FMT_YUV422P) {
1802 if (dstFormat == PIX_FMT_YUYV422)
1803 c->swScale= yuv422pToYuy2Wrapper;
1804 else if (dstFormat == PIX_FMT_UYVY422)
1805 c->swScale= yuv422pToUyvyWrapper;
1808 /* LQ converters if -sws 0 or -sws 4*/
1809 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1811 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1812 if (dstFormat == PIX_FMT_YUYV422)
1813 c->swScale= planarToYuy2Wrapper;
1814 else if (dstFormat == PIX_FMT_UYVY422)
1815 c->swScale= planarToUyvyWrapper;
1818 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1819 c->swScale= yuyvToYuv420Wrapper;
1820 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1821 c->swScale= uyvyToYuv420Wrapper;
1822 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1823 c->swScale= yuyvToYuv422Wrapper;
1824 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1825 c->swScale= uyvyToYuv422Wrapper;
1828 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
1829 !(c->flags & SWS_BITEXACT) &&
1830 srcFormat == PIX_FMT_YUV420P) {
1831 // unscaled YV12 -> packed YUV, we want speed
1832 if (dstFormat == PIX_FMT_YUYV422)
1833 c->swScale= yv12toyuy2_unscaled_altivec;
1834 else if (dstFormat == PIX_FMT_UYVY422)
1835 c->swScale= yv12touyvy_unscaled_altivec;
1840 if ( srcFormat == dstFormat
1841 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1842 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1843 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1844 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1845 || (isGray(dstFormat) && isGray(srcFormat))
1846 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1847 && c->chrDstHSubSample == c->chrSrcHSubSample
1848 && c->chrDstVSubSample == c->chrSrcVSubSample
1849 && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1850 && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1852 if (isPacked(c->srcFormat))
1853 c->swScale= packedCopyWrapper;
1854 else /* Planar YUV or gray */
1855 c->swScale= planarCopyWrapper;
1858 if (flags & SWS_CPU_CAPS_BFIN)
1859 ff_bfin_get_unscaled_swscale (c);
1863 static void reset_ptr(const uint8_t* src[], int format)
1865 if(!isALPHA(format))
1867 if(!isPlanarYUV(format)) {
1870 if (!usePal(format))
1875 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1876 const int linesizes[4])
1878 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1881 for (i = 0; i < 4; i++) {
1882 int plane = desc->comp[i].plane;
1883 if (!data[plane] || !linesizes[plane])
1891 * swscale wrapper, so we don't need to export the SwsContext.
1892 * Assumes planar YUV to be in YUV order instead of YVU.
1894 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1895 int srcSliceH, uint8_t* const dst[], const int dstStride[])
1898 const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1899 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1901 // do not mess up sliceDir if we have a "trailing" 0-size slice
1905 if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1906 av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1909 if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1910 av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1914 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1915 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1918 if (c->sliceDir == 0) {
1919 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1922 if (usePal(c->srcFormat)) {
1923 for (i=0; i<256; i++) {
1924 int p, r, g, b,y,u,v;
1925 if(c->srcFormat == PIX_FMT_PAL8) {
1926 p=((const uint32_t*)(src[1]))[i];
1930 } else if(c->srcFormat == PIX_FMT_RGB8) {
1934 } else if(c->srcFormat == PIX_FMT_BGR8) {
1938 } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1942 } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
1945 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
1950 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1951 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1952 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
1953 c->pal_yuv[i]= y + (u<<8) + (v<<16);
1955 switch(c->dstFormat) {
1960 c->pal_rgb[i]= r + (g<<8) + (b<<16);
1962 case PIX_FMT_BGR32_1:
1966 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
1968 case PIX_FMT_RGB32_1:
1972 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
1979 c->pal_rgb[i]= b + (g<<8) + (r<<16);
1984 // copy strides, so they can safely be modified
1985 if (c->sliceDir == 1) {
1986 // slices go from top to bottom
1987 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
1988 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
1990 reset_ptr(src2, c->srcFormat);
1991 reset_ptr((const uint8_t**)dst2, c->dstFormat);
1993 /* reset slice direction at end of frame */
1994 if (srcSliceY + srcSliceH == c->srcH)
1997 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
1999 // slices go from bottom to top => we flip the image internally
2000 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2001 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2003 src2[0] += (srcSliceH-1)*srcStride[0];
2004 if (!usePal(c->srcFormat))
2005 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2006 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2007 src2[3] += (srcSliceH-1)*srcStride[3];
2008 dst2[0] += ( c->dstH -1)*dstStride[0];
2009 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2010 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2011 dst2[3] += ( c->dstH -1)*dstStride[3];
2013 reset_ptr(src2, c->srcFormat);
2014 reset_ptr((const uint8_t**)dst2, c->dstFormat);
2016 /* reset slice direction at end of frame */
2020 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2024 #if LIBSWSCALE_VERSION_MAJOR < 1
2025 int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2026 int srcSliceH, uint8_t* dst[], int dstStride[])
2028 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2032 /* Convert the palette to the same packed 32-bit format as the palette */
2033 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2037 for (i=0; i<num_pixels; i++)
2038 ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2041 /* Palette format: ABCD -> dst format: ABC */
2042 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2046 for (i=0; i<num_pixels; i++) {
2048 dst[0]= palette[src[i]*4+0];
2049 dst[1]= palette[src[i]*4+1];
2050 dst[2]= palette[src[i]*4+2];